refactor: 重构循环语句分析逻辑
- 修改初始化语句、条件表达式和更新语句的分析方法 - 优化代码结构,提高可读性和可维护性 - 适应新的语法树节点命名
This commit is contained in:
parent
d92e3d0e17
commit
b135a4b37b
@ -44,24 +44,24 @@ public class LoopAnalyzer implements StatementAnalyzer<LoopNode> {
|
|||||||
// 新建 loopScope,以支持循环内部变量声明与外部隔离
|
// 新建 loopScope,以支持循环内部变量声明与外部隔离
|
||||||
SymbolTable loopScope = new SymbolTable(locals);
|
SymbolTable loopScope = new SymbolTable(locals);
|
||||||
|
|
||||||
// 2. 分析初始化语句(如 for(i=0)),使用 loopScope 作为作用域
|
// 2. 分析初始化语句
|
||||||
var initAnalyzer = ctx.getRegistry().getStatementAnalyzer(ln.initializer());
|
var initAnalyzer = ctx.getRegistry().getStatementAnalyzer(ln.init());
|
||||||
if (initAnalyzer != null) {
|
if (initAnalyzer != null) {
|
||||||
initAnalyzer.analyze(ctx, mi, fn, loopScope, ln.initializer());
|
initAnalyzer.analyze(ctx, mi, fn, loopScope, ln.init());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 分析条件表达式(如 for(...; cond; ...) 或 while(cond))
|
// 3. 分析条件表达式
|
||||||
var condAnalyzer = ctx.getRegistry().getExpressionAnalyzer(ln.condition());
|
var condAnalyzer = ctx.getRegistry().getExpressionAnalyzer(ln.cond());
|
||||||
Type condType = condAnalyzer.analyze(ctx, mi, fn, loopScope, ln.condition());
|
Type condType = condAnalyzer.analyze(ctx, mi, fn, loopScope, ln.cond());
|
||||||
// 条件类型必须为 boolean,否则记录错误
|
// 条件类型必须为 boolean,否则记录错误
|
||||||
if (TypeUtils.isLogic(condType)) {
|
if (TypeUtils.isLogic(condType)) {
|
||||||
ctx.getErrors().add(new SemanticError(ln, "loop 条件必须为 boolean"));
|
ctx.getErrors().add(new SemanticError(ln, "loop 条件必须为 boolean"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. 分析更新语句(如 for(...; ...; update))
|
// 4. 分析更新语句
|
||||||
var updateAnalyzer = ctx.getRegistry().getStatementAnalyzer(ln.update());
|
var updateAnalyzer = ctx.getRegistry().getStatementAnalyzer(ln.step());
|
||||||
if (updateAnalyzer != null) {
|
if (updateAnalyzer != null) {
|
||||||
updateAnalyzer.analyze(ctx, mi, fn, loopScope, ln.update());
|
updateAnalyzer.analyze(ctx, mi, fn, loopScope, ln.step());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. 分析循环体内的每一条语句
|
// 5. 分析循环体内的每一条语句
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user