diff --git a/compiler/Syms.ts b/compiler/Syms.ts index 86dc70c..109888d 100644 --- a/compiler/Syms.ts +++ b/compiler/Syms.ts @@ -59,7 +59,45 @@ export class Resolver { } return; } - throw new Error(`unknown expression ${expr.kind.type}`); + if (expr.kind.type === "group") { + this.resolveExpr(expr.kind.expr, syms) + return; + } + if (expr.kind.type === "field") { + this.resolveExpr(expr.kind.subject, syms) + return; + } + if (expr.kind.type === "index") { + this.resolveExpr(expr.kind.subject, syms) + this.resolveExpr(expr.kind.value, syms) + return; + } + if (expr.kind.type === "call") { + this.resolveExpr(expr.kind.subject, syms) + for (const e of expr.kind.args) { + this.resolveExpr(e, syms) + } + return; + } + if (expr.kind.type === "unary") { + this.resolveExpr(expr.kind.subject, syms) + return; + } + if (expr.kind.type === "if") { + this.resolveExpr(expr.kind.cond, syms) + this.resolveExpr(expr.kind.truthy, syms) + if (expr.kind.falsy !== undefined) { + this.resolveExpr(expr.kind.falsy, syms) + } + return; + } + if (expr.kind.type === "loop") { + this.resolveExpr(expr.kind.body, syms) + return; + } + if (expr.kind.type === "int" || expr.kind.type === "bool" || expr.kind.type === "null" || expr.kind.type === "string" || expr.kind.type === "sym") { + return; + } } private resolveIdentExpr(expr: Expr, syms: Syms) { @@ -100,7 +138,21 @@ export class Resolver { this.resolveExpr(stmt.kind.expr, syms); return; } - throw new Error(`unknown statement ${stmt.kind.type}`); + if (stmt.kind.type === "break") { + if (stmt.kind.expr !== undefined) { + this.resolveExpr(stmt.kind.expr, syms) + } + return; + } + if (stmt.kind.type === "assign") { + this.resolveExpr(stmt.kind.subject, syms) + this.resolveExpr(stmt.kind.value, syms) + return; + } + if (stmt.kind.type === "expr") { + this.resolveExpr(stmt.kind.expr, syms) + return; + } } private resolveLetStmt(stmt: Stmt, syms: Syms) { diff --git a/compiler/example-no-types.slg b/compiler/example-no-types.slg index aafec18..b1bd82b 100644 --- a/compiler/example-no-types.slg +++ b/compiler/example-no-types.slg @@ -3,7 +3,7 @@ fn sum(a, b) { + a b; } -add(2,3); // -> 5 +sum(2,3); // -> 5 let a = "Hello"; diff --git a/compiler/main.ts b/compiler/main.ts index 00131f7..2de6dd6 100644 --- a/compiler/main.ts +++ b/compiler/main.ts @@ -15,6 +15,5 @@ const lexer = new Lexer(text); const parser = new Parser(lexer) const ast = parser.parseStmts() -// const resolver = new Resolver() -// const resolvedAst = resolver.resolve(ast) +new Resolver().resolve(ast) console.log(JSON.stringify(ast, null, 4))