Parser - Handle AST refactorings

This commit is contained in:
Emmanuel BENOîT 2023-01-09 07:46:42 +01:00
parent bb2765d057
commit 0c588942bf

View file

@ -1,7 +1,10 @@
use std::collections::HashSet; use std::collections::HashSet;
use crate::{ use crate::{
ast::{ClassDecl, ExprNode, FunDecl, GetExpr, ProgramNode, SetExpr, StmtNode}, ast::{
BinaryExpr, ClassDecl, ExprNode, FunDecl, GetExpr, ProgramNode, SetExpr, StmtNode,
VariableExpr,
},
errors::{ErrorHandler, ErrorKind, SloxError, SloxResult}, errors::{ErrorHandler, ErrorKind, SloxError, SloxResult},
tokens::{Token, TokenType}, tokens::{Token, TokenType},
}; };
@ -526,9 +529,9 @@ impl Parser {
let expr = self.parse_logic_or()?; 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 ExprNode::Variable { name, id: _ } = expr { if let ExprNode::Variable(var) = expr {
Ok(ExprNode::Assignment { Ok(ExprNode::Assignment {
name, name: var.token,
value: Box::new(value), value: Box::new(value),
id: self.make_id(), id: self.make_id(),
}) })
@ -558,11 +561,11 @@ impl Parser {
let mut expr = self.parse_logic_and()?; let mut expr = self.parse_logic_and()?;
while let Some(operator) = self.expect(&[TokenType::Or]) { while let Some(operator) = self.expect(&[TokenType::Or]) {
let right = self.parse_logic_and()?; let right = self.parse_logic_and()?;
expr = ExprNode::Logical { expr = ExprNode::Logical(BinaryExpr {
left: Box::new(expr), left: Box::new(expr),
operator: operator.clone(), operator: operator.clone(),
right: Box::new(right), right: Box::new(right),
}; });
} }
Ok(expr) Ok(expr)
} }
@ -575,11 +578,11 @@ impl Parser {
let mut expr = self.parse_equality()?; let mut expr = self.parse_equality()?;
while let Some(operator) = self.expect(&[TokenType::And]) { while let Some(operator) = self.expect(&[TokenType::And]) {
let right = self.parse_equality()?; let right = self.parse_equality()?;
expr = ExprNode::Logical { expr = ExprNode::Logical(BinaryExpr {
left: Box::new(expr), left: Box::new(expr),
operator: operator.clone(), operator: operator.clone(),
right: Box::new(right), right: Box::new(right),
}; });
} }
Ok(expr) Ok(expr)
} }
@ -593,11 +596,11 @@ impl Parser {
let mut expr = self.parse_comparison()?; let mut expr = self.parse_comparison()?;
while let Some(operator) = self.expect(&[TokenType::BangEqual, TokenType::EqualEqual]) { while let Some(operator) = self.expect(&[TokenType::BangEqual, TokenType::EqualEqual]) {
let right = self.parse_comparison()?; let right = self.parse_comparison()?;
expr = ExprNode::Binary { expr = ExprNode::Binary(BinaryExpr {
left: Box::new(expr), left: Box::new(expr),
operator: operator.clone(), operator: operator.clone(),
right: Box::new(right), right: Box::new(right),
}; });
} }
Ok(expr) Ok(expr)
} }
@ -616,11 +619,11 @@ impl Parser {
TokenType::LessEqual, TokenType::LessEqual,
]) { ]) {
let right = self.parse_term()?; let right = self.parse_term()?;
expr = ExprNode::Binary { expr = ExprNode::Binary(BinaryExpr {
left: Box::new(expr), left: Box::new(expr),
operator: operator.clone(), operator: operator.clone(),
right: Box::new(right), right: Box::new(right),
}; });
} }
Ok(expr) Ok(expr)
} }
@ -634,11 +637,11 @@ impl Parser {
let mut expr = self.parse_factor()?; let mut expr = self.parse_factor()?;
while let Some(operator) = self.expect(&[TokenType::Minus, TokenType::Plus]) { while let Some(operator) = self.expect(&[TokenType::Minus, TokenType::Plus]) {
let right = self.parse_factor()?; let right = self.parse_factor()?;
expr = ExprNode::Binary { expr = ExprNode::Binary(BinaryExpr {
left: Box::new(expr), left: Box::new(expr),
operator: operator.clone(), operator: operator.clone(),
right: Box::new(right), right: Box::new(right),
}; });
} }
Ok(expr) Ok(expr)
} }
@ -652,11 +655,11 @@ impl Parser {
let mut expr = self.parse_unary()?; let mut expr = self.parse_unary()?;
while let Some(operator) = self.expect(&[TokenType::Slash, TokenType::Star]) { while let Some(operator) = self.expect(&[TokenType::Slash, TokenType::Star]) {
let right = self.parse_unary()?; let right = self.parse_unary()?;
expr = ExprNode::Binary { expr = ExprNode::Binary(BinaryExpr {
left: Box::new(expr), left: Box::new(expr),
operator: operator.clone(), operator: operator.clone(),
right: Box::new(right), right: Box::new(right),
}; });
} }
Ok(expr) Ok(expr)
} }
@ -722,10 +725,10 @@ impl Parser {
TokenType::Number(_) | &TokenType::String(_) => Ok(ExprNode::Litteral { TokenType::Number(_) | &TokenType::String(_) => Ok(ExprNode::Litteral {
value: self.advance().clone(), value: self.advance().clone(),
}), }),
TokenType::Identifier(_) => Ok(ExprNode::Variable { TokenType::Identifier(_) => Ok(ExprNode::Variable(VariableExpr {
name: self.advance().clone(), token: self.advance().clone(),
id: self.make_id(), id: self.make_id(),
}), })),
_ => self.error("expected expression"), _ => self.error("expected expression"),
} }
} }