pub struct Environment {
pub enclosing: Option<Rc<RefCell<Environment>>>,
/* private fields */
}Expand description
Represents an environment in which variables are stored. The environment is a hash map of variable names to their values. Each environment has a reference to its enclosing environment. This is an optional reference to implement lexical scoping and closures.
Fields§
§enclosing: Option<Rc<RefCell<Environment>>>Using an Rc and Refcell here allows us to have multiple mutable references to the same environment.
Implementations§
Source§impl Environment
impl Environment
Sourcepub fn new(enclosing: Option<Rc<RefCell<Environment>>>) -> Self
pub fn new(enclosing: Option<Rc<RefCell<Environment>>>) -> Self
Creates a new environment with the given enclosing environment.
Sourcepub fn define(&mut self, name: &str, value: Object)
pub fn define(&mut self, name: &str, value: Object)
Defines a new variable in the environment with the given name and value.
Sourcepub fn assign(&mut self, name: &Token, value: Object)
pub fn assign(&mut self, name: &Token, value: Object)
Assigns the given value to the variable with the given name. If the variable is not define in this environment but is defined in an enclosing environment, it will try to recursively assign the value to the variable in the enclosing environment. If the variable is not defined in this environment or any enclosing environment, it will throw a runtime error.
Sourcepub fn assign_at(&mut self, distance: usize, name: &Token, value: Object)
pub fn assign_at(&mut self, distance: usize, name: &Token, value: Object)
Works like Environment::assign but assigns the value to the variable in the ancestor
environment at the given distance.
Sourcepub fn get(&self, name: &Token) -> Result<Object, RuntimeError>
pub fn get(&self, name: &Token) -> Result<Object, RuntimeError>
Returns the value of the variable with the given name. If the variable is not defined in this environment but is defined in an enclosing environment, it will try to recursively get the value of the variable in the enclosing environment. If the variable is not defined in this environment or any enclosing environment, it will throw a runtime error.
Sourcepub fn get_at(
&self,
distance: usize,
name: &Token,
) -> Result<Object, RuntimeError>
pub fn get_at( &self, distance: usize, name: &Token, ) -> Result<Object, RuntimeError>
Works like Environment::get but gets the value of the variable in the ancestor
environment at the given distance.
Trait Implementations§
Source§impl Clone for Environment
impl Clone for Environment
Source§fn clone(&self) -> Environment
fn clone(&self) -> Environment
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more