[Fix][Impl] while 和 else if 括号现在主动探测,可支持无括号写法

This commit is contained in:
2025-12-29 19:05:55 +08:00
parent 393a64cac9
commit edea443801

View File

@@ -202,8 +202,7 @@ namespace Fig
throw SyntaxError( throw SyntaxError(
u8"Expects right paren, variable parameter function can only have one parameter", u8"Expects right paren, variable parameter function can only have one parameter",
currentAAI.line, currentAAI.line,
currentAAI.column currentAAI.column);
);
next(); // skip `)` next(); // skip `)`
return Ast::FunctionParameters(variaPara); return Ast::FunctionParameters(variaPara);
} }
@@ -495,7 +494,17 @@ namespace Fig
{ {
// else if // else if
next(); // consume `if` next(); // consume `if`
Ast::Expression elifCondition = parseExpression(0); Ast::Expression elifCondition;
if (isThis(TokenType::LeftParen))
{
elifCondition = parseExpression(0, TokenType::RightParen);
expect(TokenType::RightParen);
next(); // consume `)`
}
else
{
elifCondition = parseExpression(0);
}
expect(TokenType::LeftBrace); // { expect(TokenType::LeftBrace); // {
Ast::BlockStatement elifBody = __parseBlockStatement(); Ast::BlockStatement elifBody = __parseBlockStatement();
elifs.push_back(makeAst<Ast::ElseIfSt>(elifCondition, elifBody)); elifs.push_back(makeAst<Ast::ElseIfSt>(elifCondition, elifBody));
@@ -514,7 +523,18 @@ namespace Fig
{ {
// entry: current is `while` // entry: current is `while`
next(); // consume `while` next(); // consume `while`
Ast::Expression condition = parseExpression(0); Ast::Expression condition;
if (isThis(TokenType::LeftParen))
{
next(); // consume `(`
condition = parseExpression(0, TokenType::RightParen);
expect(TokenType::RightParen);
next(); // consume `)`
}
else
{
condition = parseExpression(0);
}
expect(TokenType::LeftBrace); // { expect(TokenType::LeftBrace); // {
Ast::BlockStatement body = __parseBlockStatement(); Ast::BlockStatement body = __parseBlockStatement();
return makeAst<Ast::WhileSt>(condition, body); return makeAst<Ast::WhileSt>(condition, body);