rust_lisp/model/
env.rs

1use std::cell::RefCell;
2use std::rc::Rc;
3use std::{collections::HashMap, fmt::Debug};
4
5use super::{RuntimeError, Symbol, Value};
6
7/// An environment of symbol bindings. Used for the base environment, for
8/// closures, for `let` statements, for function arguments, etc.
9#[derive(Debug)]
10pub struct Env {
11    parent: Option<Rc<RefCell<Env>>>,
12    entries: HashMap<Symbol, Value>,
13}
14
15impl Env {
16    /// Create a new, empty environment
17    pub fn new() -> Self {
18        Self {
19            parent: None,
20            entries: HashMap::new(),
21        }
22    }
23
24    /// Create a new environment extending the given environment
25    pub fn extend(parent: Rc<RefCell<Env>>) -> Self {
26        Self {
27            parent: Some(parent),
28            entries: HashMap::new(),
29        }
30    }
31
32    /// Walks up the environment hierarchy until it finds the symbol's value or
33    /// runs out of environments.
34    pub fn get(&self, key: &Symbol) -> Option<Value> {
35        if let Some(val) = self.entries.get(&key) {
36            Some(val.clone()) // clone the Rc
37        } else if let Some(parent) = &self.parent {
38            parent.borrow().get(key)
39        } else {
40            None
41        }
42    }
43
44    /// Define a new key in the current environment
45    pub fn define(&mut self, key: Symbol, value: Value) {
46        self.entries.insert(key, value);
47    }
48
49    /// Find the environment where this key is defined, and update its value.
50    /// Returns an Err if the symbol has not been defined anywhere in the hierarchy.
51    pub fn set(&mut self, key: Symbol, value: Value) -> Result<(), RuntimeError> {
52        if self.entries.contains_key(&key) {
53            self.entries.insert(key, value);
54            Ok(())
55        } else if let Some(parent) = &self.parent {
56            parent.borrow_mut().set(key, value)
57        } else {
58            Err(RuntimeError {
59                msg: format!("Tried to set value of undefined symbol \"{}\"", key),
60            })
61        }
62    }
63
64    /// Delete the nearest (going upwards) definition of this key
65    pub fn undefine(&mut self, key: &Symbol) {
66        if self.entries.contains_key(key) {
67            self.entries.remove(key);
68        } else if let Some(parent) = &self.parent {
69            parent.borrow_mut().undefine(key);
70        }
71    }
72
73    fn display_recursive(&self, output: &mut String, depth: i32) {
74        let indent = &(0..depth).map(|_| "  ").collect::<String>();
75
76        output.push_str(indent);
77        output.push_str("{ ");
78
79        for (symbol, value) in &self.entries {
80            output.push_str(format!("\n{}  {}: {}", indent, symbol, value).as_str());
81        }
82
83        if let Some(parent) = &self.parent {
84            output.push_str("\n\n");
85            parent
86                .as_ref()
87                .borrow()
88                .display_recursive(output, depth + 1);
89        }
90
91        output.push('\n');
92        output.push_str(indent);
93        output.push('}');
94    }
95}
96
97impl std::fmt::Display for Env {
98    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
99        let mut output = String::new();
100
101        output.push_str("Env: ");
102        self.display_recursive(&mut output, 0);
103
104        write!(formatter, "{}", &output)
105    }
106}