diff --git a/checked.ts b/checked.ts index 7d997d1..f96e386 100644 --- a/checked.ts +++ b/checked.ts @@ -22,6 +22,8 @@ export type CheckedExpr = } | { exprType: "block"; body: CheckedExpr[]; + value: CheckedExpr | null; + valueType: CheckedType; } | { exprType: "call"; subject: CheckedExpr; diff --git a/checker.ts b/checker.ts index 006e755..353f3d1 100644 --- a/checker.ts +++ b/checker.ts @@ -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 { diff --git a/parsed.ts b/parsed.ts index de3c711..6e5abbb 100644 --- a/parsed.ts +++ b/parsed.ts @@ -50,6 +50,7 @@ export type ParsedExpr = } | { exprType: "block"; body: ParsedExpr[]; + value: ParsedExpr | null; } | { exprType: "call"; subject: ParsedExpr; diff --git a/parser.ts b/parser.ts index 67001c6..487b832 100644 --- a/parser.ts +++ b/parser.ts @@ -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()); - while (this.currentIs("semicolon")) this.step(); + 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, }; }