This commit is contained in:
Luke 2025-04-29 15:57:59 +08:00
parent a67ac422bc
commit b2e5665bf6

View File

@ -50,7 +50,7 @@ public final class BasicIRBuilder {
*/ */
public IRProgram buildProgram(List<Node> roots) { public IRProgram buildProgram(List<Node> roots) {
IRProgram prog = new IRProgram(); // 创建新的 IRProgram IRProgram prog = new IRProgram(); // 创建新的 IRProgram
for (Node n : roots) dispatchTop(n, prog); // 依次处理所有顶层节点 for (Node node : roots) dispatchTop(node, prog); // 依次处理所有顶层节点
return prog; return prog;
} }
@ -60,18 +60,18 @@ public final class BasicIRBuilder {
* 处理单个顶层节点 * 处理单个顶层节点
* 可能是模块ModuleNode函数FunctionNode单条语句StatementNode * 可能是模块ModuleNode函数FunctionNode单条语句StatementNode
* *
* @param n AST 顶层节点 * @param node AST 顶层节点
* @param prog 正在构建的 IRProgram * @param irProgram 正在构建的 IRProgram
*/ */
private void dispatchTop(Node n, IRProgram prog) { private void dispatchTop(Node node, IRProgram irProgram) {
switch (n) { switch (node) {
case ModuleNode m -> m.functions().forEach(f -> dispatchTop(f, prog)); // 递归处理模块内的函数 case ModuleNode moduleNode -> moduleNode.functions().forEach(f -> dispatchTop(f, irProgram)); // 递归处理模块内的函数
case FunctionNode f -> prog.add(buildFunction(f)); // 将函数编译成 IRFunction 并加入 IRProgram case FunctionNode functionNode -> irProgram.add(buildFunction(functionNode)); // 将函数编译成 IRFunction 并加入 IRProgram
case StatementNode s -> { // 单条语句如脚本式 main case StatementNode statementNode -> { // 单条语句如脚本式 main
FunctionNode fake = new FunctionNode("main", List.of(), "void", List.of(s)); FunctionNode fake = new FunctionNode("main", List.of(), "void", List.of(statementNode));
prog.add(buildFunction(fake)); irProgram.add(buildFunction(fake));
} }
default -> throw new IllegalStateException("Unsupported top-level node: " + n); default -> throw new IllegalStateException("Unsupported top-level node: " + node);
} }
} }