add some checker

This commit is contained in:
SimonFJ20 2023-05-01 13:36:06 +02:00
parent a431185747
commit 0e46ac7bf5
4 changed files with 33 additions and 4 deletions

View File

@ -22,6 +22,8 @@ export type CheckedExpr =
} | { } | {
exprType: "block"; exprType: "block";
body: CheckedExpr[]; body: CheckedExpr[];
value: CheckedExpr | null;
valueType: CheckedType;
} | { } | {
exprType: "call"; exprType: "call";
subject: CheckedExpr; subject: CheckedExpr;

View File

@ -1,7 +1,15 @@
import { CheckedExpr, CheckedType } from "./checked.ts"; import { CheckedExpr, CheckedType } from "./checked.ts";
import { ParsedExpr } from "./parsed.ts"; import { ParsedExpr as AnyParsedExpr } from "./parsed.ts";
import { CompileError, Position } from "./token.ts"; import { CompileError, Position } from "./token.ts";
type ParsedExpr<
Type extends AnyParsedExpr["exprType"] = AnyParsedExpr["exprType"],
> =
& AnyParsedExpr
& {
exprType: Type;
};
export type SymbolValue = { export type SymbolValue = {
id: number; id: number;
subject: string; subject: string;
@ -49,7 +57,7 @@ export class Checker {
} }
} }
private searchTopLevelFn(statement: ParsedExpr & { exprType: "fn" }) { private searchTopLevelFn(statement: ParsedExpr<"fn">) {
} }
private checkExpr(expr: ParsedExpr): CheckedExpr { private checkExpr(expr: ParsedExpr): CheckedExpr {
@ -61,6 +69,7 @@ export class Checker {
case "id": case "id":
throw new Error("not implemented"); throw new Error("not implemented");
case "int": case "int":
return this.checkInt(expr);
case "if": case "if":
case "block": case "block":
case "call": case "call":
@ -78,6 +87,15 @@ export class Checker {
} }
} }
private checkInt(expr: ParsedExpr<"int">): CheckedExpr {
return {
pos: expr.pos,
exprType: "int",
value: expr.value,
valueType: { pos: expr.pos, typeType: "i16" },
};
}
private errorExpr(message: string, pos: Position): CheckedExpr { private errorExpr(message: string, pos: Position): CheckedExpr {
this.errors.push({ message, pos }); this.errors.push({ message, pos });
return { return {

View File

@ -50,6 +50,7 @@ export type ParsedExpr =
} | { } | {
exprType: "block"; exprType: "block";
body: ParsedExpr[]; body: ParsedExpr[];
value: ParsedExpr | null;
} | { } | {
exprType: "call"; exprType: "call";
subject: ParsedExpr; subject: ParsedExpr;

View File

@ -629,9 +629,16 @@ export class Parser {
this.step(); this.step();
while (this.currentIs("semicolon")) this.step(); while (this.currentIs("semicolon")) this.step();
const body: ParsedExpr[] = []; const body: ParsedExpr[] = [];
let value: ParsedExpr | null = null;
while (!this.done() && this.current.tokenType != "rbrace") { while (!this.done() && this.current.tokenType != "rbrace") {
body.push(this.parseStatement()); const exprOrStatement = this.parseStatement();
if (this.currentIs("semicolon")) {
body.push(exprOrStatement);
while (this.currentIs("semicolon")) this.step(); while (this.currentIs("semicolon")) this.step();
} else {
value = exprOrStatement;
break;
}
} }
if (!this.currentIs("rbrace")) { if (!this.currentIs("rbrace")) {
return this.errorExpr( return this.errorExpr(
@ -643,6 +650,7 @@ export class Parser {
pos, pos,
exprType: "block", exprType: "block",
body, body,
value,
}; };
} }