refactor: 重构 ASTPrinter 类中的代码

- 修改了 IfNode 和 LoopNode 的打印逻辑
- 优化了变量命名,提高了代码可读性
- 调整了缩进和格式,保持代码风格一致性
This commit is contained in:
Luke 2025-07-14 23:46:11 +08:00
parent 1141abf231
commit bc309dfa97

View File

@ -86,9 +86,9 @@ public class ASTPrinter {
case AssignmentNode(String variable, ExpressionNode value, NodeContext _) -> case AssignmentNode(String variable, ExpressionNode value, NodeContext _) ->
System.out.println(pad + variable + " = " + value); System.out.println(pad + variable + " = " + value);
case IfNode( case IfNode(
ExpressionNode condition, List<StatementNode> thenBranch, List<StatementNode> elseBranch, NodeContext _ ExpressionNode cond, List<StatementNode> thenBranch, List<StatementNode> elseBranch, NodeContext _
) -> { ) -> {
System.out.println(pad + "if " + condition); System.out.println(pad + "if " + cond);
for (StatementNode stmt : thenBranch) { for (StatementNode stmt : thenBranch) {
print(stmt, indent + 1); print(stmt, indent + 1);
} }
@ -100,13 +100,13 @@ public class ASTPrinter {
} }
} }
case LoopNode( case LoopNode(
StatementNode initializer, ExpressionNode condition, StatementNode update, List<StatementNode> body, StatementNode init, ExpressionNode cond, StatementNode step, List<StatementNode> body,
NodeContext _ NodeContext _
) -> { ) -> {
System.out.println(pad + "loop {"); System.out.println(pad + "loop {");
print(initializer, indent + 1); print(init, indent + 1);
System.out.println(pad + " condition: " + condition); System.out.println(pad + " cond: " + cond);
System.out.println(pad + " update: " + update); System.out.println(pad + " step: " + step);
System.out.println(pad + " body {"); System.out.println(pad + " body {");
for (StatementNode stmt : body) { for (StatementNode stmt : body) {
print(stmt, indent + 2); print(stmt, indent + 2);