feat: add monkey-rs

This commit is contained in:
2022-06-02 00:32:03 +08:00
parent 6a8d8ca043
commit 2330a646b7
13 changed files with 2241 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
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);
}
}