feat: better builtins implementaion

get rid of circular references
This commit is contained in:
2025-05-17 18:31:36 +08:00
parent 8480e0891b
commit ff9afd0cc1
19 changed files with 191 additions and 244 deletions

View File

@@ -3,12 +3,12 @@ use std::rc::Rc;
use rpds::HashTrieMap;
use crate::ty::internal::Value;
use crate::ty::internal::{AttrSet, Value};
#[derive(Debug, Default, PartialEq)]
#[derive(Debug, Default)]
pub struct Env<'vm> {
last: RefCell<Option<Rc<Env<'vm>>>>,
pub map: RefCell<HashTrieMap<usize, Value<'vm>>>,
map: RefCell<HashTrieMap<usize, Value<'vm>>>,
}
impl Clone for Env<'_> {
@@ -38,10 +38,10 @@ impl<'vm> Env<'vm> {
self.map.borrow_mut().insert_mut(symbol, value);
}
pub fn enter(&self, new: HashTrieMap<usize, Value<'vm>>) {
pub fn enter(&self, new: impl Iterator<Item = (usize, Value<'vm>)>) {
let mut map = self.map.borrow().clone();
for (k, v) in new.iter() {
map.insert_mut(k.clone(), v.clone());
for (k, v) in new {
map.insert_mut(k, v);
}
let last = Env {
last: self.last.clone(),
@@ -51,19 +51,22 @@ impl<'vm> Env<'vm> {
*self.map.borrow_mut() = map;
}
pub fn enter_rec(self: &mut Rc<Self>, new: Rc<Env<'vm>>) {
let last = (*self.last.borrow_mut()).take();
*self = new;
*self.last.borrow_mut() = last;
}
pub fn new_rec(self: Rc<Self>) -> Rc<Self> {
pub fn enter_with(&self, new: Rc<AttrSet<'vm>>) {
let mut map = self.map.borrow().clone();
for (k, v) in new.as_inner().iter() {
let v = if let Value::Builtins = v {
Value::AttrSet(new.clone())
} else {
v.clone()
};
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.clone()
*self.map.borrow_mut() = map;
}
pub fn leave(&self) {