This commit is contained in:
Luke 2025-04-29 11:12:30 +08:00
parent e994cb39b4
commit 2957541a0c
3 changed files with 33 additions and 32 deletions

View File

@ -48,12 +48,12 @@ public class SnowCompiler {
ParserContext ctx = new ParserContext(tokens);
List<Node> ast = new ParserEngine(ctx).parse();
System.out.println(source);
TokenPrinter.print(tokens); // 打印 Token 列表
// System.out.println(source);
// TokenPrinter.print(tokens); // 打印 Token 列表
ASTPrinter.print(ast); // 打印 AST
ASTPrinter.printJson(ast); // 打印JSON AST
// ASTPrinter.printJson(ast); // 打印JSON AST
/* 3. 语义分析 */
SemanticAnalyzerRunner.runSemanticAnalysis(ast, true);
SemanticAnalyzerRunner.runSemanticAnalysis(ast, false);
/* 4. AST → IRProgram */
IRProgram program = new BasicIRBuilder().buildProgram(ast);
@ -61,27 +61,27 @@ public class SnowCompiler {
System.out.println(program);
/* 5. 后端:寄存器分配 & 代码生成 + VM 执行 */
// for (IRFunction fn : program.functions()) {
// var alloc = new RegisterAllocator();
// var slotM = alloc.allocate(fn);
//
// var gen = new VMCodeGenerator(slotM);
// var code = gen.generate(fn);
//
// System.out.println("== VM code for " + fn.name() + " ==");
// code.forEach(System.out::println);
//
//
// /* 只执行 main 函数 */
// if ("main".equals(fn.name())) {
// VirtualMachineEngine vm = new VirtualMachineEngine(VMMode.RUN);
//
// vm.execute(code); // 运行指令
// vm.printStack(); // 打印 Operand-/Call-Stack
// vm.printLocalVariables(); // 打印局部变量槽
//
// System.out.println("Process has ended");
// }
// }
for (IRFunction fn : program.functions()) {
var alloc = new RegisterAllocator();
var slotM = alloc.allocate(fn);
var gen = new VMCodeGenerator(slotM);
var code = gen.generate(fn);
System.out.println("== VM code for " + fn.name() + " ==");
code.forEach(System.out::println);
/* 只执行 main 函数 */
if ("main".equals(fn.name())) {
VirtualMachineEngine vm = new VirtualMachineEngine(VMMode.RUN);
vm.execute(code); // 运行指令
vm.printStack(); // 打印 Operand-/Call-Stack
vm.printLocalVariables(); // 打印局部变量槽
System.out.println("Process has ended");
}
}
}
}

View File

@ -76,7 +76,7 @@ public class VirtualMachineEngine {
throw new IllegalArgumentException("The command list cannot be empty or null.");
}
ensureRootFrame(); // 新增
ensureRootFrame();
if (command == null || command.isEmpty()) {
throw new IllegalArgumentException("The command list cannot be empty or null.");
@ -111,10 +111,10 @@ public class VirtualMachineEngine {
LocalVariableStore lvs = this.localVariableStore; // 仍复用现有对象
/* ---------- 预分配 32 个空位 ---------- */
for (int i = 0; i < 32; i++) {
lvs.setVariable(i, 0); // setVariable() 会自动扩容先填 0 占位
}
/* ---------- 预分配 1 个空位 ---------- */
// for (int i = 0; i < 0; i++) {
// lvs.setVariable(i, 0); // setVariable() 会自动扩容先填 0 占位
// }
MethodContext mc = new MethodContext("root", null);
StackFrame sf = new StackFrame(0, lvs, mc);

3
test
View File

@ -8,7 +8,8 @@ module: CommonTasks
body:
num1 = 10
num2 = 20
return (num1 + num2) * 2
declare result:int = num1+num2
return 0
end body
end function
end module