slige/compiler/compiler.ts

139 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-12-30 21:12:28 +01:00
import { AstCreator, Item, Mod, Stmt } from "./ast.ts";
2024-12-15 00:07:36 +01:00
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 01:51:05 +01:00
import { Monomorphizer } from "./mono.ts";
2024-12-26 02:38:32 +01:00
import { FnNamesMap, Lowerer } from "./lowerer.ts";
2024-12-15 00:07:36 +01:00
import { Parser } from "./parser.ts";
import { Resolver } from "./resolver.ts";
2024-12-30 21:12:28 +01:00
import { AstVisitor, visitItems, VisitRes } from "./ast_visitor.ts";
import { Pos } from "./token.ts";
2024-12-15 00:07:36 +01:00
import * as path from "jsr:@std/path";
2024-12-15 00:07:36 +01:00
export type CompileResult = {
program: number[];
fnNames: FnNamesMap;
};
export class Compiler {
private astCreator = new AstCreator();
2024-12-29 05:39:22 +01:00
private reporter;
2024-12-15 00:07:36 +01:00
2024-12-29 05:39:22 +01:00
public constructor(private startFilePath: string) {
this.reporter = new Reporter(this.startFilePath);
}
2024-12-15 00:07:36 +01:00
public async compile(): Promise<CompileResult> {
2024-12-29 05:39:22 +01:00
const mod = new ModTree(
this.startFilePath,
this.astCreator,
this.reporter,
).resolve();
2024-12-15 00:07:36 +01:00
2024-12-30 21:12:28 +01:00
new SpecialLoopDesugarer(this.astCreator).desugar(mod.items);
2024-12-15 00:07:36 +01:00
2024-12-30 21:12:28 +01:00
new Resolver(this.reporter).resolve(mod.items);
2024-12-15 00:07:36 +01:00
2024-12-30 21:12:28 +01:00
new CompoundAssignDesugarer(this.astCreator).desugar(mod.items);
2024-12-15 00:07:36 +01:00
2024-12-30 21:12:28 +01:00
new Checker(this.reporter).check(mod.items);
2024-12-15 00:07:36 +01:00
if (this.reporter.errorOccured()) {
console.error("Errors occurred, stopping compilation.");
Deno.exit(1);
}
2024-12-30 21:12:28 +01:00
const { monoFns, callMap } = new Monomorphizer(mod.items)
.monomorphize();
2024-12-29 05:39:22 +01:00
const lastPos = await lastPosInTextFile(this.startFilePath);
2024-12-26 01:51:05 +01:00
2024-12-29 05:39:22 +01:00
const lowerer = new Lowerer(monoFns, callMap, lastPos);
2024-12-26 01:51:05 +01:00
const { program, fnNames } = lowerer.lower();
//lowerer.printProgram();
2024-12-15 00:07:36 +01:00
return { program, fnNames };
}
}
2024-12-29 05:39:22 +01:00
export class ModTree implements AstVisitor<[string]> {
constructor(
private entryFilePath: string,
private astCreator: AstCreator,
private reporter: Reporter,
) {}
public resolve(): Mod {
const entryAst = this.parseFile(this.entryFilePath);
2024-12-30 21:12:28 +01:00
visitItems(entryAst, this, this.entryFilePath);
2024-12-29 05:39:22 +01:00
2024-12-30 21:12:28 +01:00
return { filePath: this.entryFilePath, items: entryAst };
2024-12-29 05:39:22 +01:00
}
2024-12-30 21:12:28 +01:00
private parseFile(filePath: string): Item[] {
2024-12-29 05:39:22 +01:00
const text = Deno.readTextFileSync(filePath);
const lexer = new Lexer(text, this.reporter);
const parser = new Parser(lexer, this.astCreator, this.reporter);
const ast = parser.parse();
return ast;
}
visitModBlockStmt(stmt: Stmt, filePath: string): VisitRes {
if (stmt.kind.type !== "mod_block") {
throw new Error();
}
2024-12-30 21:12:28 +01:00
const { ident, items } = stmt.kind;
2024-12-29 05:39:22 +01:00
stmt.kind = {
type: "mod",
ident,
2024-12-30 21:12:28 +01:00
mod: { filePath, items },
2024-12-29 05:39:22 +01:00
};
2024-12-30 21:12:28 +01:00
visitItems(items, this, filePath);
2024-12-29 05:39:22 +01:00
return "stop";
}
visitModFileStmt(stmt: Stmt, filePath: string): VisitRes {
if (stmt.kind.type !== "mod_file") {
throw new Error();
}
const { ident, filePath: modFilePath } = stmt.kind;
const ast = this.parseFile(
path.join(path.dirname(filePath), modFilePath),
);
stmt.kind = {
type: "mod",
ident,
2024-12-30 21:12:28 +01:00
mod: { filePath, items: ast },
2024-12-29 05:39:22 +01:00
};
2024-12-30 21:12:28 +01:00
visitItems(ast, this, filePath);
2024-12-29 05:39:22 +01:00
return "stop";
}
}
async function lastPosInTextFile(filePath: string): Promise<Pos> {
const text = await Deno.readTextFile(filePath);
let index = 0;
let line = 1;
let col = 1;
while (index < text.length) {
if (text[index] == "\n") {
line += 1;
col = 1;
} else {
col += 1;
}
index += 1;
}
return { index, line, col };
}