2025-08-18 22:53:36 -06:00
|
|
|
import { defineCommand } from "@bunli/core";
|
|
|
|
|
import { boringGrammar } from "../parse/grammar";
|
|
|
|
|
import { semantics } from "../parse/semantics";
|
2025-08-19 21:54:06 -06:00
|
|
|
import TraitChecker from "../types/trait_checker";
|
2025-08-25 21:51:50 -06:00
|
|
|
import { TypeAliasResolver } from "../types/type_alias_resolution";
|
|
|
|
|
import { TypeSystem } from "../types/type_system";
|
|
|
|
|
import { TypeChecker } from "../types/type_checker";
|
2025-08-30 22:11:19 -06:00
|
|
|
import { TypeResolver } from "../types/type_resolver";
|
2025-08-31 23:06:26 -06:00
|
|
|
import { TreeWalkInterpreter } from "../interpreter";
|
2025-08-18 22:53:36 -06:00
|
|
|
|
|
|
|
|
export const run = defineCommand({
|
|
|
|
|
name: "run",
|
|
|
|
|
description: "Run a boringlang file",
|
|
|
|
|
|
|
|
|
|
handler: async ({ positional }) => {
|
|
|
|
|
const [path] = positional;
|
|
|
|
|
|
|
|
|
|
if (!path) {
|
|
|
|
|
throw new Error("Usage: run <path>");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const file = Bun.file(path);
|
|
|
|
|
const text = await file.text();
|
|
|
|
|
|
|
|
|
|
const match = boringGrammar.match(text, "Module");
|
|
|
|
|
if (match.succeeded()) {
|
|
|
|
|
const adapter = semantics(match);
|
|
|
|
|
const ast = adapter.toAST();
|
2025-10-09 17:40:54 -06:00
|
|
|
// console.log(JSON.stringify(ast, null, 2));
|
2025-08-19 21:54:06 -06:00
|
|
|
new TraitChecker().withModule(ast);
|
2025-08-25 21:51:50 -06:00
|
|
|
const aliasResolvedAst = new TypeAliasResolver().withModule(ast);
|
|
|
|
|
const typeSystem = new TypeSystem();
|
|
|
|
|
const typeChecker = new TypeChecker();
|
2025-08-30 22:11:19 -06:00
|
|
|
const typeResolver = new TypeResolver();
|
2025-10-09 17:40:54 -06:00
|
|
|
|
2025-08-25 21:51:50 -06:00
|
|
|
typeChecker.withModule(aliasResolvedAst, typeSystem);
|
2025-10-09 17:40:54 -06:00
|
|
|
try {
|
|
|
|
|
typeSystem.solve();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e);
|
|
|
|
|
console.log(JSON.stringify(typeSystem.result, null, 2));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-30 22:11:19 -06:00
|
|
|
const typeResolvedAst = typeResolver.withModule(aliasResolvedAst, typeSystem);
|
2025-10-09 17:40:54 -06:00
|
|
|
// console.log(JSON.stringify(typeResolvedAst, null, 2));
|
2025-08-31 23:06:26 -06:00
|
|
|
const interpreter = new TreeWalkInterpreter();
|
|
|
|
|
const result = interpreter.withModule(typeResolvedAst);
|
|
|
|
|
console.log(JSON.stringify(result, null, 2));
|
2025-08-19 21:54:06 -06:00
|
|
|
|
2025-08-25 21:51:50 -06:00
|
|
|
// console.log(JSON.stringify(aliasResolvedAst, null, 2));
|
2025-08-18 22:53:36 -06:00
|
|
|
} else {
|
|
|
|
|
console.log(match.message);
|
|
|
|
|
// console.log(boringGrammar.trace(text, "Module").toString());
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|