2024-12-10 13:36:41 +00:00
|
|
|
import { Expr, Stmt } from "./ast.ts";
|
2024-12-13 22:22:08 +00:00
|
|
|
import { AstVisitor, visitExpr, VisitRes, visitStmts } from "./ast_visitor.ts";
|
2024-12-11 11:36:19 +00:00
|
|
|
import { printStackTrace, Reporter } from "./info.ts";
|
2024-12-10 13:36:41 +00:00
|
|
|
import {
|
|
|
|
FnSyms,
|
|
|
|
GlobalSyms,
|
|
|
|
LeafSyms,
|
|
|
|
StaticSyms,
|
|
|
|
Syms,
|
|
|
|
} from "./resolver_syms.ts";
|
2024-12-10 20:42:15 +00:00
|
|
|
import { Pos } from "./token.ts";
|
2024-11-26 18:37:21 +00:00
|
|
|
|
2024-12-13 22:22:08 +00:00
|
|
|
export class Resolver implements AstVisitor<[Syms]> {
|
2024-12-10 13:36:41 +00:00
|
|
|
private root = new GlobalSyms();
|
2024-11-26 18:37:21 +00:00
|
|
|
|
2024-12-11 11:36:19 +00:00
|
|
|
public constructor(private reporter: Reporter) {
|
|
|
|
}
|
2024-12-11 02:11:00 +00:00
|
|
|
|
2024-12-13 22:22:08 +00:00
|
|
|
public resolve(stmts: Stmt[]): VisitRes {
|
2024-12-10 13:36:41 +00:00
|
|
|
const scopeSyms = new StaticSyms(this.root);
|
|
|
|
this.scoutFnStmts(stmts, scopeSyms);
|
2024-12-13 22:22:08 +00:00
|
|
|
visitStmts(stmts, this, scopeSyms);
|
|
|
|
return "stop";
|
|
|
|
}
|
|
|
|
|
|
|
|
visitLetStmt(stmt: Stmt, syms: Syms): VisitRes {
|
|
|
|
if (stmt.kind.type !== "let") {
|
|
|
|
throw new Error("expected let statement");
|
|
|
|
}
|
|
|
|
visitExpr(stmt.kind.value, this, syms);
|
|
|
|
const ident = stmt.kind.param.ident;
|
|
|
|
if (syms.definedLocally(ident)) {
|
|
|
|
this.reportAlreadyDefined(ident, stmt.pos, syms);
|
|
|
|
return;
|
2024-11-26 18:37:21 +00:00
|
|
|
}
|
2024-12-13 22:22:08 +00:00
|
|
|
syms.define(ident, {
|
|
|
|
ident,
|
|
|
|
type: "let",
|
|
|
|
pos: stmt.kind.param.pos,
|
|
|
|
stmt,
|
|
|
|
param: stmt.kind.param,
|
|
|
|
});
|
|
|
|
return "stop";
|
2024-11-26 18:37:21 +00:00
|
|
|
}
|
|
|
|
|
2024-12-10 13:36:41 +00:00
|
|
|
private scoutFnStmts(stmts: Stmt[], syms: Syms) {
|
|
|
|
for (const stmt of stmts) {
|
|
|
|
if (stmt.kind.type !== "fn") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (syms.definedLocally(stmt.kind.ident)) {
|
|
|
|
this.reportAlreadyDefined(stmt.kind.ident, stmt.pos, syms);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const ident = stmt.kind.ident;
|
|
|
|
syms.define(ident, {
|
|
|
|
ident: stmt.kind.ident,
|
|
|
|
type: "fn",
|
|
|
|
pos: stmt.pos,
|
|
|
|
stmt,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-13 22:22:08 +00:00
|
|
|
visitFnStmt(stmt: Stmt, syms: Syms): VisitRes {
|
|
|
|
if (stmt.kind.type !== "fn") {
|
|
|
|
throw new Error("expected fn statement");
|
2024-12-06 11:21:57 +00:00
|
|
|
}
|
2024-12-13 22:22:08 +00:00
|
|
|
const fnScopeSyms = new FnSyms(syms);
|
|
|
|
for (const param of stmt.kind.params) {
|
|
|
|
if (fnScopeSyms.definedLocally(param.ident)) {
|
|
|
|
this.reportAlreadyDefined(param.ident, param.pos, syms);
|
|
|
|
continue;
|
2024-12-06 11:21:57 +00:00
|
|
|
}
|
2024-12-13 22:22:08 +00:00
|
|
|
fnScopeSyms.define(param.ident, {
|
|
|
|
ident: param.ident,
|
|
|
|
type: "fn_param",
|
|
|
|
pos: param.pos,
|
|
|
|
param,
|
|
|
|
});
|
2024-12-06 11:21:57 +00:00
|
|
|
}
|
2024-12-13 22:22:08 +00:00
|
|
|
visitExpr(stmt.kind.body, this, fnScopeSyms);
|
|
|
|
return "stop";
|
2024-11-26 18:37:21 +00:00
|
|
|
}
|
|
|
|
|
2024-12-13 22:22:08 +00:00
|
|
|
visitIdentExpr(expr: Expr, syms: Syms): VisitRes {
|
2024-12-10 09:39:12 +00:00
|
|
|
if (expr.kind.type !== "ident") {
|
2024-11-27 14:10:48 +00:00
|
|
|
throw new Error("expected ident");
|
2024-12-10 09:39:12 +00:00
|
|
|
}
|
2024-11-27 14:10:48 +00:00
|
|
|
const ident = expr.kind;
|
|
|
|
const symResult = syms.get(ident.value);
|
|
|
|
if (!symResult.ok) {
|
|
|
|
this.reportUseOfUndefined(ident.value, expr.pos, syms);
|
|
|
|
return;
|
|
|
|
}
|
2024-12-10 09:39:12 +00:00
|
|
|
const sym = symResult.sym;
|
2024-11-27 14:10:48 +00:00
|
|
|
expr.kind = {
|
|
|
|
type: "sym",
|
|
|
|
ident: ident.value,
|
2024-12-10 13:36:41 +00:00
|
|
|
sym,
|
2024-11-27 14:10:48 +00:00
|
|
|
};
|
2024-12-13 22:22:08 +00:00
|
|
|
return "stop";
|
2024-11-26 18:37:21 +00:00
|
|
|
}
|
|
|
|
|
2024-12-13 22:22:08 +00:00
|
|
|
visitBlockExpr(expr: Expr, syms: Syms): VisitRes {
|
|
|
|
if (expr.kind.type !== "block") {
|
|
|
|
throw new Error();
|
2024-12-10 09:39:12 +00:00
|
|
|
}
|
2024-12-13 22:22:08 +00:00
|
|
|
const childSyms = new LeafSyms(syms);
|
|
|
|
this.scoutFnStmts(expr.kind.stmts, childSyms);
|
|
|
|
visitStmts(expr.kind.stmts, this, childSyms);
|
|
|
|
if (expr.kind.expr) {
|
|
|
|
visitExpr(expr.kind.expr, this, childSyms);
|
2024-11-27 14:10:48 +00:00
|
|
|
}
|
2024-12-13 22:22:08 +00:00
|
|
|
return "stop";
|
2024-11-26 18:37:21 +00:00
|
|
|
}
|
|
|
|
|
2024-12-10 13:36:41 +00:00
|
|
|
private reportUseOfUndefined(ident: string, pos: Pos, _syms: Syms) {
|
2024-12-11 02:11:00 +00:00
|
|
|
this.reporter.reportError({
|
|
|
|
reporter: "Resolver",
|
|
|
|
msg: `use of undefined symbol '${ident}'`,
|
|
|
|
pos,
|
|
|
|
});
|
2024-12-11 11:36:19 +00:00
|
|
|
printStackTrace();
|
2024-11-26 18:37:21 +00:00
|
|
|
}
|
|
|
|
|
2024-12-10 09:39:12 +00:00
|
|
|
private reportAlreadyDefined(ident: string, pos: Pos, syms: Syms) {
|
2024-12-11 02:11:00 +00:00
|
|
|
this.reporter.reportError({
|
|
|
|
reporter: "Resolver",
|
|
|
|
msg: `symbol already defined '${ident}'`,
|
|
|
|
pos,
|
|
|
|
});
|
2024-11-26 18:37:21 +00:00
|
|
|
const prev = syms.get(ident);
|
2024-12-10 09:39:12 +00:00
|
|
|
if (!prev.ok) {
|
2024-11-26 18:37:21 +00:00
|
|
|
throw new Error("expected to be defined");
|
2024-12-10 09:39:12 +00:00
|
|
|
}
|
|
|
|
if (!prev.sym.pos) {
|
2024-11-26 18:37:21 +00:00
|
|
|
return;
|
2024-12-10 09:39:12 +00:00
|
|
|
}
|
2024-12-11 02:11:00 +00:00
|
|
|
this.reporter.addNote({
|
|
|
|
reporter: "Resolver",
|
|
|
|
msg: `previous definition of '${ident}'`,
|
|
|
|
pos: prev.sym.pos,
|
|
|
|
});
|
2024-12-11 11:36:19 +00:00
|
|
|
printStackTrace();
|
2024-11-26 18:37:21 +00:00
|
|
|
}
|
2024-12-10 09:39:12 +00:00
|
|
|
}
|