diff --git a/src/interpreter/environment.rs b/src/interpreter/environment.rs index 6b35d98..01e1734 100644 --- a/src/interpreter/environment.rs +++ b/src/interpreter/environment.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use crate::{errors::InterpreterError, tokens::Token}; -use super::Value; +use super::{InterpreterResult, Value}; /// The execution environment. #[derive(Debug, Default)] @@ -17,7 +17,7 @@ impl Environment { } /// Get the value of a variable. - pub fn get(&self, name: &Token) -> Result { + pub fn get(&self, name: &Token) -> InterpreterResult { match self.values.get(&name.lexeme as &str) { None => Err(InterpreterError::new( name, @@ -26,4 +26,17 @@ impl Environment { Some(value) => Ok(value.clone()), } } + + /// Assign a value to an existing variable. + pub fn assign(&mut self, name: &Token, value: &Value) -> Result<(), InterpreterError> { + if self.values.contains_key(&name.lexeme as &str) { + self.values.insert(name.lexeme.clone(), value.clone()); + Ok(()) + } else { + Err(InterpreterError::new( + name, + &format!("undefined variable '{}'", name.lexeme), + )) + } + } }