This commit is contained in:
Luke 2025-04-29 16:43:30 +08:00
parent cb1780327c
commit e14856bb7a
2 changed files with 6 additions and 7 deletions

View File

@ -69,11 +69,10 @@ public final class BasicIRBuilder {
moduleNode.functions().forEach(functionNode -> dispatchTop(functionNode, irProgram)); // 递归处理模块内的函数 moduleNode.functions().forEach(functionNode -> dispatchTop(functionNode, irProgram)); // 递归处理模块内的函数
case FunctionNode functionNode -> case FunctionNode functionNode ->
irProgram.add(buildFunction(functionNode)); // 将函数编译成 IRFunction 并加入 IRProgram irProgram.add(buildFunction(functionNode)); // 将函数编译成 IRFunction 并加入 IRProgram
case StatementNode statementNode -> { // 单条语句如脚本式 main case StatementNode _ -> {
FunctionNode functionNode = new FunctionNode("main", List.of(), "void", List.of(statementNode)); //TODO 单条语句如脚本式 main
irProgram.add(buildFunction(functionNode));
} }
default -> throw new IllegalStateException("Unsupported top-level node: " + node); default -> throw new IllegalStateException("不支持的顶级节点: " + node);
} }
} }
@ -128,7 +127,7 @@ public final class BasicIRBuilder {
if (r.getExpression().isPresent()) currentFn.add(new ReturnInstruction(expr(r.getExpression().get()))); if (r.getExpression().isPresent()) currentFn.add(new ReturnInstruction(expr(r.getExpression().get())));
else currentFn.add(new ReturnInstruction(null)); else currentFn.add(new ReturnInstruction(null));
} }
default -> throw new IllegalStateException("Unsupported stmt: " + n); default -> throw new IllegalStateException("不支持的语句: " + n);
} }
} }
@ -149,7 +148,7 @@ public final class BasicIRBuilder {
if (v == null) throw new IllegalStateException("变量 " + id.name() + " 未定义"); if (v == null) throw new IllegalStateException("变量 " + id.name() + " 未定义");
yield v; yield v;
} }
default -> throw new IllegalStateException("Unsupported expr: " + e); default -> throw new IllegalStateException("不支持的表达式: " + e);
}; };
} }

2
test
View File

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