修复else

This commit is contained in:
Luke 2025-04-25 15:07:06 +08:00
parent 4237c7db7a
commit 310191407e
2 changed files with 37 additions and 18 deletions

View File

@ -56,12 +56,19 @@ public class ASTPrinter {
} }
case AssignmentNode(String variable, ExpressionNode value) -> case AssignmentNode(String variable, ExpressionNode value) ->
System.out.println(pad + variable + " = " + value); System.out.println(pad + variable + " = " + value);
case IfNode(ExpressionNode condition, List<StatementNode> thenBranch) -> { case IfNode(ExpressionNode condition, List<StatementNode> thenBranch, List<StatementNode> elseBranch) -> {
System.out.println(pad + "if " + condition); System.out.println(pad + "if " + condition);
for (StatementNode stmt : thenBranch) { for (StatementNode stmt : thenBranch) {
print(stmt, indent + 1); print(stmt, indent + 1);
} }
if (!elseBranch.isEmpty()) {
System.out.println(pad + "else");
for (StatementNode stmt : elseBranch) {
print(stmt, indent + 1);
}
}
} }
case LoopNode( case LoopNode(
StatementNode initializer, ExpressionNode condition, StatementNode update, List<StatementNode> body StatementNode initializer, ExpressionNode condition, StatementNode update, List<StatementNode> body
) -> { ) -> {

View File

@ -12,65 +12,75 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* if 语句解析器支持 * 解析 if 语句
* <pre>{@code *
* if condition then * 支持格式
* statements... * if <condition> then
* else * <then-statements>
* statements... * [else
* <else-statements>]
* end if * end if
* }</pre>
*/ */
public class IfStatementParser implements StatementParser { public class IfStatementParser implements StatementParser {
@Override @Override
public IfNode parse(ParserContext ctx) { public IfNode parse(ParserContext ctx) {
var ts = ctx.getTokens(); var ts = ctx.getTokens(); // 获取 Token 流管理器
ts.expect("if"); ts.expect("if"); // 消费 "if" 关键字
// 解析条件表达式 // 使用 Pratt 解析器解析布尔表达式或其他条件表达式
var cond = new PrattExpressionParser().parse(ctx); var condition = new PrattExpressionParser().parse(ctx);
// 消费 "then" 关键字和随后的换行符
ts.expect("then"); ts.expect("then");
ts.expectType(TokenType.NEWLINE); ts.expectType(TokenType.NEWLINE);
// 准备容器存储 then else 分支的语句
List<StatementNode> thenBranch = new ArrayList<>(); List<StatementNode> thenBranch = new ArrayList<>();
List<StatementNode> elseBranch = new ArrayList<>(); List<StatementNode> elseBranch = new ArrayList<>();
// --- THEN 分支 --- // -------------------
// 解析 THEN 分支语句
// -------------------
while (true) { while (true) {
Token peek = ts.peek(); Token peek = ts.peek();
// 跳过空行 // 忽略空行
if (peek.getType() == TokenType.NEWLINE) { if (peek.getType() == TokenType.NEWLINE) {
ts.next(); ts.next();
continue; continue;
} }
// 检测是否进入 else end跳出 then 区块
if (peek.getType() == TokenType.KEYWORD && if (peek.getType() == TokenType.KEYWORD &&
(peek.getLexeme().equals("else") || peek.getLexeme().equals("end"))) { (peek.getLexeme().equals("else") || peek.getLexeme().equals("end"))) {
break; break;
} }
// 提取关键词交由 StatementParserFactory 派发对应解析器
String keyword = peek.getType() == TokenType.KEYWORD ? peek.getLexeme() : ""; String keyword = peek.getType() == TokenType.KEYWORD ? peek.getLexeme() : "";
StatementNode stmt = StatementParserFactory.get(keyword).parse(ctx); StatementNode stmt = StatementParserFactory.get(keyword).parse(ctx);
thenBranch.add(stmt); thenBranch.add(stmt);
} }
// --- ELSE 分支 --- // -------------------
// 解析 ELSE 分支语句
// -------------------
if (ts.peek().getLexeme().equals("else")) { if (ts.peek().getLexeme().equals("else")) {
ts.next(); // consume 'else' ts.next(); // 消费 "else"
ts.expectType(TokenType.NEWLINE); ts.expectType(TokenType.NEWLINE); // 消费换行
while (true) { while (true) {
Token peek = ts.peek(); Token peek = ts.peek();
// 忽略空行
if (peek.getType() == TokenType.NEWLINE) { if (peek.getType() == TokenType.NEWLINE) {
ts.next(); ts.next();
continue; continue;
} }
// "end" 关键字表示 else 块结束
if (peek.getType() == TokenType.KEYWORD && peek.getLexeme().equals("end")) { if (peek.getType() == TokenType.KEYWORD && peek.getLexeme().equals("end")) {
break; break;
} }
@ -81,10 +91,12 @@ public class IfStatementParser implements StatementParser {
} }
} }
// 统一消费 "end if" 和其后的换行
ts.expect("end"); ts.expect("end");
ts.expect("if"); ts.expect("if");
ts.expectType(TokenType.NEWLINE); ts.expectType(TokenType.NEWLINE);
return new IfNode(cond, thenBranch, elseBranch); // 构造 AST 节点返回 IfNode 包含条件then 分支else 分支
return new IfNode(condition, thenBranch, elseBranch);
} }
} }