Parser - Support for and / or

This commit is contained in:
Emmanuel BENOîT 2023-01-01 11:18:08 +01:00
parent 0637e52c83
commit 2e0e3f9ef0

View file

@ -167,7 +167,7 @@ impl Parser {
/// assignment := equality /// assignment := equality
/// ``` /// ```
fn parse_assignment(&mut self) -> ParserResult<ast::ExprNode> { fn parse_assignment(&mut self) -> ParserResult<ast::ExprNode> {
let expr = self.parse_equality()?; let expr = self.parse_logic_or()?;
if let Some(equals) = self.expect(&[TokenType::Equal]) { if let Some(equals) = self.expect(&[TokenType::Equal]) {
let value = self.parse_assignment()?; let value = self.parse_assignment()?;
if let ast::ExprNode::Variable { name } = expr { 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<ast::ExprNode> {
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<ast::ExprNode> {
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: /// Parse the following rule:
/// ``` /// ```
/// equality := comparison "==" comparison /// equality := comparison "==" comparison