mirror of
https://git.sfja.dk/Mikkel/slige.git
synced 2025-01-18 22:46:30 +00:00
242 lines
7.1 KiB
TypeScript
242 lines
7.1 KiB
TypeScript
import { Builtins } from "./arch.ts";
|
|
import { Expr, Stmt } from "./ast.ts";
|
|
import { printStackTrace, Reporter } from "./info.ts";
|
|
import {
|
|
FnSyms,
|
|
GlobalSyms,
|
|
LeafSyms,
|
|
StaticSyms,
|
|
Syms,
|
|
} from "./resolver_syms.ts";
|
|
import { Pos } from "./token.ts";
|
|
|
|
export class Resolver {
|
|
private root = new GlobalSyms();
|
|
|
|
public constructor(private reporter: Reporter) {
|
|
this.root.define("print", {
|
|
type: "builtin",
|
|
ident: "print",
|
|
builtinId: Builtins.Print,
|
|
});
|
|
}
|
|
|
|
public resolve(stmts: Stmt[]) {
|
|
const scopeSyms = new StaticSyms(this.root);
|
|
this.scoutFnStmts(stmts, scopeSyms);
|
|
for (const stmt of stmts) {
|
|
this.resolveStmt(stmt, scopeSyms);
|
|
}
|
|
}
|
|
|
|
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,
|
|
});
|
|
}
|
|
}
|
|
|
|
private resolveExpr(expr: Expr, syms: Syms) {
|
|
if (expr.kind.type === "error") {
|
|
return;
|
|
}
|
|
if (expr.kind.type === "ident") {
|
|
this.resolveIdentExpr(expr, syms);
|
|
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 LeafSyms(syms);
|
|
this.scoutFnStmts(expr.kind.stmts, childSyms);
|
|
for (const stmt of expr.kind.stmts) {
|
|
this.resolveStmt(stmt, childSyms);
|
|
}
|
|
if (expr.kind.expr) {
|
|
this.resolveExpr(expr.kind.expr, childSyms);
|
|
}
|
|
return;
|
|
}
|
|
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) {
|
|
if (expr.kind.type !== "ident") {
|
|
throw new Error("expected ident");
|
|
}
|
|
const ident = expr.kind;
|
|
const symResult = syms.get(ident.value);
|
|
if (!symResult.ok) {
|
|
this.reportUseOfUndefined(ident.value, expr.pos, syms);
|
|
return;
|
|
}
|
|
const sym = symResult.sym;
|
|
expr.kind = {
|
|
type: "sym",
|
|
ident: ident.value,
|
|
sym,
|
|
};
|
|
}
|
|
|
|
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") {
|
|
if (stmt.kind.expr) {
|
|
this.resolveExpr(stmt.kind.expr, syms);
|
|
}
|
|
return;
|
|
}
|
|
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) {
|
|
if (stmt.kind.type !== "let") {
|
|
throw new Error("expected let statement");
|
|
}
|
|
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,
|
|
});
|
|
}
|
|
|
|
private resolveFnStmt(stmt: Stmt, syms: Syms) {
|
|
if (stmt.kind.type !== "fn") {
|
|
throw new Error("expected fn statement");
|
|
}
|
|
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;
|
|
}
|
|
fnScopeSyms.define(param.ident, {
|
|
ident: param.ident,
|
|
type: "fn_param",
|
|
pos: param.pos,
|
|
param,
|
|
});
|
|
}
|
|
this.resolveExpr(stmt.kind.body, fnScopeSyms);
|
|
}
|
|
|
|
private reportUseOfUndefined(ident: string, pos: Pos, _syms: Syms) {
|
|
this.reporter.reportError({
|
|
reporter: "Resolver",
|
|
msg: `use of undefined symbol '${ident}'`,
|
|
pos,
|
|
});
|
|
printStackTrace();
|
|
}
|
|
|
|
private reportAlreadyDefined(ident: string, pos: Pos, syms: Syms) {
|
|
this.reporter.reportError({
|
|
reporter: "Resolver",
|
|
msg: `symbol already defined '${ident}'`,
|
|
pos,
|
|
});
|
|
const prev = syms.get(ident);
|
|
if (!prev.ok) {
|
|
throw new Error("expected to be defined");
|
|
}
|
|
if (!prev.sym.pos) {
|
|
return;
|
|
}
|
|
this.reporter.addNote({
|
|
reporter: "Resolver",
|
|
msg: `previous definition of '${ident}'`,
|
|
pos: prev.sym.pos,
|
|
});
|
|
printStackTrace();
|
|
}
|
|
}
|