feat: init
This commit is contained in:
47
src/vm/env.rs
Normal file
47
src/vm/env.rs
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user