slige/compiler/compiler.ts

69 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-12-14 23:07:36 +00:00
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";
2024-12-26 00:51:05 +00:00
import { Monomorphizer } from "./mono.ts";
2024-12-26 01:38:32 +00:00
import { FnNamesMap, Lowerer } from "./lowerer.ts";
2024-12-14 23:07:36 +00:00
import { Parser } from "./parser.ts";
import { Resolver } from "./resolver.ts";
import * as path from "jsr:@std/path";
2024-12-14 23:07:36 +00:00
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 stdlib = await Deno.readTextFile(
path.join(
path.dirname(path.fromFileUrl(Deno.mainModule)),
"../stdlib.slg",
),
);
const totalText = text + stdlib;
const lexer = new Lexer(totalText, this.reporter);
2024-12-14 23:07:36 +00:00
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);
}
2024-12-26 00:51:05 +00:00
const { monoFns, callMap } = new Monomorphizer(ast).monomorphize();
2024-12-26 01:38:32 +00:00
const lowerer = new Lowerer(monoFns, callMap, lexer.currentPos());
2024-12-26 00:51:05 +00:00
const { program, fnNames } = lowerer.lower();
//lowerer.printProgram();
2024-12-14 23:07:36 +00:00
return { program, fnNames };
}
}