rust-crafting-interpreters-.../src/interpreter/environment.rs

61 lines
1.9 KiB
Rust
Raw Normal View History

use std::{cell::RefCell, collections::HashMap, rc::Rc};
use crate::{errors::InterpreterError, tokens::Token};
2022-12-31 16:32:59 +01:00
use super::{InterpreterResult, Value};
/// A mutable reference to an environment.
pub type EnvironmentRef = Rc<RefCell<Environment>>;
/// The execution environment.
#[derive(Debug, Default)]
pub struct Environment {
enclosing: Option<EnvironmentRef>,
values: HashMap<String, Value>,
}
impl Environment {
/// Create an environment enclosed in another.
pub fn create_child(parent: &Rc<RefCell<Self>>) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self {
enclosing: Some(parent.clone()),
values: HashMap::default(),
}))
}
/// Define a new variable.
pub fn define(&mut self, name: String, value: Value) {
self.values.insert(name, value);
}
/// Get the value of a variable.
2022-12-31 16:32:59 +01:00
pub fn get(&self, name: &Token) -> InterpreterResult {
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(value) => Ok(value.clone()),
}
}
2022-12-31 16:32:59 +01:00
/// Assign a value to an existing variable.
2022-12-31 16:36:29 +01:00
pub fn assign(&mut self, name: &Token, value: Value) -> InterpreterResult {
2022-12-31 16:32:59 +01:00
if self.values.contains_key(&name.lexeme as &str) {
2022-12-31 16:36:29 +01:00
self.values.insert(name.lexeme.clone(), value);
Ok(Value::Nil)
2022-12-31 16:32:59 +01:00
} else {
match &mut self.enclosing {
None => Err(InterpreterError::new(
name,
&format!("undefined variable '{}'", name.lexeme),
)),
Some(parent) => parent.borrow_mut().assign(name, value),
}
2022-12-31 16:32:59 +01:00
}
}
}