From f4111577ca3cbfbfdc67491472f39ba0e5a75e2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sat, 31 Dec 2022 16:36:29 +0100 Subject: [PATCH] Interpreter - Variable assignment --- src/interpreter/environment.rs | 6 +++--- src/interpreter/interpretable.rs | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/interpreter/environment.rs b/src/interpreter/environment.rs index 01e1734..b0469a6 100644 --- a/src/interpreter/environment.rs +++ b/src/interpreter/environment.rs @@ -28,10 +28,10 @@ impl Environment { } /// Assign a value to an existing variable. - pub fn assign(&mut self, name: &Token, value: &Value) -> Result<(), InterpreterError> { + pub fn assign(&mut self, name: &Token, value: Value) -> InterpreterResult { if self.values.contains_key(&name.lexeme as &str) { - self.values.insert(name.lexeme.clone(), value.clone()); - Ok(()) + self.values.insert(name.lexeme.clone(), value); + Ok(Value::Nil) } else { Err(InterpreterError::new( name, diff --git a/src/interpreter/interpretable.rs b/src/interpreter/interpretable.rs index 057be0f..517ed3d 100644 --- a/src/interpreter/interpretable.rs +++ b/src/interpreter/interpretable.rs @@ -92,6 +92,10 @@ impl ast::StmtNode { impl Interpretable for ast::ExprNode { fn interprete(&self, environment: &mut Environment) -> InterpreterResult { match self { + ast::ExprNode::Assignment{ name, value} => { + let value = value.interprete(environment)?; + environment.assign(name, value) + } ast::ExprNode::Binary { left, operator,