feat(env): Rc
This commit is contained in:
@@ -1,28 +1,13 @@
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use rpds::HashTrieMap;
|
||||
|
||||
use crate::ty::internal::{AttrSet, Value};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct Env<'vm> {
|
||||
last: RefCell<Option<Rc<Env<'vm>>>>,
|
||||
map: RefCell<HashTrieMap<usize, Value<'vm>>>,
|
||||
}
|
||||
|
||||
impl Clone for Env<'_> {
|
||||
fn clone(&self) -> Self {
|
||||
Env {
|
||||
last: RefCell::new(
|
||||
self.last
|
||||
.borrow()
|
||||
.clone()
|
||||
.map(|e| Rc::new(e.as_ref().clone())),
|
||||
),
|
||||
map: RefCell::new(self.map.borrow().clone()),
|
||||
}
|
||||
}
|
||||
last: Option<Rc<Env<'vm>>>,
|
||||
map: HashTrieMap<usize, Value<'vm>>,
|
||||
}
|
||||
|
||||
impl<'vm> Env<'vm> {
|
||||
@@ -31,28 +16,30 @@ impl<'vm> Env<'vm> {
|
||||
}
|
||||
|
||||
pub fn lookup(&self, symbol: usize) -> Option<Value<'vm>> {
|
||||
self.map.borrow().get(&symbol).cloned()
|
||||
self.map.get(&symbol).cloned()
|
||||
}
|
||||
|
||||
pub fn insert(&self, symbol: usize, value: Value<'vm>) {
|
||||
self.map.borrow_mut().insert_mut(symbol, value);
|
||||
pub fn insert(&mut self, symbol: usize, value: Value<'vm>) {
|
||||
self.map.insert_mut(symbol, value);
|
||||
}
|
||||
|
||||
pub fn enter(&self, new: impl Iterator<Item = (usize, Value<'vm>)>) {
|
||||
let mut map = self.map.borrow().clone();
|
||||
pub fn enter(self, new: impl Iterator<Item = (usize, Value<'vm>)>) -> Self {
|
||||
let mut map = self.map.clone();
|
||||
for (k, v) in new {
|
||||
map.insert_mut(k, v);
|
||||
}
|
||||
let last = Env {
|
||||
last: self.last.clone(),
|
||||
map: self.map.clone(),
|
||||
};
|
||||
*self.last.borrow_mut() = Some(Rc::new(last));
|
||||
*self.map.borrow_mut() = map;
|
||||
let last = Some(
|
||||
Env {
|
||||
last: self.last,
|
||||
map: self.map,
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
Env { last, map }
|
||||
}
|
||||
|
||||
pub fn enter_with(&self, new: Rc<AttrSet<'vm>>) {
|
||||
let mut map = self.map.borrow().clone();
|
||||
pub fn enter_with(self, new: Rc<AttrSet<'vm>>) -> Self {
|
||||
let mut map = self.map.clone();
|
||||
for (k, v) in new.as_inner().iter() {
|
||||
let v = if let Value::Builtins = v {
|
||||
Value::AttrSet(new.clone())
|
||||
@@ -61,18 +48,17 @@ impl<'vm> Env<'vm> {
|
||||
};
|
||||
map.insert_mut(k.clone(), v);
|
||||
}
|
||||
let last = Env {
|
||||
last: self.last.clone(),
|
||||
map: self.map.clone(),
|
||||
};
|
||||
*self.last.borrow_mut() = Some(Rc::new(last));
|
||||
*self.map.borrow_mut() = map;
|
||||
let last = Some(
|
||||
Env {
|
||||
last: self.last.clone(),
|
||||
map: self.map.clone(),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
Env { last, map }
|
||||
}
|
||||
|
||||
pub fn leave(&self) {
|
||||
let last = (*self.last.borrow_mut()).take().unwrap();
|
||||
let _ = std::mem::replace(&mut *self.last.borrow_mut(), last.last.borrow().clone());
|
||||
let map = last.map.borrow().clone();
|
||||
*self.map.borrow_mut() = map;
|
||||
pub fn leave(self) -> Self {
|
||||
self.last.unwrap().as_ref().clone()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user