refactor: 重构循环语句解析器
- 简化了循环语句各区块的名称,提高代码可读性 - 更新了代码注释,使其与新的区块名称保持一致- 修改了变量命名,以更好地反映其对应的循环语句部分- 优化了代码结构,提高了维护性和可扩展性
This commit is contained in:
parent
2c4850cc8f
commit
fb1c54998b
@ -24,23 +24,23 @@ import java.util.Map;
|
||||
* 该语法结构参考了传统的 for-loop,并将其拆解为命名的语义区块:
|
||||
* <pre>{@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
|
||||
* }</pre>
|
||||
*
|
||||
* 各区块说明:
|
||||
* <ul>
|
||||
* <li>{@code initializer}:初始化语句,通常为变量声明。</li>
|
||||
* <li>{@code condition}:循环判断条件,必须为布尔或数值表达式。</li>
|
||||
* <li>{@code update}:每轮执行后更新逻辑,通常为赋值语句。</li>
|
||||
* <li>{@code init}:初始化语句,通常为变量声明。</li>
|
||||
* <li>{@code cond}:循环判断条件,必须为布尔或数值表达式。</li>
|
||||
* <li>{@code step}:每轮执行后更新逻辑,通常为赋值语句。</li>
|
||||
* <li>{@code body}:主执行语句块,支持任意多条语句。</li>
|
||||
* </ul>
|
||||
* 本类依赖 {@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<StatementNode> body = new ArrayList<>();
|
||||
|
||||
// 定义各命名区块的识别与处理逻辑
|
||||
Map<String, FlexibleSectionParser.SectionDefinition> 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));
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user