use std::{cell::RefCell, collections::HashMap, rc::Rc}; use crate::{errors::InterpreterError, tokens::Token}; use super::{native_fn, CallableRef, Value}; /// A mutable reference to an environment. pub type EnvironmentRef = Rc>; /// A variable. pub type Variable = Option; /// The execution environment. #[derive(Debug)] pub struct Environment { enclosing: Option, values: HashMap, } impl Default for Environment { /// Create the default global environment. This includes native functions. fn default() -> Self { let mut env = Self { enclosing: None, values: HashMap::new(), }; env.add_default_fun("clock", native_fn::clock()); env } } impl Environment { /// Create an environment enclosed in another. pub fn create_child(parent: &EnvironmentRef) -> EnvironmentRef { Rc::new(RefCell::new(Self { enclosing: Some(parent.clone()), values: HashMap::default(), })) } /// Add a default function to the environment. fn add_default_fun(&mut self, name: &str, fun: CallableRef) { let value = Some(Value::Callable(fun)); self.values.insert(name.to_owned(), value); } /// Define a new variable. pub fn define(&mut self, name: &Token, value: Variable) -> Result<(), InterpreterError> { if self.values.contains_key(&name.lexeme as &str) { Err(InterpreterError::new( name, &format!("variable '{}' already defined in scope", name.lexeme), )) } else { self.values.insert(name.lexeme.clone(), value); Ok(()) } } /// Get the value of a variable. pub fn get(&self, name: &Token) -> Result { match self.values.get(&name.lexeme as &str) { None => match &self.enclosing { None => Err(InterpreterError::new( name, &format!("undefined variable '{}'", name.lexeme), )), Some(parent) => parent.borrow().get(name), }, Some(None) => Err(InterpreterError::new( name, &format!("variable '{}' has not been initialized", name.lexeme), )), Some(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(), Some(value)); Ok(()) } else { match &mut self.enclosing { None => Err(InterpreterError::new( name, &format!("undefined variable '{}'", name.lexeme), )), Some(parent) => parent.borrow_mut().assign(name, value), } } } }