diff --git a/src/main/java/org/jcnc/snow/compiler/cli/SnowCompiler.java b/src/main/java/org/jcnc/snow/compiler/cli/SnowCompiler.java index 8c0fb2f..ec49410 100644 --- a/src/main/java/org/jcnc/snow/compiler/cli/SnowCompiler.java +++ b/src/main/java/org/jcnc/snow/compiler/cli/SnowCompiler.java @@ -48,12 +48,12 @@ public class SnowCompiler { ParserContext ctx = new ParserContext(tokens); List 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"); + } + } } } diff --git a/src/main/java/org/jcnc/snow/vm/engine/VirtualMachineEngine.java b/src/main/java/org/jcnc/snow/vm/engine/VirtualMachineEngine.java index 5673fde..4f5e884 100644 --- a/src/main/java/org/jcnc/snow/vm/engine/VirtualMachineEngine.java +++ b/src/main/java/org/jcnc/snow/vm/engine/VirtualMachineEngine.java @@ -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); diff --git a/test b/test index dd6f5ad..363a1a4 100644 --- a/test +++ b/test @@ -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