chore: fmt
This commit is contained in:
@@ -45,7 +45,18 @@ impl<'vm> AttrSet<'vm> {
|
||||
self.data = self
|
||||
.data
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k.clone(), match v.clone() { x @ Value::ThunkRef(thunk) => { thunk.capture(env.clone()); x }, x => x }))
|
||||
.map(|(k, v)| {
|
||||
(
|
||||
k.clone(),
|
||||
match v.clone() {
|
||||
x @ Value::ThunkRef(thunk) => {
|
||||
thunk.capture(env.clone());
|
||||
x
|
||||
}
|
||||
x => x,
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
|
||||
@@ -99,7 +110,7 @@ impl ToPublic for AttrSet<'_> {
|
||||
|
||||
#[derive(Debug, Constructor, Clone, PartialEq)]
|
||||
pub struct RecAttrSet<'vm> {
|
||||
data: Rc<Env<'vm>>
|
||||
data: Rc<Env<'vm>>,
|
||||
}
|
||||
|
||||
impl<'vm> RecAttrSet<'vm> {
|
||||
@@ -178,7 +189,8 @@ impl<'vm> RecAttrSet<'vm> {
|
||||
impl ToPublic for RecAttrSet<'_> {
|
||||
fn to_public(self, vm: &VM) -> p::Value {
|
||||
p::Value::AttrSet(p::AttrSet::new(
|
||||
self.data.map
|
||||
self.data
|
||||
.map
|
||||
.borrow()
|
||||
.iter()
|
||||
.map(|(sym, value)| (sym.clone(), value.clone().to_public(vm)))
|
||||
|
||||
@@ -25,77 +25,6 @@ pub use func::*;
|
||||
pub use list::List;
|
||||
pub use primop::*;
|
||||
|
||||
pub trait ToPublic {
|
||||
fn to_public(self, vm: &VM) -> p::Value;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Thunk<'vm> {
|
||||
pub thunk: RefCell<_Thunk<'vm>>,
|
||||
pub env: RefCell<Option<Rc<Env<'vm>>>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, IsVariant, Unwrap, Clone)]
|
||||
pub enum _Thunk<'vm> {
|
||||
Code(OpCodes),
|
||||
SuspendedFrom(*const Thunk<'vm>),
|
||||
Value(Box<Value<'vm>>),
|
||||
}
|
||||
|
||||
impl<'vm> Thunk<'vm> {
|
||||
pub fn new(opcodes: OpCodes) -> Self {
|
||||
Thunk {
|
||||
thunk: RefCell::new(_Thunk::Code(opcodes)),
|
||||
env: RefCell::new(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unwrap_code(&self) -> OpCodes {
|
||||
self.thunk.borrow().clone().unwrap_code()
|
||||
}
|
||||
|
||||
pub fn capture(&self, env: Rc<Env<'vm>>) {
|
||||
*self.env.borrow_mut() = Some(env);
|
||||
}
|
||||
|
||||
pub fn force(&self, vm: &VM<'vm>) -> Result<Value<'vm>> {
|
||||
match &*self.thunk.borrow() {
|
||||
_Thunk::Value(value) => return Ok(value.as_ref().clone()),
|
||||
_Thunk::SuspendedFrom(from) => {
|
||||
return Err(Error::EvalError(format!(
|
||||
"thunk {:p} already suspended from {from:p} (infinite recursion encountered)",
|
||||
self as *const Thunk
|
||||
)));
|
||||
}
|
||||
_Thunk::Code(_) => (),
|
||||
}
|
||||
let opcodes = std::mem::replace(
|
||||
&mut *self.thunk.borrow_mut(),
|
||||
_Thunk::SuspendedFrom(self as *const Thunk),
|
||||
)
|
||||
.unwrap_code();
|
||||
let value = vm.eval(opcodes, self.env.borrow().clone().unwrap())?;
|
||||
let _ = std::mem::replace(
|
||||
&mut *self.thunk.borrow_mut(),
|
||||
_Thunk::Value(value.clone().into()),
|
||||
);
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
pub fn value(&'vm self) -> Option<Value<'vm>> {
|
||||
match &*self.thunk.borrow() {
|
||||
_Thunk::Value(value) => Some(value.as_ref().clone()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Thunk<'_> {
|
||||
fn eq(&self, _: &Self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, IsVariant, Unwrap, Clone, PartialEq)]
|
||||
pub enum Value<'vm> {
|
||||
Const(Const),
|
||||
@@ -484,3 +413,74 @@ impl ToPublic for Value<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ToPublic {
|
||||
fn to_public(self, vm: &VM) -> p::Value;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Thunk<'vm> {
|
||||
pub thunk: RefCell<_Thunk<'vm>>,
|
||||
pub env: RefCell<Option<Rc<Env<'vm>>>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, IsVariant, Unwrap, Clone)]
|
||||
pub enum _Thunk<'vm> {
|
||||
Code(OpCodes),
|
||||
SuspendedFrom(*const Thunk<'vm>),
|
||||
Value(Box<Value<'vm>>),
|
||||
}
|
||||
|
||||
impl<'vm> Thunk<'vm> {
|
||||
pub fn new(opcodes: OpCodes) -> Self {
|
||||
Thunk {
|
||||
thunk: RefCell::new(_Thunk::Code(opcodes)),
|
||||
env: RefCell::new(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unwrap_code(&self) -> OpCodes {
|
||||
self.thunk.borrow().clone().unwrap_code()
|
||||
}
|
||||
|
||||
pub fn capture(&self, env: Rc<Env<'vm>>) {
|
||||
*self.env.borrow_mut() = Some(env);
|
||||
}
|
||||
|
||||
pub fn force(&self, vm: &VM<'vm>) -> Result<Value<'vm>> {
|
||||
match &*self.thunk.borrow() {
|
||||
_Thunk::Value(value) => return Ok(value.as_ref().clone()),
|
||||
_Thunk::SuspendedFrom(from) => {
|
||||
return Err(Error::EvalError(format!(
|
||||
"thunk {:p} already suspended from {from:p} (infinite recursion encountered)",
|
||||
self as *const Thunk
|
||||
)));
|
||||
}
|
||||
_Thunk::Code(_) => (),
|
||||
}
|
||||
let opcodes = std::mem::replace(
|
||||
&mut *self.thunk.borrow_mut(),
|
||||
_Thunk::SuspendedFrom(self as *const Thunk),
|
||||
)
|
||||
.unwrap_code();
|
||||
let value = vm.eval(opcodes, self.env.borrow().clone().unwrap())?;
|
||||
let _ = std::mem::replace(
|
||||
&mut *self.thunk.borrow_mut(),
|
||||
_Thunk::Value(value.clone().into()),
|
||||
);
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
pub fn value(&'vm self) -> Option<Value<'vm>> {
|
||||
match &*self.thunk.borrow() {
|
||||
_Thunk::Value(value) => Some(value.as_ref().clone()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Thunk<'_> {
|
||||
fn eq(&self, _: &Self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user