Affectation de variable

This commit is contained in:
2024-06-04 08:16:58 +02:00
parent e121ba2160
commit 897a6c4c13
6 changed files with 64 additions and 18 deletions

View File

@@ -1,4 +1,4 @@
use crate::expr::{Binary, Expr, Grouping, Unary, Variable};
use crate::expr::{Assign, Binary, Expr, Grouping, Unary, Variable};
use crate::expr::Literal::{LiteralBool, LiteralNumber, LiteralString};
use crate::token::Token;
use crate::token_type::TokenType;
@@ -92,11 +92,28 @@ impl Parser {
}
}
fn expression(&mut self) -> Result<Expr,ParseError> {
self.equality()
fn expression(&mut self) -> Result<Expr, ParseError> {
self.assignment()
}
fn equality(&mut self) -> Result<Expr,ParseError> {
fn assignment(&mut self) -> Result<Expr, ParseError> {
let expr = self.equality()?;
if self.match_token(&[TokenType::Equal]) {
let value = Box::new(self.assignment()?); // l'assignation a une associativité par la droite
match expr {
Expr::VariableExpr(v) => {
Ok( Expr::AssignExpr(Assign { name: v.name, value }))
}
_ => Err(ParseError { message: String::from("Invalid assignment target") })
}
} else {
Ok(expr)
}
}
fn equality(&mut self) -> Result<Expr, ParseError> {
let mut expr: Expr = self.comparison()?;
while self.match_token( &[TokenType::BangEqual, TokenType::EqualEqual]) {
@@ -112,7 +129,7 @@ impl Parser {
return Ok(expr);
}
fn comparison(&mut self) -> Result<Expr,ParseError> {
fn comparison(&mut self) -> Result<Expr, ParseError> {
let mut expr = self.term()?;
while self.match_token(&[TokenType::Greater, TokenType::GreaterEqual, TokenType::Less, TokenType::LessEqual]) {