slige/compiler/Resolver.ts

240 lines
7.0 KiB
TypeScript
Raw Normal View History

2024-11-26 18:37:21 +00:00
import { Expr, Stmt, Sym } from "./ast.ts";
import { Pos } from "./Token.ts";
2024-12-10 09:39:12 +00:00
type SymMap = { [ident: string]: Sym };
2024-11-26 18:37:21 +00:00
class Syms {
private syms: SymMap = {};
public constructor(private parent?: Syms) {}
public define(ident: string, sym: Sym) {
this.syms[ident] = sym;
}
public definedLocally(ident: string): boolean {
return ident in this.syms;
}
2024-12-10 09:39:12 +00:00
public get(ident: string): { ok: true; sym: Sym } | { ok: false } {
if (ident in this.syms) {
2024-11-26 18:37:21 +00:00
return { ok: true, sym: this.syms[ident] };
2024-12-10 09:39:12 +00:00
}
if (this.parent) {
2024-11-26 18:37:21 +00:00
return this.parent.get(ident);
2024-12-10 09:39:12 +00:00
}
2024-11-26 18:37:21 +00:00
return { ok: false };
}
}
2024-11-27 14:10:48 +00:00
export class Resolver {
2024-11-26 18:37:21 +00:00
private root = new Syms();
2024-11-27 14:10:48 +00:00
public resolve(stmts: Stmt[]) {
const scopeSyms = new Syms(this.root);
for (const stmt of stmts) {
this.resolveStmt(stmt, scopeSyms);
2024-11-26 18:37:21 +00:00
}
}
private resolveExpr(expr: Expr, syms: Syms) {
if (expr.kind.type === "error") {
return;
}
if (expr.kind.type === "ident") {
2024-11-27 14:10:48 +00:00
this.resolveIdentExpr(expr, syms);
2024-11-26 18:37:21 +00:00
return;
}
if (expr.kind.type === "binary") {
this.resolveExpr(expr.kind.left, syms);
this.resolveExpr(expr.kind.right, syms);
return;
}
if (expr.kind.type === "block") {
const childSyms = new Syms(syms);
for (const stmt of expr.kind.stmts) {
this.resolveStmt(stmt, childSyms);
}
if (expr.kind.expr) {
this.resolveExpr(expr.kind.expr, childSyms);
}
return;
}
2024-12-06 11:21:57 +00:00
if (expr.kind.type === "group") {
2024-12-10 09:39:12 +00:00
this.resolveExpr(expr.kind.expr, syms);
2024-12-06 11:21:57 +00:00
return;
}
if (expr.kind.type === "field") {
2024-12-10 09:39:12 +00:00
this.resolveExpr(expr.kind.subject, syms);
2024-12-06 11:21:57 +00:00
return;
}
if (expr.kind.type === "index") {
2024-12-10 09:39:12 +00:00
this.resolveExpr(expr.kind.subject, syms);
this.resolveExpr(expr.kind.value, syms);
2024-12-06 11:21:57 +00:00
return;
}
if (expr.kind.type === "call") {
2024-12-10 09:39:12 +00:00
this.resolveExpr(expr.kind.subject, syms);
2024-12-06 11:21:57 +00:00
for (const e of expr.kind.args) {
2024-12-10 09:39:12 +00:00
this.resolveExpr(e, syms);
2024-12-06 11:21:57 +00:00
}
return;
}
if (expr.kind.type === "unary") {
2024-12-10 09:39:12 +00:00
this.resolveExpr(expr.kind.subject, syms);
2024-12-06 11:21:57 +00:00
return;
}
if (expr.kind.type === "if") {
2024-12-10 09:39:12 +00:00
this.resolveExpr(expr.kind.cond, syms);
this.resolveExpr(expr.kind.truthy, syms);
2024-12-06 11:21:57 +00:00
if (expr.kind.falsy !== undefined) {
2024-12-10 09:39:12 +00:00
this.resolveExpr(expr.kind.falsy, syms);
2024-12-06 11:21:57 +00:00
}
return;
}
if (expr.kind.type === "loop") {
2024-12-10 09:39:12 +00:00
this.resolveExpr(expr.kind.body, syms);
2024-12-06 11:21:57 +00:00
return;
}
2024-12-10 09:39:12 +00:00
if (
expr.kind.type === "int" || expr.kind.type === "bool" ||
expr.kind.type === "null" || expr.kind.type === "string" ||
expr.kind.type === "sym"
) {
2024-12-06 11:21:57 +00:00
return;
}
2024-11-26 18:37:21 +00:00
}
2024-11-27 14:10:48 +00:00
private resolveIdentExpr(expr: Expr, syms: Syms) {
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,
defType: sym.type,
};
2024-12-10 09:39:12 +00:00
if (sym.stmt) {
2024-11-27 14:10:48 +00:00
expr.kind.stmt = sym.stmt;
2024-12-10 09:39:12 +00:00
}
if (sym.param) {
2024-11-27 14:10:48 +00:00
expr.kind.param = sym.param;
2024-12-10 09:39:12 +00:00
}
2024-11-26 18:37:21 +00:00
}
private resolveStmt(stmt: Stmt, syms: Syms) {
if (stmt.kind.type === "error") {
return;
}
if (stmt.kind.type === "let") {
this.resolveLetStmt(stmt, syms);
return;
}
if (stmt.kind.type === "fn") {
this.resolveFnStmt(stmt, syms);
return;
}
if (stmt.kind.type === "return") {
2024-12-10 09:39:12 +00:00
if (stmt.kind.expr) {
2024-11-26 18:37:21 +00:00
this.resolveExpr(stmt.kind.expr, syms);
2024-12-10 09:39:12 +00:00
}
2024-11-26 18:37:21 +00:00
return;
}
2024-12-06 11:21:57 +00:00
if (stmt.kind.type === "break") {
if (stmt.kind.expr !== undefined) {
2024-12-10 09:39:12 +00:00
this.resolveExpr(stmt.kind.expr, syms);
2024-12-06 11:21:57 +00:00
}
return;
}
if (stmt.kind.type === "assign") {
2024-12-10 09:39:12 +00:00
this.resolveExpr(stmt.kind.subject, syms);
this.resolveExpr(stmt.kind.value, syms);
2024-12-06 11:21:57 +00:00
return;
}
if (stmt.kind.type === "expr") {
2024-12-10 09:39:12 +00:00
this.resolveExpr(stmt.kind.expr, syms);
2024-12-06 11:21:57 +00:00
return;
}
2024-11-26 18:37:21 +00:00
}
2024-11-27 14:10:48 +00:00
private resolveLetStmt(stmt: Stmt, syms: Syms) {
2024-12-10 09:39:12 +00:00
if (stmt.kind.type !== "let") {
2024-11-27 14:10:48 +00:00
throw new Error("expected let statement");
2024-12-10 09:39:12 +00:00
}
2024-11-27 14:10:48 +00:00
this.resolveExpr(stmt.kind.value, syms);
const ident = stmt.kind.param.ident;
if (syms.definedLocally(ident)) {
this.reportAlreadyDefined(ident, stmt.pos, syms);
return;
}
syms.define(ident, {
ident,
type: "let",
pos: stmt.kind.param.pos,
stmt,
param: stmt.kind.param,
});
2024-11-26 18:37:21 +00:00
}
private resolveFnStmt(stmt: Stmt, syms: Syms) {
2024-12-10 09:39:12 +00:00
if (stmt.kind.type !== "fn") {
2024-11-27 14:10:48 +00:00
throw new Error("expected fn statement");
2024-12-10 09:39:12 +00:00
}
2024-11-27 14:10:48 +00:00
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,
});
const fnScopeSyms = new Syms(syms);
for (const param of stmt.kind.params) {
if (fnScopeSyms.definedLocally(param.ident)) {
this.reportAlreadyDefined(param.ident, param.pos, syms);
continue;
}
fnScopeSyms.define(param.ident, {
ident: param.ident,
type: "fn_param",
pos: param.pos,
param,
});
}
this.resolveExpr(stmt.kind.body, fnScopeSyms);
2024-11-26 18:37:21 +00:00
}
2024-11-27 14:10:48 +00:00
private reportUseOfUndefined(ident: string, pos: Pos, syms: Syms) {
2024-12-10 09:39:12 +00:00
console.error(
`use of undefined symbol '${ident}' at ${pos.line}${pos.col}`,
);
2024-11-26 18:37:21 +00:00
}
2024-12-10 09:39:12 +00:00
private reportAlreadyDefined(ident: string, pos: Pos, syms: Syms) {
console.error(
`symbol already defined '${ident}', at ${pos.line}${pos.col}`,
);
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-11-26 18:37:21 +00:00
const { line: prevLine, col: prevCol } = prev.sym.pos;
2024-12-10 09:39:12 +00:00
console.error(
`previous definition of '${ident}' at ${prevLine}:${prevCol}`,
);
2024-11-26 18:37:21 +00:00
}
2024-12-10 09:39:12 +00:00
}