From fb1c54998b1b4b8b34554ff941ec45a3f58c5f7a Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 14 Jul 2025 23:49:44 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E5=BE=AA?= =?UTF-8?q?=E7=8E=AF=E8=AF=AD=E5=8F=A5=E8=A7=A3=E6=9E=90=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 简化了循环语句各区块的名称,提高代码可读性 - 更新了代码注释,使其与新的区块名称保持一致- 修改了变量命名,以更好地反映其对应的循环语句部分- 优化了代码结构,提高了维护性和可扩展性 --- .../parser/statement/LoopStatementParser.java | 62 +++++++++---------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/jcnc/snow/compiler/parser/statement/LoopStatementParser.java b/src/main/java/org/jcnc/snow/compiler/parser/statement/LoopStatementParser.java index c1a9c66..7ffeb74 100644 --- a/src/main/java/org/jcnc/snow/compiler/parser/statement/LoopStatementParser.java +++ b/src/main/java/org/jcnc/snow/compiler/parser/statement/LoopStatementParser.java @@ -24,23 +24,23 @@ import java.util.Map; * 该语法结构参考了传统的 for-loop,并将其拆解为命名的语义区块: *
{@code
  * loop:
- *   initializer:
- *     declare i:int = 0
- *   condition:
- *     i < 10
- *   update:
- *     i = i + 1
- *   body:
- *     print(i)
- * end body
+ *     init:
+ *         declare i: int = 0
+ *     cond:
+ *         i < 10
+ *     step:
+ *         i = i + 1
+ *     body:
+ *         …
+ *     end body
  * end loop
  * }
* * 各区块说明: * * 本类依赖 {@link FlexibleSectionParser} 实现各区块的统一处理,确保结构明确、可扩展。 @@ -75,49 +75,49 @@ public class LoopStatementParser implements StatementParser { ParserUtils.matchHeader(ts, "loop"); // 使用数组模拟引用以便在 lambda 中写入(Java 不支持闭包内修改局部变量) - final StatementNode[] initializer = new StatementNode[1]; - final ExpressionNode[] condition = new ExpressionNode[1]; - final AssignmentNode[] update = new AssignmentNode[1]; + final StatementNode[] init = new StatementNode[1]; + final ExpressionNode[] cond = new ExpressionNode[1]; + final AssignmentNode[] step = new AssignmentNode[1]; final List body = new ArrayList<>(); // 定义各命名区块的识别与处理逻辑 Map sections = new HashMap<>(); - // initializer 区块:仅支持一条语句,通常为 declare - sections.put("initializer", new FlexibleSectionParser.SectionDefinition( - ts1 -> ts1.peek().getLexeme().equals("initializer"), + // init 区块:仅支持一条语句,通常为 declare + sections.put("init", new FlexibleSectionParser.SectionDefinition( + ts1 -> ts1.peek().getLexeme().equals("init"), (ctx1, ts1) -> { - ParserUtils.matchHeader(ts1, "initializer"); - initializer[0] = StatementParserFactory.get(ts1.peek().getLexeme()).parse(ctx1); + ParserUtils.matchHeader(ts1, "init"); + init[0] = StatementParserFactory.get(ts1.peek().getLexeme()).parse(ctx1); ParserUtils.skipNewlines(ts1); } )); - // condition 区块:支持任意可解析为布尔的表达式 - sections.put("condition", new FlexibleSectionParser.SectionDefinition( - ts1 -> ts1.peek().getLexeme().equals("condition"), + // cond 区块:支持任意可解析为布尔的表达式 + sections.put("cond", new FlexibleSectionParser.SectionDefinition( + ts1 -> ts1.peek().getLexeme().equals("cond"), (ctx1, ts1) -> { - ParserUtils.matchHeader(ts1, "condition"); - condition[0] = new PrattExpressionParser().parse(ctx1); + ParserUtils.matchHeader(ts1, "cond"); + cond[0] = new PrattExpressionParser().parse(ctx1); ts1.expectType(TokenType.NEWLINE); ParserUtils.skipNewlines(ts1); } )); - // update 区块:目前仅支持单一变量赋值语句 - sections.put("update", new FlexibleSectionParser.SectionDefinition( - ts1 -> ts1.peek().getLexeme().equals("update"), + // step 区块:目前仅支持单一变量赋值语句 + sections.put("step", new FlexibleSectionParser.SectionDefinition( + ts1 -> ts1.peek().getLexeme().equals("step"), (ctx1, ts1) -> { // 获取当前 token 的行号、列号 int line = ctx.getTokens().peek().getLine(); int column = ctx.getTokens().peek().getCol(); - ParserUtils.matchHeader(ts1, "update"); + ParserUtils.matchHeader(ts1, "step"); String varName = ts1.expectType(TokenType.IDENTIFIER).getLexeme(); ts1.expect("="); ExpressionNode expr = new PrattExpressionParser().parse(ctx1); ts1.expectType(TokenType.NEWLINE); - update[0] = new AssignmentNode(varName, expr, new NodeContext(line, column, file)); + step[0] = new AssignmentNode(varName, expr, new NodeContext(line, column, file)); ParserUtils.skipNewlines(ts1); } )); @@ -151,6 +151,6 @@ public class LoopStatementParser implements StatementParser { ParserUtils.matchFooter(ts, "loop"); // 返回构造完成的 LoopNode - return new LoopNode(initializer[0], condition[0], update[0], body, new NodeContext(loop_line, loop_column, file)); + return new LoopNode(init[0], cond[0], step[0], body, new NodeContext(loop_line, loop_column, file)); } }