From d8888fd15f981d321f9217819ad5889ffe4b3dba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sun, 1 Jan 2023 19:04:37 +0100 Subject: [PATCH] Parser - Re-added parens around if / while conditions Mostly because lox's for loop is C-like and would feel weird without parens. --- src/parser.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/parser.rs b/src/parser.rs index 24c92a7..24a3a14 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -144,7 +144,9 @@ impl Parser { /// if := "if" condition statement "else" statement /// ``` fn parse_if_statement(&mut self) -> ParserResult { + self.consume(&TokenType::LeftParen, "expected '(' after 'if'")?; let expression = self.parse_expression()?; + self.consume(&TokenType::RightParen, "expected ')' after condition in 'if' statement")?; let then_branch = Box::new(self.parse_statement()?); let else_branch = match self.expect(&[TokenType::Else]) { Some(_) => Some(Box::new(self.parse_statement()?)), @@ -162,7 +164,9 @@ impl Parser { /// while := "while" condition statement /// ``` fn parse_while_statement(&mut self) -> ParserResult { + self.consume(&TokenType::LeftParen, "expected '(' after 'while'")?; let condition = self.parse_expression()?; + self.consume(&TokenType::RightParen, "expected ')' after condition in 'while' statement")?; let body = Box::new(self.parse_statement()?); Ok(ast::StmtNode::WhileStmt { condition, body }) }