2022-12-31 17:26:40 +01:00
|
|
|
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
2022-12-31 15:57:54 +01:00
|
|
|
|
|
|
|
use crate::{errors::InterpreterError, tokens::Token};
|
|
|
|
|
2023-01-02 11:25:48 +01:00
|
|
|
use super::Value;
|
2022-12-31 15:57:54 +01:00
|
|
|
|
2022-12-31 17:26:40 +01:00
|
|
|
/// A mutable reference to an environment.
|
|
|
|
pub type EnvironmentRef = Rc<RefCell<Environment>>;
|
|
|
|
|
2023-01-01 10:52:29 +01:00
|
|
|
/// A variable.
|
|
|
|
pub type Variable = Option<Value>;
|
|
|
|
|
2022-12-31 15:57:54 +01:00
|
|
|
/// The execution environment.
|
2023-01-02 17:55:05 +01:00
|
|
|
#[derive(Debug)]
|
2022-12-31 15:57:54 +01:00
|
|
|
pub struct Environment {
|
2022-12-31 17:26:40 +01:00
|
|
|
enclosing: Option<EnvironmentRef>,
|
2023-01-01 10:52:29 +01:00
|
|
|
values: HashMap<String, Variable>,
|
2022-12-31 15:57:54 +01:00
|
|
|
}
|
|
|
|
|
2023-01-02 17:55:05 +01:00
|
|
|
impl Default for Environment {
|
|
|
|
/// Create the default global environment. This includes native functions.
|
|
|
|
fn default() -> Self {
|
|
|
|
let env = Self {
|
|
|
|
enclosing: None,
|
|
|
|
values: HashMap::new(),
|
|
|
|
};
|
|
|
|
env
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-31 15:57:54 +01:00
|
|
|
impl Environment {
|
2022-12-31 16:56:00 +01:00
|
|
|
/// Create an environment enclosed in another.
|
2023-01-01 10:52:29 +01:00
|
|
|
pub fn create_child(parent: &EnvironmentRef) -> EnvironmentRef {
|
2022-12-31 17:26:40 +01:00
|
|
|
Rc::new(RefCell::new(Self {
|
|
|
|
enclosing: Some(parent.clone()),
|
2022-12-31 16:56:00 +01:00
|
|
|
values: HashMap::default(),
|
2022-12-31 17:26:40 +01:00
|
|
|
}))
|
2022-12-31 16:56:00 +01:00
|
|
|
}
|
|
|
|
|
2022-12-31 15:57:54 +01:00
|
|
|
/// Define a new variable.
|
2023-01-01 10:52:29 +01:00
|
|
|
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!("variables '{}' already defined in scope", name.lexeme),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
self.values.insert(name.lexeme.clone(), value);
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-12-31 15:57:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the value of a variable.
|
2023-01-02 11:25:48 +01:00
|
|
|
pub fn get(&self, name: &Token) -> Result<Value, InterpreterError> {
|
2022-12-31 15:57:54 +01:00
|
|
|
match self.values.get(&name.lexeme as &str) {
|
2022-12-31 16:56:00 +01:00
|
|
|
None => match &self.enclosing {
|
|
|
|
None => Err(InterpreterError::new(
|
|
|
|
name,
|
|
|
|
&format!("undefined variable '{}'", name.lexeme),
|
|
|
|
)),
|
2022-12-31 17:26:40 +01:00
|
|
|
Some(parent) => parent.borrow().get(name),
|
2022-12-31 16:56:00 +01:00
|
|
|
},
|
2023-01-01 10:52:29 +01:00
|
|
|
Some(None) => Err(InterpreterError::new(
|
|
|
|
name,
|
|
|
|
&format!("variable '{}' has not been initialized", name.lexeme),
|
|
|
|
)),
|
|
|
|
Some(Some(value)) => Ok(value.clone()),
|
2022-12-31 15:57:54 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-31 16:32:59 +01:00
|
|
|
|
|
|
|
/// Assign a value to an existing variable.
|
2023-01-02 11:25:48 +01:00
|
|
|
pub fn assign(&mut self, name: &Token, value: Value) -> Result<(), InterpreterError> {
|
2022-12-31 16:32:59 +01:00
|
|
|
if self.values.contains_key(&name.lexeme as &str) {
|
2023-01-01 10:52:29 +01:00
|
|
|
self.values.insert(name.lexeme.clone(), Some(value));
|
2023-01-02 11:25:48 +01:00
|
|
|
Ok(())
|
2022-12-31 16:32:59 +01:00
|
|
|
} else {
|
2022-12-31 16:56:00 +01:00
|
|
|
match &mut self.enclosing {
|
|
|
|
None => Err(InterpreterError::new(
|
|
|
|
name,
|
|
|
|
&format!("undefined variable '{}'", name.lexeme),
|
|
|
|
)),
|
2022-12-31 17:26:40 +01:00
|
|
|
Some(parent) => parent.borrow_mut().assign(name, value),
|
2022-12-31 16:56:00 +01:00
|
|
|
}
|
2022-12-31 16:32:59 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-31 15:57:54 +01:00
|
|
|
}
|