This commit is contained in:
Luke 2025-05-06 22:21:59 +08:00
parent 2884159f3e
commit d9f402f8b8
2 changed files with 7 additions and 12 deletions

View File

@ -29,22 +29,22 @@ public final class ProgramBuilder {
* @throws IllegalStateException 当遇到不支持的顶层节点时抛出 * @throws IllegalStateException 当遇到不支持的顶层节点时抛出
*/ */
public IRProgram buildProgram(List<Node> roots) { public IRProgram buildProgram(List<Node> roots) {
IRProgram prog = new IRProgram(); IRProgram irProgram = new IRProgram();
for (Node n : roots) { for (Node n : roots) {
switch (n) { switch (n) {
case ModuleNode m -> case ModuleNode m ->
// 模块节点将模块内的所有函数加入程序 // 模块节点将模块内的所有函数加入程序
m.functions().forEach(f -> prog.add(buildFunction(f))); m.functions().forEach(f -> irProgram.add(buildFunction(f)));
case FunctionNode f -> case FunctionNode f ->
// 顶层函数节点 // 顶层函数节点
prog.add(buildFunction(f)); irProgram.add(buildFunction(f));
case StatementNode s -> case StatementNode s ->
// 顶层脚本语句包装为 _start 函数 // 顶层脚本语句包装为 _start 函数
prog.add(buildFunction(wrapTopLevel(s))); irProgram.add(buildFunction(wrapTopLevel(s)));
case null, default -> throw new IllegalStateException("Unsupported top-level node: " + n); case null, default -> throw new IllegalStateException("Unsupported top-level node: " + n);
} }
} }
return prog; return irProgram;
} }
/** /**
@ -72,11 +72,6 @@ public final class ProgramBuilder {
* @return 新建的 FunctionNode * @return 新建的 FunctionNode
*/ */
private FunctionNode wrapTopLevel(StatementNode stmt) { private FunctionNode wrapTopLevel(StatementNode stmt) {
return new FunctionNode( return new FunctionNode("_start", null, String.valueOf(List.of()), List.of(stmt));
"_start",
null,
String.valueOf(List.of()),
List.of(stmt)
);
} }
} }

2
test
View File

@ -1,5 +1,5 @@
module: CommonTasks module: CommonTasks
function: main function: main1
parameter: parameter:
declare num1: int declare num1: int
declare num2: int declare num2: int