From 2e0e3f9ef0072e799e1d01676f1236d98f6cbdfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sun, 1 Jan 2023 11:18:08 +0100 Subject: [PATCH] Parser - Support for and / or --- src/parser.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/parser.rs b/src/parser.rs index 38f516a..fe2c143 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -167,7 +167,7 @@ impl Parser { /// assignment := equality /// ``` fn parse_assignment(&mut self) -> ParserResult { - let expr = self.parse_equality()?; + let expr = self.parse_logic_or()?; if let Some(equals) = self.expect(&[TokenType::Equal]) { let value = self.parse_assignment()?; if let ast::ExprNode::Variable { name } = expr { @@ -183,6 +183,40 @@ impl Parser { } } + /// Parse the following rule: + /// ``` + /// logic_or := logic_and ( "or" logic_and )* + /// ``` + fn parse_logic_or(&mut self) -> ParserResult { + let mut expr = self.parse_logic_and()?; + while let Some(operator) = self.expect(&[TokenType::Or]) { + let right = self.parse_logic_and()?; + expr = ast::ExprNode::Logical { + left: Box::new(expr), + operator: operator.clone(), + right: Box::new(right), + }; + } + Ok(expr) + } + + /// Parse the following rule: + /// ``` + /// logic_and := equality ( "and" equality )* + /// ``` + fn parse_logic_and(&mut self) -> ParserResult { + let mut expr = self.parse_equality()?; + while let Some(operator) = self.expect(&[TokenType::And]) { + let right = self.parse_equality()?; + expr = ast::ExprNode::Logical { + left: Box::new(expr), + operator: operator.clone(), + right: Box::new(right), + }; + } + Ok(expr) + } + /// Parse the following rule: /// ``` /// equality := comparison "==" comparison