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";
body: CheckedExpr[];
value: CheckedExpr | null;
valueType: CheckedType;
} | {
exprType: "call";
subject: CheckedExpr;

View File

@ -1,7 +1,15 @@
import { CheckedExpr, CheckedType } from "./checked.ts";
import { ParsedExpr } from "./parsed.ts";
import { ParsedExpr as AnyParsedExpr } from "./parsed.ts";
import { CompileError, Position } from "./token.ts";
type ParsedExpr<
Type extends AnyParsedExpr["exprType"] = AnyParsedExpr["exprType"],
> =
& AnyParsedExpr
& {
exprType: Type;
};
export type SymbolValue = {
id: number;
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 {
@ -61,6 +69,7 @@ export class Checker {
case "id":
throw new Error("not implemented");
case "int":
return this.checkInt(expr);
case "if":
case "block":
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 {
this.errors.push({ message, pos });
return {

View File

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

View File

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