nandgame-compiler/checker.ts
SimonFJ20 b2b1533ee6 init
2023-04-27 02:32:06 +02:00

56 lines
1.6 KiB
TypeScript

import { CheckedExpr, CheckedType } from "./checked.ts";
import { ParsedExpr } from "./parsed.ts";
import { CompileError } from "./token.ts";
export type SymbolValue = {
id: number;
subject: string;
valueType: CheckedType;
};
export class SymbolTable {
private idCounter = 0;
private symbols: { [key: string]: SymbolValue } = {};
public define(subject: string, valueType: CheckedType) {
if (subject in this.symbols) {
throw new Error("redefinition not implemented");
}
const id = this.idCounter;
this.idCounter++;
this.symbols[subject] = { id, subject, valueType };
}
}
export class Checker {
private symbolTable = new SymbolTable();
public errors: CompileError[] = [];
public check(statements: ParsedExpr[]): CheckedExpr[] {
return statements as CheckedExpr[];
}
private searchTopLevelStatements(statements: ParsedExpr[]) {
for (const statement of statements) {
switch (statement.exprType) {
case "fn":
this.searchTopLevelFn(statement);
break;
case "let":
break;
default:
this.errors.push({
pos: statement.pos,
message:
`statement '${statement.exprType}' not supported in top level`,
});
break;
}
}
}
private searchTopLevelFn(statement: ParsedExpr & { exprType: "fn" }) {
}
}