Files
simple-rust-tests/__lang/monkey-rs/src/environment.rs
2022-06-02 00:32:03 +08:00

37 lines
894 B
Rust

use crate::object::Object;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
#[derive(Debug, PartialEq, Default)]
pub struct Environment {
store: HashMap<String, Object>,
outer: Option<Rc<RefCell<Environment>>>,
}
impl Environment {
pub fn new() -> Self {
Default::default()
}
pub fn extend(outer: Rc<RefCell<Self>>) -> Environment {
Environment {
store: HashMap::new(),
outer: Some(outer),
}
}
pub fn get(&self, name: &str) -> Option<Object> {
match self.store.get(name) {
Some(value) => Some(value.clone()),
None => self
.outer
.as_ref()
.and_then(|o| o.borrow().get(name).clone()),
}
}
pub fn set(&mut self, name: &str, val: Object) {
self.store.insert(name.to_string(), val);
}
}