symbol resolver

This commit is contained in:
Mikkel Kongsted 2024-12-06 12:21:57 +01:00
parent 68efbfe7a2
commit 8771d887c0
3 changed files with 56 additions and 5 deletions

View File

@ -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) {

View File

@ -3,7 +3,7 @@ fn sum(a, b) {
+ a b;
}
add(2,3); // -> 5
sum(2,3); // -> 5
let a = "Hello";

View File

@ -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))