From edea443801d08297cf33d3886ba89e95aee2bba8 Mon Sep 17 00:00:00 2001 From: PuqiAR Date: Mon, 29 Dec 2025 19:05:55 +0800 Subject: [PATCH] =?UTF-8?q?[Fix][Impl]=20while=20=E5=92=8C=20else=20if=20?= =?UTF-8?q?=E6=8B=AC=E5=8F=B7=E7=8E=B0=E5=9C=A8=E4=B8=BB=E5=8A=A8=E6=8E=A2?= =?UTF-8?q?=E6=B5=8B=EF=BC=8C=E5=8F=AF=E6=94=AF=E6=8C=81=E6=97=A0=E6=8B=AC?= =?UTF-8?q?=E5=8F=B7=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Parser/parser.cpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Parser/parser.cpp b/src/Parser/parser.cpp index bb3aaf0..f0f81d6 100644 --- a/src/Parser/parser.cpp +++ b/src/Parser/parser.cpp @@ -160,7 +160,7 @@ namespace Fig } expect(TokenType::Identifier, FString(u8"Identifier or `)`")); // check current FString pname = currentToken().getValue(); - next(); // skip pname + next(); // skip pname if (isThis(TokenType::Assign)) // = { next(); @@ -202,8 +202,7 @@ namespace Fig throw SyntaxError( u8"Expects right paren, variable parameter function can only have one parameter", currentAAI.line, - currentAAI.column - ); + currentAAI.column); next(); // skip `)` return Ast::FunctionParameters(variaPara); } @@ -495,7 +494,17 @@ namespace Fig { // else 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); // { Ast::BlockStatement elifBody = __parseBlockStatement(); elifs.push_back(makeAst(elifCondition, elifBody)); @@ -514,7 +523,18 @@ namespace Fig { // entry: current is `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); // { Ast::BlockStatement body = __parseBlockStatement(); return makeAst(condition, body);