refactor: 优化编译流程,词法分析有错误时立即终止

- 在语法分析之前增加对词法分析错误的检查
- 若词法阶段存在错误,立即终止编译,避免进入后续的语法及语义分析
- 这种优化可以减少不必要的计算,提高编译效率
This commit is contained in:
Luke 2025-07-16 23:37:15 +08:00
parent 93a553ea93
commit cc106f57e1

View File

@ -168,6 +168,11 @@ public final class CompileTask implements Task {
// 词法语法分析 // 词法语法分析
LexerEngine lexer = new LexerEngine(code, p.toString()); LexerEngine lexer = new LexerEngine(code, p.toString());
// 若词法阶段存在错误立即终止编译避免进入后续的语法及语义分析
if (!lexer.getErrors().isEmpty()) {
return 1;
}
ParserContext ctx = new ParserContext(lexer.getAllTokens(), p.toString()); ParserContext ctx = new ParserContext(lexer.getAllTokens(), p.toString());
allAst.addAll(new ParserEngine(ctx).parse()); allAst.addAll(new ParserEngine(ctx).parse());
} }