From 9840269ab7e69d3bd1bfdf870c95898807848e3f Mon Sep 17 00:00:00 2001 From: Theis Pieter Hollebeek Date: Thu, 12 Dec 2024 10:47:25 +0100 Subject: [PATCH] create temputils to satisfy compileWithDebug requirement from web --- compiler/mod.ts | 2 ++ compiler/temputils.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 compiler/temputils.ts diff --git a/compiler/mod.ts b/compiler/mod.ts index b6af563..5c1fc23 100644 --- a/compiler/mod.ts +++ b/compiler/mod.ts @@ -3,3 +3,5 @@ export * from "./ast.ts"; export * from "./arch.ts"; export * from "./lexer.ts"; export * from "./token.ts"; + +export * from "./temputils.ts"; diff --git a/compiler/temputils.ts b/compiler/temputils.ts new file mode 100644 index 0000000..56f361c --- /dev/null +++ b/compiler/temputils.ts @@ -0,0 +1,31 @@ +import { Checker } from "./checker.ts"; +import { Reporter } from "./info.ts"; +import { Lexer } from "./lexer.ts"; +import { Lowerer } from "./lowerer.ts"; +import { Parser } from "./parser.ts"; +import { Resolver } from "./resolver.ts"; + +/// TODO: find a better place for this function +export async function compileWithDebug(path: string): Promise { + const text = await Deno.readTextFile(path); + + const reporter = new Reporter(); + + const lexer = new Lexer(text, reporter); + + const parser = new Parser(lexer, reporter); + const ast = parser.parseStmts(); + + new Resolver(reporter).resolve(ast); + new Checker(reporter).check(ast); + + if (reporter.errorOccured()) { + console.error("Errors occurred, stopping compilation."); + } + + const lowerer = new Lowerer(); + lowerer.lower(ast); + lowerer.printProgram(); + const program = lowerer.finish(); + return program; +}