mirror of
https://git.sfja.dk/Mikkel/slige.git
synced 2025-01-18 13:06:30 +00:00
59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
import { AstCreator } from "./ast.ts";
|
|
import { Checker } from "./checker.ts";
|
|
import { CompoundAssignDesugarer } from "./desugar/compound_assign.ts";
|
|
import { SpecialLoopDesugarer } from "./desugar/special_loop.ts";
|
|
import { Reporter } from "./info.ts";
|
|
import { Lexer } from "./lexer.ts";
|
|
import { FnNamesMap, Lowerer } from "./lowerer.ts";
|
|
import { monomorphizeFunctionGraphs } from "./mfg.ts";
|
|
import { Parser } from "./parser.ts";
|
|
import { Resolver } from "./resolver.ts";
|
|
|
|
export type CompiledFile = {
|
|
filepath: string;
|
|
program: number[];
|
|
};
|
|
|
|
export type CompileResult = {
|
|
program: number[];
|
|
fnNames: FnNamesMap;
|
|
};
|
|
|
|
export class Compiler {
|
|
private astCreator = new AstCreator();
|
|
private reporter = new Reporter();
|
|
|
|
public constructor(private startFilePath: string) {}
|
|
|
|
public async compile(): Promise<CompileResult> {
|
|
const text = await Deno.readTextFile(this.startFilePath);
|
|
|
|
const lexer = new Lexer(text, this.reporter);
|
|
|
|
const parser = new Parser(lexer, this.astCreator, this.reporter);
|
|
const ast = parser.parse();
|
|
|
|
new SpecialLoopDesugarer(this.astCreator).desugar(ast);
|
|
|
|
new Resolver(this.reporter).resolve(ast);
|
|
|
|
new CompoundAssignDesugarer(this.astCreator).desugar(ast);
|
|
|
|
new Checker(this.reporter).check(ast);
|
|
|
|
if (this.reporter.errorOccured()) {
|
|
console.error("Errors occurred, stopping compilation.");
|
|
Deno.exit(1);
|
|
}
|
|
|
|
const monomorphizedFns = monomorphizeFunctionGraphs(ast);
|
|
|
|
const lowerer = new Lowerer(lexer.currentPos());
|
|
lowerer.lower(monomorphizedFns);
|
|
lowerer.printProgram();
|
|
const { program, fnNames } = lowerer.finish();
|
|
|
|
return { program, fnNames };
|
|
}
|
|
}
|