Parser - Variable references in expressions

This commit is contained in:
Emmanuel BENOîT 2022-12-31 15:44:26 +01:00
parent a90b1529ad
commit d821331fa2

View file

@ -214,6 +214,7 @@ impl Parser {
/// ``` /// ```
/// primary := "(" expression ")" /// primary := "(" expression ")"
/// primary := FALSE | TRUE | NIL | STRING | NUMBER /// primary := FALSE | TRUE | NIL | STRING | NUMBER
/// primary := IDENTIFIER
/// ``` /// ```
fn parse_primary(&mut self) -> Result<ast::ExprNode, ParserError> { fn parse_primary(&mut self) -> Result<ast::ExprNode, ParserError> {
if self.expect(&[TokenType::LeftParen]).is_some() { if self.expect(&[TokenType::LeftParen]).is_some() {
@ -231,6 +232,9 @@ impl Parser {
TokenType::Number(_) | &TokenType::String(_) => Ok(ast::ExprNode::Litteral { TokenType::Number(_) | &TokenType::String(_) => Ok(ast::ExprNode::Litteral {
value: self.advance().clone(), value: self.advance().clone(),
}), }),
TokenType::Identifier(_) => Ok(ast::ExprNode::Variable {
name: self.advance().clone(),
}),
_ => Err(ParserError::new(self.peek(), "expected expression")), _ => Err(ParserError::new(self.peek(), "expected expression")),
} }
} }