This commit is contained in:
Luke 2025-05-06 22:15:10 +08:00
parent e6bf9ecd12
commit de2190c211

View File

@ -31,17 +31,17 @@ public final class ProgramBuilder {
public IRProgram buildProgram(List<Node> roots) {
IRProgram prog = new IRProgram();
for (Node n : roots) {
if (n instanceof ModuleNode m) {
// 模块节点将模块内的所有函数加入程序
m.functions().forEach(f -> prog.add(buildFunction(f)));
} else if (n instanceof FunctionNode f) {
// 顶层函数节点
prog.add(buildFunction(f));
} else if (n instanceof StatementNode s) {
// 顶层脚本语句包装为 _start 函数
prog.add(buildFunction(wrapTopLevel(s)));
} else {
throw new IllegalStateException("Unsupported top-level node: " + n);
switch (n) {
case ModuleNode m ->
// 模块节点将模块内的所有函数加入程序
m.functions().forEach(f -> prog.add(buildFunction(f)));
case FunctionNode f ->
// 顶层函数节点
prog.add(buildFunction(f));
case StatementNode s ->
// 顶层脚本语句包装为 _start 函数
prog.add(buildFunction(wrapTopLevel(s)));
case null, default -> throw new IllegalStateException("Unsupported top-level node: " + n);
}
}
return prog;