diff --git a/.gitee/ISSUE_TEMPLATE/bug.yml b/.gitee/ISSUE_TEMPLATE/bug.yml
index 8f2368e..f7f9965 100644
--- a/.gitee/ISSUE_TEMPLATE/bug.yml
+++ b/.gitee/ISSUE_TEMPLATE/bug.yml
@@ -69,7 +69,7 @@ body:
attributes:
label: 软件版本/分支
options:
- - v0.5.0
+ - v0.6.0
- main
- dev
- 其他
diff --git a/.run/Bug2.run.xml b/.run/Bug2.run.xml
new file mode 100644
index 0000000..e77abfc
--- /dev/null
+++ b/.run/Bug2.run.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/Bug3.run.xml b/.run/Bug3.run.xml
new file mode 100644
index 0000000..a371f33
--- /dev/null
+++ b/.run/Bug3.run.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/Bug4.run.xml b/.run/Bug4.run.xml
new file mode 100644
index 0000000..d10e3f5
--- /dev/null
+++ b/.run/Bug4.run.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/Bug5.run.xml b/.run/Bug5.run.xml
new file mode 100644
index 0000000..054f0b9
--- /dev/null
+++ b/.run/Bug5.run.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/Demo1.run.xml b/.run/Demo1.run.xml
index aa50447..98e3db5 100644
--- a/.run/Demo1.run.xml
+++ b/.run/Demo1.run.xml
@@ -3,7 +3,7 @@
-
+
diff --git a/.run/Demo10.run.xml b/.run/Demo10.run.xml
index 74bb0ba..29e014f 100644
--- a/.run/Demo10.run.xml
+++ b/.run/Demo10.run.xml
@@ -3,7 +3,7 @@
-
+
diff --git a/.run/Demo11.run.xml b/.run/Demo11.run.xml
index d61b202..6888bed 100644
--- a/.run/Demo11.run.xml
+++ b/.run/Demo11.run.xml
@@ -3,7 +3,7 @@
-
+
diff --git a/.run/Demo12.run.xml b/.run/Demo12.run.xml
index bce1584..844a988 100644
--- a/.run/Demo12.run.xml
+++ b/.run/Demo12.run.xml
@@ -3,7 +3,7 @@
-
+
diff --git a/.run/Demo13.run.xml b/.run/Demo13.run.xml
index c722dcf..e6dff33 100644
--- a/.run/Demo13.run.xml
+++ b/.run/Demo13.run.xml
@@ -2,7 +2,7 @@
-
+
diff --git a/.run/Demo14.run.xml b/.run/Demo14.run.xml
index a3c55c6..8b0f241 100644
--- a/.run/Demo14.run.xml
+++ b/.run/Demo14.run.xml
@@ -2,7 +2,7 @@
-
+
diff --git a/.run/Demo15.run.xml b/.run/Demo15.run.xml
new file mode 100644
index 0000000..1767560
--- /dev/null
+++ b/.run/Demo15.run.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/Demo16.run.xml b/.run/Demo16.run.xml
new file mode 100644
index 0000000..f217625
--- /dev/null
+++ b/.run/Demo16.run.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/Demo17.run.xml b/.run/Demo17.run.xml
new file mode 100644
index 0000000..c608651
--- /dev/null
+++ b/.run/Demo17.run.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/Demo18.run.xml b/.run/Demo18.run.xml
new file mode 100644
index 0000000..c9f4bb6
--- /dev/null
+++ b/.run/Demo18.run.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/Demo2.run.xml b/.run/Demo2.run.xml
index 9a1b95d..4fb0f17 100644
--- a/.run/Demo2.run.xml
+++ b/.run/Demo2.run.xml
@@ -3,7 +3,7 @@
-
+
diff --git a/.run/Demo3.run.xml b/.run/Demo3.run.xml
index 0b86b8b..0bf6172 100644
--- a/.run/Demo3.run.xml
+++ b/.run/Demo3.run.xml
@@ -3,7 +3,7 @@
-
+
diff --git a/.run/Demo4.run.xml b/.run/Demo4.run.xml
index 8c44d89..d777224 100644
--- a/.run/Demo4.run.xml
+++ b/.run/Demo4.run.xml
@@ -3,7 +3,7 @@
-
+
diff --git a/.run/Demo5.run.xml b/.run/Demo5.run.xml
index 90ef70e..3fa8a56 100644
--- a/.run/Demo5.run.xml
+++ b/.run/Demo5.run.xml
@@ -3,7 +3,7 @@
-
+
diff --git a/.run/Demo6.run.xml b/.run/Demo6.run.xml
index e58b6dc..a5c3432 100644
--- a/.run/Demo6.run.xml
+++ b/.run/Demo6.run.xml
@@ -3,7 +3,7 @@
-
+
diff --git a/.run/Demo7.run.xml b/.run/Demo7.run.xml
index aeab6e2..bef0dce 100644
--- a/.run/Demo7.run.xml
+++ b/.run/Demo7.run.xml
@@ -3,7 +3,7 @@
-
+
diff --git a/.run/Demo8.run.xml b/.run/Demo8.run.xml
index 6f6d2d3..c14026b 100644
--- a/.run/Demo8.run.xml
+++ b/.run/Demo8.run.xml
@@ -3,7 +3,7 @@
-
+
diff --git a/.run/Demo9.run.xml b/.run/Demo9.run.xml
index 23d6f53..c8036a0 100644
--- a/.run/Demo9.run.xml
+++ b/.run/Demo9.run.xml
@@ -3,7 +3,7 @@
-
+
diff --git a/.run/测试.run.xml b/.run/测试.run.xml
index c37e7f3..097c514 100644
--- a/.run/测试.run.xml
+++ b/.run/测试.run.xml
@@ -5,6 +5,10 @@
+
+
+
+
diff --git a/README.md b/README.md
index 1e47407..d2fd83b 100644
--- a/README.md
+++ b/README.md
@@ -11,8 +11,8 @@
-
-
+
+
diff --git a/playground/BugFarm/Bug2/Main.snow b/playground/BugFarm/Bug2/Main.snow
new file mode 100644
index 0000000..674b74b
--- /dev/null
+++ b/playground/BugFarm/Bug2/Main.snow
@@ -0,0 +1,33 @@
+module: Main
+ import: os
+ function: main
+ return_type: void
+ body:
+ loop:
+ init:
+ declare outer_i: int = 1
+ cond:
+ outer_i <= 10
+ step:
+ outer_i = outer_i + 1
+ body:
+ print(outer_i)
+
+ loop:
+ init:
+ // 注意这一行使用了外层循环的变量 outer_i
+ declare inter_j: int = outer_i
+ cond:
+ inter_j <= 10
+ step:
+ inter_j = inter_j + 1
+ body:
+
+ end body
+ end loop
+
+ end body
+ end loop
+ end body
+ end function
+end module
\ No newline at end of file
diff --git a/playground/BugFarm/Bug2/OS.snow b/playground/BugFarm/Bug2/OS.snow
new file mode 100644
index 0000000..6026d43
--- /dev/null
+++ b/playground/BugFarm/Bug2/OS.snow
@@ -0,0 +1,11 @@
+module: os
+ import: os
+ function: print
+ parameter:
+ declare i1: int
+ return_type: void
+ body:
+ syscall("PRINT",i1)
+ end body
+ end function
+end module
\ No newline at end of file
diff --git a/playground/BugFarm/Bug3/Main.snow b/playground/BugFarm/Bug3/Main.snow
new file mode 100644
index 0000000..7918328
--- /dev/null
+++ b/playground/BugFarm/Bug3/Main.snow
@@ -0,0 +1,14 @@
+module: Main
+ import: os
+ function: main
+ return_type: void
+ body:
+ // 合法
+ declare b1: byte = 127b
+ declare s1: short = 32767s
+ declare i1: int = 2147483647
+ declare l1: long = 9223372036854775807L
+
+ end body
+ end function
+end module
diff --git a/playground/BugFarm/Bug3/OS.snow b/playground/BugFarm/Bug3/OS.snow
new file mode 100644
index 0000000..6026d43
--- /dev/null
+++ b/playground/BugFarm/Bug3/OS.snow
@@ -0,0 +1,11 @@
+module: os
+ import: os
+ function: print
+ parameter:
+ declare i1: int
+ return_type: void
+ body:
+ syscall("PRINT",i1)
+ end body
+ end function
+end module
\ No newline at end of file
diff --git a/playground/BugFarm/Bug4/Main.snow b/playground/BugFarm/Bug4/Main.snow
new file mode 100644
index 0000000..0efa4b1
--- /dev/null
+++ b/playground/BugFarm/Bug4/Main.snow
@@ -0,0 +1,9 @@
+module: Main
+ import: ModuleB
+ function: main
+ return_type: int
+ body:
+ return ModuleB.fun()
+ end body
+ end function
+end module
diff --git a/playground/BugFarm/Bug4/a.snow b/playground/BugFarm/Bug4/a.snow
new file mode 100644
index 0000000..d717032
--- /dev/null
+++ b/playground/BugFarm/Bug4/a.snow
@@ -0,0 +1,8 @@
+module: ModuleA
+ function: fun
+ return_type: int
+ body:
+ return 123
+ end body
+ end function
+end module
\ No newline at end of file
diff --git a/playground/BugFarm/Bug4/b.snow b/playground/BugFarm/Bug4/b.snow
new file mode 100644
index 0000000..4db51a0
--- /dev/null
+++ b/playground/BugFarm/Bug4/b.snow
@@ -0,0 +1,9 @@
+module: ModuleB
+ import: ModuleA
+ function: fun
+ return_type: int
+ body:
+ return ModuleA.fun() + ModuleA.fun()
+ end body
+ end function
+end module
\ No newline at end of file
diff --git a/playground/BugFarm/Bug5/Main.snow b/playground/BugFarm/Bug5/Main.snow
new file mode 100644
index 0000000..89c6c84
--- /dev/null
+++ b/playground/BugFarm/Bug5/Main.snow
@@ -0,0 +1,16 @@
+module: Main
+ import: os
+ globals:
+ declare sum: int = 0
+ function: main
+ parameter:
+ return_type: int
+ body:
+ sum = 20
+
+ os.print(sum)
+
+ return 0
+ end body
+ end function
+end module
\ No newline at end of file
diff --git a/playground/BugFarm/Bug5/OS.snow b/playground/BugFarm/Bug5/OS.snow
new file mode 100644
index 0000000..6026d43
--- /dev/null
+++ b/playground/BugFarm/Bug5/OS.snow
@@ -0,0 +1,11 @@
+module: os
+ import: os
+ function: print
+ parameter:
+ declare i1: int
+ return_type: void
+ body:
+ syscall("PRINT",i1)
+ end body
+ end function
+end module
\ No newline at end of file
diff --git a/playground/Demo/Demo14/Main.snow b/playground/Demo/Demo14/Main.snow
index 7ef2b05..7bb005a 100644
--- a/playground/Demo/Demo14/Main.snow
+++ b/playground/Demo/Demo14/Main.snow
@@ -3,7 +3,7 @@ module: Main
function: main
return_type: void
body:
- print(222)
+ os.print(222)
end body
end function
end module
\ No newline at end of file
diff --git a/playground/Demo/Demo15/Main.snow b/playground/Demo/Demo15/Main.snow
new file mode 100644
index 0000000..65533fe
--- /dev/null
+++ b/playground/Demo/Demo15/Main.snow
@@ -0,0 +1,20 @@
+module: Main
+ import: os
+ globals:
+ declare num2:int=10
+ function: main
+ return_type: void
+ body:
+ declare num1:int=11
+ os.print(num1+num2+abc())
+ end body
+ end function
+
+ function: abc
+ return_type: int
+ body:
+ return 1
+ end body
+ end function
+
+end module
\ No newline at end of file
diff --git a/playground/Demo/Demo15/OS.snow b/playground/Demo/Demo15/OS.snow
new file mode 100644
index 0000000..6026d43
--- /dev/null
+++ b/playground/Demo/Demo15/OS.snow
@@ -0,0 +1,11 @@
+module: os
+ import: os
+ function: print
+ parameter:
+ declare i1: int
+ return_type: void
+ body:
+ syscall("PRINT",i1)
+ end body
+ end function
+end module
\ No newline at end of file
diff --git a/playground/Demo/Demo16/Main.snow b/playground/Demo/Demo16/Main.snow
new file mode 100644
index 0000000..4aa7b8d
--- /dev/null
+++ b/playground/Demo/Demo16/Main.snow
@@ -0,0 +1,22 @@
+module: Main
+ import: os
+ function: main
+ return_type: int
+ body:
+ loop:
+ init:
+ declare i:int = 1
+ cond:
+ i <= 10
+ step:
+ i = i + 1
+ body:
+ if i % 2 == 0 then
+ os.print(i)
+ end if
+ end body
+ end loop
+ return 0
+ end body
+ end function
+end module
diff --git a/playground/Demo/Demo16/OS.snow b/playground/Demo/Demo16/OS.snow
new file mode 100644
index 0000000..6026d43
--- /dev/null
+++ b/playground/Demo/Demo16/OS.snow
@@ -0,0 +1,11 @@
+module: os
+ import: os
+ function: print
+ parameter:
+ declare i1: int
+ return_type: void
+ body:
+ syscall("PRINT",i1)
+ end body
+ end function
+end module
\ No newline at end of file
diff --git a/playground/Demo/Demo17/Main.snow b/playground/Demo/Demo17/Main.snow
new file mode 100644
index 0000000..8c15d75
--- /dev/null
+++ b/playground/Demo/Demo17/Main.snow
@@ -0,0 +1,23 @@
+module: Main
+ import: os
+ function: main
+ return_type: int
+ body:
+ loop:
+ init:
+ declare i:int = 1
+ cond:
+ i <= 10
+ step:
+ i = i + 1
+ body:
+ if i % 2 == 0 then
+ os.print(i)
+ break
+ end if
+ end body
+ end loop
+ return 0
+ end body
+ end function
+end module
diff --git a/playground/Demo/Demo17/OS.snow b/playground/Demo/Demo17/OS.snow
new file mode 100644
index 0000000..6026d43
--- /dev/null
+++ b/playground/Demo/Demo17/OS.snow
@@ -0,0 +1,11 @@
+module: os
+ import: os
+ function: print
+ parameter:
+ declare i1: int
+ return_type: void
+ body:
+ syscall("PRINT",i1)
+ end body
+ end function
+end module
\ No newline at end of file
diff --git a/playground/Demo/Demo18/Main.snow b/playground/Demo/Demo18/Main.snow
new file mode 100644
index 0000000..e41610c
--- /dev/null
+++ b/playground/Demo/Demo18/Main.snow
@@ -0,0 +1,43 @@
+module: Main
+ import: os
+ function: main
+ return_type: int
+ body:
+ loop:
+ init:
+ declare i:int = 1
+ cond:
+ i <= 5
+ step:
+ i = i + 1
+ body:
+ if i % 2 == 0 then
+ continue
+ end if
+ if i > 3 then
+ break
+ end if
+ loop:
+ init:
+ declare j:int = 1
+ cond:
+ j <= 5
+ step:
+ j = j + 1
+ body:
+ if j == 4 then
+ break
+ end if
+ if j % 2 == 0 then
+ continue
+ end if
+ print(i)
+ print(j)
+ end body
+ end loop
+ end body
+ end loop
+ return 0
+ end body
+ end function
+end module
diff --git a/playground/Demo/Demo18/OS.snow b/playground/Demo/Demo18/OS.snow
new file mode 100644
index 0000000..6026d43
--- /dev/null
+++ b/playground/Demo/Demo18/OS.snow
@@ -0,0 +1,11 @@
+module: os
+ import: os
+ function: print
+ parameter:
+ declare i1: int
+ return_type: void
+ body:
+ syscall("PRINT",i1)
+ end body
+ end function
+end module
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index ec86d77..8846ae1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,7 +7,7 @@
org.jcnc.snowSnow
- 0.5.0
+ 0.6.0UTF-8
diff --git a/src/main/java/org/jcnc/snow/cli/commands/CompileCommand.java b/src/main/java/org/jcnc/snow/cli/commands/CompileCommand.java
index 361413b..6955a16 100644
--- a/src/main/java/org/jcnc/snow/cli/commands/CompileCommand.java
+++ b/src/main/java/org/jcnc/snow/cli/commands/CompileCommand.java
@@ -60,9 +60,9 @@ public final class CompileCommand implements CLICommand {
List argList = new ArrayList<>();
- // 保留用户在 cloud 模式下传入的 “run” / “-debug” 标志
+ // 保留用户在 cloud 模式下传入的 “run” / “--debug” 标志
for (String a : args) {
- if ("run".equals(a) || "-debug".equals(a)) {
+ if ("run".equals(a) || "--debug".equals(a)) {
argList.add(a);
}
}
diff --git a/src/main/java/org/jcnc/snow/compiler/backend/builder/VMCodeGenerator.java b/src/main/java/org/jcnc/snow/compiler/backend/builder/VMCodeGenerator.java
index 2458b9e..e764e41 100644
--- a/src/main/java/org/jcnc/snow/compiler/backend/builder/VMCodeGenerator.java
+++ b/src/main/java/org/jcnc/snow/compiler/backend/builder/VMCodeGenerator.java
@@ -5,96 +5,83 @@ import org.jcnc.snow.compiler.backend.utils.OpHelper;
import org.jcnc.snow.compiler.ir.core.IRFunction;
import org.jcnc.snow.compiler.ir.core.IRInstruction;
import org.jcnc.snow.compiler.ir.value.IRVirtualRegister;
-
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
- * 虚拟机代码生成器(VMCodeGenerator)
+ * VMCodeGenerator 负责将中间表示(IR)函数转换为目标虚拟机(VM)的指令序列。
*
- * 本类作为指令生成器调度中心,不负责任何具体 IR 指令到 VM 指令的转换实现,
- * 仅负责根据指令类型分发到对应的 {@link InstructionGenerator} 子类完成实际生成。
- *
- *
- * 工作流程简述:
- *
- *
接收一组已注册的 IR 指令生成器,并建立类型到生成器的映射表。
- *
遍历 IR 函数体的每条指令,根据类型找到对应的生成器,调用其 generate 方法生成 VM 指令。
- *
生成流程以函数为单位(beginFunction/endFunction)。
- *
+ * 每个 IR 指令根据类型由对应的 InstructionGenerator 处理,并将结果输出到 VMProgramBuilder。
+ * 该类通过注册表(registry)实现 IR 到 VM 指令生成器的快速分发。
*/
public final class VMCodeGenerator {
/**
- * 指令类型到生成器的注册表(调度表)。
- *
- * 键: IR 指令类型(Class对象),
- * 值: 对应的指令生成器实例。
- *
+ * IR 指令类型到指令生成器的映射。
+ * 每种 IRInstruction 都有对应的 InstructionGenerator 处理。
*/
private final Map, InstructionGenerator extends IRInstruction>> registry;
/**
- * 虚拟寄存器到槽号的映射表,由 RegisterAllocator 负责生成。
+ * 虚拟寄存器到 VM 局部槽位的映射表。
+ * 用于寄存器分配与指令生成。
*/
private final Map slotMap;
/**
- * 虚拟机程序构建器,用于输出 VM 指令。
+ * 输出目标 VM 程序的构建器。
+ * 提供 emit、beginFunction、endFunction 等接口。
*/
private final VMProgramBuilder out;
/**
- * 当前处理的函数名,用于部分指令生成逻辑(如主函数判断等)。
- */
- private String currentFn;
-
- /**
- * 构造方法
+ * 构造 VMCodeGenerator。
*
- * @param slotMap 虚拟寄存器到槽号的映射
- * @param out 虚拟机程序构建器
- * @param generators 各类 IR 指令生成器集合,需预先构建
+ * @param slotMap 虚拟寄存器到 VM 局部槽位的分配表
+ * @param out 输出 VM 程序的 builder
+ * @param generators 可用的 IR 指令生成器列表,每个类型只应有一个
*/
public VMCodeGenerator(Map slotMap,
VMProgramBuilder out,
List> generators) {
this.slotMap = slotMap;
this.out = out;
- // 按类型注册各 IR 指令生成器,建立不可变类型-生成器映射表
+ // 构建不可变的类型到生成器的注册表
this.registry = generators.stream()
.collect(Collectors.toUnmodifiableMap(InstructionGenerator::supportedClass, g -> g));
}
/**
- * 为一个 IR 函数生成虚拟机指令
+ * 将 IRFunction 生成对应 VM 代码,并写入输出。
*
- * @param fn 待生成的 IR 函数对象
- * @throws IllegalStateException 若遇到不支持的 IR 指令类型
+ *
+ *