feat: init

This commit is contained in:
2025-05-03 12:49:48 +08:00
commit 5e35da49ef
27 changed files with 3390 additions and 0 deletions

47
src/vm/env.rs Normal file
View File

@@ -0,0 +1,47 @@
use rpds::HashTrieMapSync;
use super::value::{Symbol, VmValue};
pub struct Env {
last: Option<*mut Env>,
map: HashTrieMapSync<Symbol, VmValue>,
}
impl Env {
pub fn empty() -> Env {
Env {
last: None,
map: HashTrieMapSync::new_sync(),
}
}
pub fn lookup(&self, symbol: Symbol) -> VmValue {
if let Some(value) = self.map.get(&symbol) {
value.clone()
} else {
let last = unsafe { &*self.last.unwrap() };
last.lookup(symbol)
}
}
pub fn insert(&mut self, symbol: Symbol, value: VmValue) {
self.map.insert_mut(symbol, value);
}
pub fn enter(&mut self, map: HashTrieMapSync<Symbol, VmValue>) {
let last = std::mem::replace(
self,
Env {
last: None,
map,
},
);
self.last = Some(Box::leak(Box::new(last)) as *mut Env);
}
pub fn leave(&mut self) {
let last = unsafe { &*self.last.unwrap() };
self.last = last.last;
self.map = last.map.clone();
}
}