feat: refactor
This commit is contained in:
@@ -3,7 +3,7 @@ use rpds::HashTrieMapSync;
|
||||
use super::value::{Symbol, VmValue};
|
||||
|
||||
pub struct Env {
|
||||
last: Option<*mut Env>,
|
||||
last: Option<Box<Env>>,
|
||||
map: HashTrieMapSync<Symbol, VmValue>,
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ impl Env {
|
||||
if let Some(value) = self.map.get(&symbol) {
|
||||
value.clone()
|
||||
} else {
|
||||
let last = unsafe { &*self.last.unwrap() };
|
||||
let last = self.last.as_ref().unwrap();
|
||||
last.lookup(symbol)
|
||||
}
|
||||
}
|
||||
@@ -29,19 +29,13 @@ impl Env {
|
||||
}
|
||||
|
||||
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);
|
||||
let last = std::mem::replace(self, Env { last: None, map });
|
||||
self.last = Some(Box::new(last));
|
||||
}
|
||||
|
||||
pub fn leave(&mut self) {
|
||||
let last = unsafe { &*self.last.unwrap() };
|
||||
self.last = last.last;
|
||||
let last = std::mem::replace(&mut self.last, None).unwrap();
|
||||
let _ = std::mem::replace(&mut self.last, last.last);
|
||||
self.map = last.map.clone();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::mem::{size_of, transmute, MaybeUninit};
|
||||
use std::mem::{MaybeUninit, size_of, transmute};
|
||||
use std::ops::Deref;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use anyhow::{Result, anyhow};
|
||||
|
||||
use super::value::VmValue;
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ use rpds::{ht_map_sync, vector_sync};
|
||||
|
||||
use crate::compile::compile;
|
||||
use crate::value::*;
|
||||
use crate::bytecode::Const;
|
||||
|
||||
use super::vm::run;
|
||||
|
||||
|
||||
@@ -12,7 +12,14 @@ pub struct AttrSet {
|
||||
}
|
||||
|
||||
impl AttrSet {
|
||||
pub fn push_attr_force(&mut self, sym: Symbol, val: VmValue) {
|
||||
self.data.insert_mut(sym, val);
|
||||
}
|
||||
|
||||
pub fn push_attr(&mut self, sym: Symbol, val: VmValue) {
|
||||
if self.data.get(&sym).is_some() {
|
||||
todo!()
|
||||
}
|
||||
self.data.insert_mut(sym, val);
|
||||
}
|
||||
|
||||
@@ -26,7 +33,7 @@ impl AttrSet {
|
||||
|
||||
pub fn update(mut self, other: AttrSet) -> AttrSet {
|
||||
for (k, v) in other.data.iter() {
|
||||
self.push_attr(k.clone(), v.clone())
|
||||
self.push_attr_force(k.clone(), v.clone())
|
||||
}
|
||||
self
|
||||
}
|
||||
@@ -41,7 +48,12 @@ impl ToValue for AttrSet {
|
||||
Value::AttrSet(value::AttrSet::new(
|
||||
self.data
|
||||
.iter()
|
||||
.map(|(sym, value)| (value::Symbol::new(sym.0.clone()), value.clone().to_value(vm)))
|
||||
.map(|(sym, value)| {
|
||||
(
|
||||
value::Symbol::new(sym.0.clone()),
|
||||
value.clone().to_value(vm),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
use derive_more::{Constructor, IsVariant, Unwrap};
|
||||
use anyhow::Result;
|
||||
use derive_more::{Constructor, IsVariant, Unwrap};
|
||||
use ecow::EcoString;
|
||||
|
||||
use crate::value::*;
|
||||
use crate::bytecode::Const;
|
||||
|
||||
use super::vm::VM;
|
||||
use super::env::Env;
|
||||
use super::vm::VM;
|
||||
|
||||
mod attrset;
|
||||
mod list;
|
||||
@@ -40,7 +39,7 @@ pub enum VmValue {
|
||||
List(List),
|
||||
Catchable(crate::value::Catchable),
|
||||
PrimOp(crate::builtins::PrimOp),
|
||||
PartialPrimOp(crate::builtins::PartialPrimOp)
|
||||
PartialPrimOp(crate::builtins::PartialPrimOp),
|
||||
}
|
||||
|
||||
use VmValue::Const as VmConst;
|
||||
@@ -49,7 +48,7 @@ impl VmValue {
|
||||
match self {
|
||||
VmValue::PrimOp(func) => func.call(args),
|
||||
VmValue::PartialPrimOp(func) => func.call(args),
|
||||
_ => todo!()
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +89,7 @@ impl VmValue {
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => a < b as f64,
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => a < b,
|
||||
(VmConst(String(a)), VmConst(String(b))) => a < b,
|
||||
_ => todo!()
|
||||
_ => todo!(),
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -144,7 +143,9 @@ impl VmValue {
|
||||
}
|
||||
|
||||
pub fn concat_string(&mut self, mut other: VmValue) -> &mut Self {
|
||||
if let (VmConst(Const::String(a)), VmConst(Const::String(b))) = (self.coerce_to_string(), other.coerce_to_string()) {
|
||||
if let (VmConst(Const::String(a)), VmConst(Const::String(b))) =
|
||||
(self.coerce_to_string(), other.coerce_to_string())
|
||||
{
|
||||
a.push_str(b.as_str());
|
||||
} else {
|
||||
todo!()
|
||||
@@ -190,7 +191,9 @@ impl VmValue {
|
||||
if let VmValue::AttrSet(attrs) = self {
|
||||
let val = attrs
|
||||
.select(sym.clone())
|
||||
.unwrap_or(VmValue::Catchable(Catchable::new(Some(format!("{sym:?} not found")))));
|
||||
.unwrap_or(VmValue::Catchable(Catchable::new(Some(format!(
|
||||
"{sym:?} not found"
|
||||
)))));
|
||||
*self = val;
|
||||
} else {
|
||||
todo!()
|
||||
|
||||
30
src/vm/vm.rs
30
src/vm/vm.rs
@@ -1,13 +1,12 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use anyhow::Result;
|
||||
use rpds::{HashTrieMap, HashTrieMapSync, Vector};
|
||||
|
||||
use crate::bytecode::{self, *};
|
||||
use crate::slice::*;
|
||||
use crate::value::{self, Value};
|
||||
use crate::builtins::env;
|
||||
use crate::bytecode::{self, *};
|
||||
use crate::value::{Const, Value};
|
||||
|
||||
use super::env::Env;
|
||||
use super::stack::{Stack, STACK_SIZE};
|
||||
use super::stack::{STACK_SIZE, Stack};
|
||||
use super::value::{self as vmValue, *};
|
||||
use super::vmthunk::*;
|
||||
|
||||
@@ -17,7 +16,7 @@ pub fn run(prog: Program) -> Result<Value> {
|
||||
}
|
||||
|
||||
pub struct VM {
|
||||
thunks: Slice<VmThunk>,
|
||||
thunks: Box<[VmThunk]>,
|
||||
}
|
||||
|
||||
impl VM {
|
||||
@@ -26,9 +25,7 @@ impl VM {
|
||||
.into_iter()
|
||||
.map(|bytecode::Thunk { opcodes }| VmThunk::new(opcodes))
|
||||
.collect();
|
||||
VM {
|
||||
thunks,
|
||||
}
|
||||
VM { thunks }
|
||||
}
|
||||
|
||||
pub fn get_thunk_value(&self, idx: usize, env: &mut Env) -> Result<VmValue> {
|
||||
@@ -77,7 +74,7 @@ impl VM {
|
||||
}
|
||||
}
|
||||
OpCode::Call { arity } => {
|
||||
let mut args = Vec::with_capacity(arity);
|
||||
let mut args = Vec::with_capacity(arity);
|
||||
for _ in 0..arity {
|
||||
args.insert(0, stack.pop()?);
|
||||
}
|
||||
@@ -139,9 +136,10 @@ impl VM {
|
||||
.select_with_default(Symbol::new(sym), default.clone());
|
||||
}
|
||||
OpCode::SelectOrEmpty { sym } => {
|
||||
stack
|
||||
.tos_mut()?
|
||||
.select_with_default(Symbol::new(sym), VmValue::AttrSet(AttrSet::new(HashTrieMapSync::new_sync())));
|
||||
stack.tos_mut()?.select_with_default(
|
||||
Symbol::new(sym),
|
||||
VmValue::AttrSet(AttrSet::new(HashTrieMapSync::new_sync())),
|
||||
);
|
||||
}
|
||||
OpCode::SelectDynamic => {
|
||||
let mut val = stack.pop().unwrap();
|
||||
@@ -160,7 +158,10 @@ impl VM {
|
||||
let mut val = stack.pop().unwrap();
|
||||
val.coerce_to_string();
|
||||
let sym = val.unwrap_const().unwrap_string().into();
|
||||
stack.tos_mut()?.select_with_default(sym, VmValue::AttrSet(AttrSet::new(HashTrieMapSync::new_sync())));
|
||||
stack.tos_mut()?.select_with_default(
|
||||
sym,
|
||||
VmValue::AttrSet(AttrSet::new(HashTrieMapSync::new_sync())),
|
||||
);
|
||||
}
|
||||
OpCode::HasAttr { sym } => {
|
||||
stack.tos_mut()?.has_attr(Symbol::new(sym));
|
||||
@@ -185,4 +186,3 @@ impl VM {
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
use std::cell::RefCell;
|
||||
use std::sync::RwLock;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use anyhow::{Result, anyhow};
|
||||
use derive_more::{IsVariant, Unwrap};
|
||||
|
||||
use crate::bytecode::OpCodes;
|
||||
|
||||
use super::vm::VM;
|
||||
use super::env::Env;
|
||||
use super::value::VmValue;
|
||||
use super::vm::VM;
|
||||
|
||||
pub struct VmThunk {
|
||||
thunk: RefCell<_VmThunk>,
|
||||
lock: RwLock<()>
|
||||
lock: RwLock<()>,
|
||||
}
|
||||
|
||||
#[derive(IsVariant, Unwrap)]
|
||||
@@ -26,7 +26,7 @@ impl VmThunk {
|
||||
pub fn new(opcodes: OpCodes) -> VmThunk {
|
||||
VmThunk {
|
||||
thunk: RefCell::new(_VmThunk::Code(opcodes)),
|
||||
lock: RwLock::new(())
|
||||
lock: RwLock::new(()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,8 +37,8 @@ impl VmThunk {
|
||||
_VmThunk::Value(value) => return Ok(value.clone()),
|
||||
_VmThunk::SuspendedFrom(from) => {
|
||||
return Err(anyhow!(
|
||||
"already suspended from {from:p} (infinite recursion encountered)"
|
||||
))
|
||||
"already suspended from {from:p} (infinite recursion encountered)"
|
||||
));
|
||||
}
|
||||
_VmThunk::Code(_) => (),
|
||||
}
|
||||
@@ -48,9 +48,13 @@ impl VmThunk {
|
||||
let opcodes = std::mem::replace(
|
||||
&mut *self.thunk.borrow_mut(),
|
||||
_VmThunk::SuspendedFrom(self as *const VmThunk),
|
||||
).unwrap_code();
|
||||
)
|
||||
.unwrap_code();
|
||||
let value = vm.eval(opcodes, env).unwrap();
|
||||
let _ = std::mem::replace(&mut *self.thunk.borrow_mut(), _VmThunk::Value(value.clone()));
|
||||
let _ = std::mem::replace(
|
||||
&mut *self.thunk.borrow_mut(),
|
||||
_VmThunk::Value(value.clone()),
|
||||
);
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user