优化ir
This commit is contained in:
parent
2884159f3e
commit
d9f402f8b8
@ -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)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user