Parser - Re-added parens around if / while conditions

Mostly because lox's for loop is C-like and would feel weird without
parens.
This commit is contained in:
Emmanuel BENOîT 2023-01-01 19:04:37 +01:00
parent ddf6625326
commit d8888fd15f

View file

@ -144,7 +144,9 @@ impl Parser {
/// if := "if" condition statement "else" statement /// if := "if" condition statement "else" statement
/// ``` /// ```
fn parse_if_statement(&mut self) -> ParserResult<ast::StmtNode> { fn parse_if_statement(&mut self) -> ParserResult<ast::StmtNode> {
self.consume(&TokenType::LeftParen, "expected '(' after 'if'")?;
let expression = self.parse_expression()?; let expression = self.parse_expression()?;
self.consume(&TokenType::RightParen, "expected ')' after condition in 'if' statement")?;
let then_branch = Box::new(self.parse_statement()?); let then_branch = Box::new(self.parse_statement()?);
let else_branch = match self.expect(&[TokenType::Else]) { let else_branch = match self.expect(&[TokenType::Else]) {
Some(_) => Some(Box::new(self.parse_statement()?)), Some(_) => Some(Box::new(self.parse_statement()?)),
@ -162,7 +164,9 @@ impl Parser {
/// while := "while" condition statement /// while := "while" condition statement
/// ``` /// ```
fn parse_while_statement(&mut self) -> ParserResult<ast::StmtNode> { fn parse_while_statement(&mut self) -> ParserResult<ast::StmtNode> {
self.consume(&TokenType::LeftParen, "expected '(' after 'while'")?;
let condition = self.parse_expression()?; let condition = self.parse_expression()?;
self.consume(&TokenType::RightParen, "expected ')' after condition in 'while' statement")?;
let body = Box::new(self.parse_statement()?); let body = Box::new(self.parse_statement()?);
Ok(ast::StmtNode::WhileStmt { condition, body }) Ok(ast::StmtNode::WhileStmt { condition, body })
} }