56 lines
1.6 KiB
TypeScript
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" }) {
|
||
|
}
|
||
|
}
|
||
|
|