feat(value): less clone
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::ty::internal::{_Thunk, Const, PrimOp, RecAttrSet, Thunk, Value};
|
use crate::ty::internal::{Const, PrimOp, RecAttrSet, Value};
|
||||||
use crate::vm::{Env, VM};
|
use crate::vm::{Env, VM};
|
||||||
|
|
||||||
pub fn env<'vm>(vm: &'vm VM) -> Rc<Env<'vm>> {
|
pub fn env<'vm>(vm: &'vm VM) -> Rc<Env<'vm>> {
|
||||||
@@ -45,17 +44,15 @@ pub fn env<'vm>(vm: &'vm VM) -> Rc<Env<'vm>> {
|
|||||||
let builtins_env = Rc::new(Env::empty());
|
let builtins_env = Rc::new(Env::empty());
|
||||||
let map = builtins_env.clone().new_rec();
|
let map = builtins_env.clone().new_rec();
|
||||||
for primop in primops {
|
for primop in primops {
|
||||||
|
let primop = Rc::new(primop);
|
||||||
env.insert(
|
env.insert(
|
||||||
vm.new_sym(format!("__{}", primop.name)),
|
vm.new_sym(format!("__{}", primop.name)),
|
||||||
Value::PrimOp(primop.clone()),
|
Value::PrimOp(primop.clone()),
|
||||||
);
|
);
|
||||||
map.insert(vm.new_sym(primop.name), Value::PrimOp(primop));
|
map.insert(vm.new_sym(primop.name), Value::PrimOp(primop));
|
||||||
}
|
}
|
||||||
let builtins = Value::RecAttrSet(RecAttrSet::from_inner(map.clone()));
|
let builtins = Value::RecAttrSet(RecAttrSet::from_inner(map.clone()).into());
|
||||||
let thunk = Thunk {
|
map.insert(vm.new_sym("builtins"), builtins.clone());
|
||||||
thunk: Rc::new(RefCell::new(_Thunk::Value(Box::new(builtins.clone())))),
|
|
||||||
};
|
|
||||||
map.insert(vm.new_sym("builtins"), Value::Thunk(thunk));
|
|
||||||
|
|
||||||
env.insert(vm.new_sym("builtins"), builtins);
|
env.insert(vm.new_sym("builtins"), builtins);
|
||||||
env
|
env
|
||||||
|
|||||||
@@ -685,7 +685,7 @@ fn downgrade_inherit(
|
|||||||
};
|
};
|
||||||
for attr in inherit.attrs() {
|
for attr in inherit.attrs() {
|
||||||
let ident = match downgrade_attr(attr, ctx)? {
|
let ident = match downgrade_attr(attr, ctx)? {
|
||||||
Attr::Str(ident) => ctx.new_sym(ident.to_string()),
|
Attr::Str(ident) => ident,
|
||||||
_ => {
|
_ => {
|
||||||
return Err(Error::DowngradeError(
|
return Err(Error::DowngradeError(
|
||||||
"dynamic attributes not allowed in inherit".to_string(),
|
"dynamic attributes not allowed in inherit".to_string(),
|
||||||
@@ -753,5 +753,9 @@ fn downgrade_attrpathvalue(
|
|||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let path = downgrade_attrpath(value.attrpath().unwrap(), ctx)?;
|
let path = downgrade_attrpath(value.attrpath().unwrap(), ctx)?;
|
||||||
let value = value.value().unwrap().downgrade(ctx)?;
|
let value = value.value().unwrap().downgrade(ctx)?;
|
||||||
attrs.insert(path, ctx.new_thunk(value).ir())
|
let value = match value {
|
||||||
|
x @ Ir::Const(_) | x @ Ir::Var(_) => x,
|
||||||
|
x => ctx.new_thunk(x).ir()
|
||||||
|
};
|
||||||
|
attrs.insert(path, value)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use std::fmt::{Display, Formatter, Result as FmtResult};
|
|||||||
use derive_more::Constructor;
|
use derive_more::Constructor;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Constructor)]
|
#[derive(Clone, Debug, PartialEq, Constructor, Hash)]
|
||||||
pub struct Catchable {
|
pub struct Catchable {
|
||||||
msg: String,
|
msg: String,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use derive_more::Constructor;
|
use derive_more::Constructor;
|
||||||
use rpds::HashTrieMap;
|
use rpds::HashTrieMap;
|
||||||
@@ -6,7 +7,7 @@ use rpds::HashTrieMap;
|
|||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::vm::{Env, VM};
|
use crate::vm::{Env, VM};
|
||||||
use super::super::public as p;
|
use super::super::public as p;
|
||||||
use super::{ToPublic, Value};
|
use super::Value;
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Debug, Constructor, Clone, PartialEq)]
|
#[derive(Debug, Constructor, Clone, PartialEq)]
|
||||||
@@ -54,22 +55,16 @@ impl<'vm> AttrSet<'vm> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(mut self, other: AttrSet<'vm>) -> AttrSet<'vm> {
|
pub fn update(&mut self, other: &AttrSet<'vm>) {
|
||||||
for (k, v) in other.data.iter() {
|
for (k, v) in other.data.iter() {
|
||||||
self.push_attr_force(k.clone(), v.clone())
|
self.push_attr_force(k.clone(), v.clone())
|
||||||
}
|
}
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_rec(mut self, other: RecAttrSet<'vm>) -> AttrSet<'vm> {
|
pub fn update_rec(&mut self, other: &RecAttrSet<'vm>) {
|
||||||
for (k, v) in other.data.map.borrow().iter() {
|
for (k, v) in other.data.map.borrow().iter() {
|
||||||
self.push_attr_force(k.clone(), v.clone())
|
self.push_attr_force(k.clone(), v.clone())
|
||||||
}
|
}
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn into_inner(self) -> HashTrieMap<usize, Value<'vm>> {
|
|
||||||
self.data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_inner(&self) -> &HashTrieMap<usize, Value<'vm>> {
|
pub fn as_inner(&self) -> &HashTrieMap<usize, Value<'vm>> {
|
||||||
@@ -82,21 +77,18 @@ impl<'vm> AttrSet<'vm> {
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(k, v)| (k.clone(), v.clone()))
|
.map(|(k, v)| (k.clone(), v.clone()))
|
||||||
.collect();
|
.collect();
|
||||||
map.iter_mut()
|
for (_, v) in map.iter_mut() {
|
||||||
.map(|(_, v)| v.force_deep(vm).map(|_| ()))
|
v.force_deep(vm)?;
|
||||||
.find(|v| v.is_err())
|
}
|
||||||
.map_or(Ok(()), |err| err)?;
|
|
||||||
self.data = map.into_iter().collect();
|
self.data = map.into_iter().collect();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl ToPublic for AttrSet<'_> {
|
pub fn to_public(&self, vm: &'vm VM, seen: &mut HashSet<Value<'vm>>) -> p::Value {
|
||||||
fn to_public(self, vm: &VM) -> p::Value {
|
|
||||||
p::Value::AttrSet(p::AttrSet::new(
|
p::Value::AttrSet(p::AttrSet::new(
|
||||||
self.data
|
self.data
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(&sym, value)| (vm.get_sym(sym), value.clone().to_public(vm)))
|
.map(|(&sym, value)| (vm.get_sym(sym), value.clone().to_public(vm, seen)))
|
||||||
.collect(),
|
.collect(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@@ -133,14 +125,13 @@ impl<'vm> RecAttrSet<'vm> {
|
|||||||
self.data.lookup(sym).is_some()
|
self.data.lookup(sym).is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(mut self, other: RecAttrSet<'vm>) -> RecAttrSet<'vm> {
|
pub fn update(&mut self, other: RecAttrSet<'vm>) {
|
||||||
for (k, v) in other.data.map.borrow().iter() {
|
for (k, v) in other.data.map.borrow().iter() {
|
||||||
self.push_attr_force(k.clone(), v.clone())
|
self.push_attr_force(k.clone(), v.clone())
|
||||||
}
|
}
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_normal(self, other: AttrSet<'vm>) -> AttrSet<'vm> {
|
pub fn update_normal(&self, other: &AttrSet<'vm>) -> AttrSet<'vm> {
|
||||||
let map = self
|
let map = self
|
||||||
.data
|
.data
|
||||||
.map
|
.map
|
||||||
@@ -178,16 +169,14 @@ impl<'vm> RecAttrSet<'vm> {
|
|||||||
*self.data.map.borrow_mut() = map.into_iter().collect();
|
*self.data.map.borrow_mut() = map.into_iter().collect();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl ToPublic for RecAttrSet<'_> {
|
pub fn to_public(&self, vm: &'vm VM, seen: &mut HashSet<Value<'vm>>) -> p::Value {
|
||||||
fn to_public(self, vm: &VM) -> p::Value {
|
|
||||||
p::Value::AttrSet(p::AttrSet::new(
|
p::Value::AttrSet(p::AttrSet::new(
|
||||||
self.data
|
self.data
|
||||||
.map
|
.map
|
||||||
.borrow()
|
.borrow()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(&sym, value)| (vm.get_sym(sym), value.clone().to_public(vm)))
|
.map(|(&sym, value)| (vm.get_sym(sym), value.clone().to_public(vm, seen)))
|
||||||
.collect(),
|
.collect(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ pub struct Func<'vm> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'vm> Func<'vm> {
|
impl<'vm> Func<'vm> {
|
||||||
pub fn call(self, vm: &'vm VM<'_>, arg: Value<'vm>) -> Result<Value<'vm>> {
|
pub fn call(&self, vm: &'vm VM<'_>, arg: Value<'vm>) -> Result<Value<'vm>> {
|
||||||
use Param::*;
|
use Param::*;
|
||||||
|
|
||||||
let env = Rc::new(self.env.as_ref().clone());
|
let env = Rc::new(self.env.as_ref().clone());
|
||||||
@@ -78,7 +78,7 @@ impl<'vm> Func<'vm> {
|
|||||||
let formal = formal.clone().into();
|
let formal = formal.clone().into();
|
||||||
let arg = arg
|
let arg = arg
|
||||||
.select(formal)
|
.select(formal)
|
||||||
.or_else(|| default.map(|idx| Value::Thunk(Thunk::new(vm.get_thunk(idx)))))
|
.or_else(|| default.map(|idx| Value::Thunk(Thunk::new(vm.get_thunk(idx)).into())))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
new.insert_mut(formal, arg);
|
new.insert_mut(formal, arg);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,23 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use derive_more::Constructor;
|
use derive_more::Constructor;
|
||||||
use rpds::VectorSync;
|
use rpds::Vector;
|
||||||
|
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::ty::public as p;
|
use crate::ty::public as p;
|
||||||
use crate::vm::VM;
|
use crate::vm::VM;
|
||||||
|
|
||||||
use super::{ToPublic, Value};
|
use super::Value;
|
||||||
|
|
||||||
#[derive(Debug, Constructor, Clone, PartialEq)]
|
#[derive(Debug, Constructor, Clone, PartialEq)]
|
||||||
pub struct List<'vm> {
|
pub struct List<'vm> {
|
||||||
data: VectorSync<Value<'vm>>,
|
data: Vector<Value<'vm>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'vm> List<'vm> {
|
impl<'vm> List<'vm> {
|
||||||
pub fn empty() -> Self {
|
pub fn empty() -> Self {
|
||||||
List {
|
List {
|
||||||
data: VectorSync::new_sync(),
|
data: Vector::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,30 +25,26 @@ impl<'vm> List<'vm> {
|
|||||||
self.data.push_back_mut(elem);
|
self.data.push_back_mut(elem);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn concat(mut self, other: List<'vm>) -> List<'vm> {
|
pub fn concat(&mut self, other: &List<'vm>) {
|
||||||
for elem in other.data.iter() {
|
for elem in other.data.iter() {
|
||||||
self.data.push_back_mut(elem.clone());
|
self.data.push_back_mut(elem.clone());
|
||||||
}
|
}
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn force_deep(&mut self, vm: &'vm VM<'_>) -> Result<()> {
|
pub fn force_deep(&mut self, vm: &'vm VM<'_>) -> Result<()> {
|
||||||
let mut vec: Vec<_> = self.data.iter().cloned().collect();
|
let mut vec: Vec<_> = self.data.iter().cloned().collect();
|
||||||
vec.iter_mut()
|
for v in vec.iter_mut() {
|
||||||
.map(|v| v.force_deep(vm).map(|_| ()))
|
v.force_deep(vm)?;
|
||||||
.find(|v| v.is_err())
|
}
|
||||||
.map_or(Ok(()), |err| err)?;
|
|
||||||
self.data = vec.into_iter().collect();
|
self.data = vec.into_iter().collect();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl ToPublic for List<'_> {
|
pub fn to_public(&self, vm: &'vm VM, seen: &mut HashSet<Value<'vm>>) -> p::Value {
|
||||||
fn to_public(self, vm: &VM) -> p::Value {
|
|
||||||
p::Value::List(p::List::new(
|
p::Value::List(p::List::new(
|
||||||
self.data
|
self.data
|
||||||
.iter()
|
.iter()
|
||||||
.map(|value| value.clone().to_public(vm))
|
.map(|value| value.clone().to_public(vm, seen))
|
||||||
.collect(),
|
.collect(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
use std::cell::OnceCell;
|
use std::cell::OnceCell;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
use std::hash::Hash;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use derive_more::{IsVariant, Unwrap};
|
use derive_more::{IsVariant, Unwrap};
|
||||||
@@ -27,17 +29,37 @@ pub use primop::*;
|
|||||||
#[derive(Debug, IsVariant, Unwrap, Clone, PartialEq)]
|
#[derive(Debug, IsVariant, Unwrap, Clone, PartialEq)]
|
||||||
pub enum Value<'vm> {
|
pub enum Value<'vm> {
|
||||||
Const(Const),
|
Const(Const),
|
||||||
Thunk(Thunk<'vm>),
|
Thunk(Rc<Thunk<'vm>>),
|
||||||
ThunkRef(&'vm Thunk<'vm>),
|
ThunkRef(&'vm Thunk<'vm>),
|
||||||
AttrSet(AttrSet<'vm>),
|
AttrSet(Rc<AttrSet<'vm>>),
|
||||||
RecAttrSet(RecAttrSet<'vm>),
|
RecAttrSet(Rc<RecAttrSet<'vm>>),
|
||||||
List(List<'vm>),
|
List(Rc<List<'vm>>),
|
||||||
Catchable(c::Catchable),
|
Catchable(c::Catchable),
|
||||||
PrimOp(PrimOp<'vm>),
|
PrimOp(Rc<PrimOp<'vm>>),
|
||||||
PartialPrimOp(PartialPrimOp<'vm>),
|
PartialPrimOp(Rc<PartialPrimOp<'vm>>),
|
||||||
Func(Func<'vm>),
|
Func(Rc<Func<'vm>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Hash for Value<'_> {
|
||||||
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||||
|
use Value::*;
|
||||||
|
match self {
|
||||||
|
Const(x) => x.hash(state),
|
||||||
|
Thunk(x) => (x.as_ref() as *const self::Thunk).hash(state),
|
||||||
|
ThunkRef(x) => (*x as *const self::Thunk).hash(state),
|
||||||
|
AttrSet(x) => (x.as_ref() as *const self::AttrSet).hash(state),
|
||||||
|
RecAttrSet(x) => (x.as_ref() as *const self::RecAttrSet).hash(state),
|
||||||
|
List(x) => (x.as_ref() as *const self::List).hash(state),
|
||||||
|
Catchable(x) => x.hash(state),
|
||||||
|
PrimOp(x) => (x.as_ref() as *const self::PrimOp).hash(state),
|
||||||
|
PartialPrimOp(x) => (x.as_ref() as *const self::PartialPrimOp).hash(state),
|
||||||
|
Func(x) => (x.as_ref() as *const self::Func).hash(state),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for Value<'_> {}
|
||||||
|
|
||||||
#[derive(Debug, IsVariant, Unwrap, Clone, PartialEq)]
|
#[derive(Debug, IsVariant, Unwrap, Clone, PartialEq)]
|
||||||
pub enum ValueAsRef<'v, 'vm: 'v> {
|
pub enum ValueAsRef<'v, 'vm: 'v> {
|
||||||
Const(&'v Const),
|
Const(&'v Const),
|
||||||
@@ -89,12 +111,12 @@ impl<'v, 'vm: 'v> Value<'vm> {
|
|||||||
Const(x) => M::Const(x),
|
Const(x) => M::Const(x),
|
||||||
Thunk(x) => M::Thunk(x),
|
Thunk(x) => M::Thunk(x),
|
||||||
ThunkRef(x) => M::Thunk(x),
|
ThunkRef(x) => M::Thunk(x),
|
||||||
AttrSet(x) => M::AttrSet(x),
|
AttrSet(x) => M::AttrSet(Rc::make_mut(x)),
|
||||||
RecAttrSet(x) => M::RecAttrSet(x),
|
RecAttrSet(x) => M::RecAttrSet(Rc::make_mut(x)),
|
||||||
List(x) => M::List(x),
|
List(x) => M::List(Rc::make_mut(x)),
|
||||||
Catchable(x) => M::Catchable(x),
|
Catchable(x) => M::Catchable(x),
|
||||||
PrimOp(x) => M::PrimOp(x),
|
PrimOp(x) => M::PrimOp(Rc::make_mut(x)),
|
||||||
PartialPrimOp(x) => M::PartialPrimOp(x),
|
PartialPrimOp(x) => M::PartialPrimOp(Rc::make_mut(x)),
|
||||||
Func(x) => M::Func(x),
|
Func(x) => M::Func(x),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -130,13 +152,14 @@ impl<'vm> Value<'vm> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn call(self, vm: &'vm VM<'_>, args: Vec<Self>) -> Result<Self> {
|
pub fn call(&self, vm: &'vm VM<'_>, args: Vec<Self>) -> Result<Self> {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
match self {
|
match self {
|
||||||
PrimOp(func) => func.call(vm, args),
|
PrimOp(func) => func.call(vm, args),
|
||||||
PartialPrimOp(func) => func.call(vm, args),
|
PartialPrimOp(func) => func.call(vm, args),
|
||||||
mut func @ Value::Func(_) => {
|
func @ Value::Func(_) => {
|
||||||
let mut iter = args.into_iter();
|
let mut iter = args.into_iter();
|
||||||
|
let mut func = func.clone();
|
||||||
while let Some(arg) = iter.next() {
|
while let Some(arg) = iter.next() {
|
||||||
func = match func {
|
func = match func {
|
||||||
PrimOp(func) => {
|
PrimOp(func) => {
|
||||||
@@ -151,7 +174,7 @@ impl<'vm> Value<'vm> {
|
|||||||
}
|
}
|
||||||
func.ok()
|
func.ok()
|
||||||
}
|
}
|
||||||
x @ Catchable(_) => x.ok(),
|
x @ Catchable(_) => x.clone().ok(),
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -247,7 +270,10 @@ impl<'vm> Value<'vm> {
|
|||||||
use Const::*;
|
use Const::*;
|
||||||
Ok(match (self, other) {
|
Ok(match (self, other) {
|
||||||
(_, VmConst(Int(0))) => return Err(Error::EvalError("division by zero".to_string())),
|
(_, VmConst(Int(0))) => return Err(Error::EvalError("division by zero".to_string())),
|
||||||
(_, VmConst(Float(0.))) => return Err(Error::EvalError("division by zero".to_string())), (VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a / b)),
|
(_, VmConst(Float(0.))) => {
|
||||||
|
return Err(Error::EvalError("division by zero".to_string()));
|
||||||
|
}
|
||||||
|
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a / b)),
|
||||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(a as f64 / b)),
|
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(a as f64 / b)),
|
||||||
(VmConst(Float(a)), VmConst(Int(b))) => VmConst(Float(a / b as f64)),
|
(VmConst(Float(a)), VmConst(Int(b))) => VmConst(Float(a / b as f64)),
|
||||||
(VmConst(Float(a)), VmConst(Float(b))) => VmConst(Float(a / b)),
|
(VmConst(Float(a)), VmConst(Float(b))) => VmConst(Float(a / b)),
|
||||||
@@ -268,7 +294,7 @@ impl<'vm> Value<'vm> {
|
|||||||
|
|
||||||
pub fn push(&mut self, elem: Self) -> &mut Self {
|
pub fn push(&mut self, elem: Self) -> &mut Self {
|
||||||
if let Value::List(list) = self {
|
if let Value::List(list) = self {
|
||||||
list.push(elem);
|
Rc::make_mut(list).push(elem);
|
||||||
} else if let Value::Catchable(_) = self {
|
} else if let Value::Catchable(_) = self {
|
||||||
} else if let Value::Catchable(_) = elem {
|
} else if let Value::Catchable(_) = elem {
|
||||||
*self = elem;
|
*self = elem;
|
||||||
@@ -280,7 +306,10 @@ impl<'vm> Value<'vm> {
|
|||||||
|
|
||||||
pub fn concat(self, other: Self) -> Self {
|
pub fn concat(self, other: Self) -> Self {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(Value::List(a), Value::List(b)) => Value::List(a.concat(b)),
|
(Value::List(mut a), Value::List(b)) => {
|
||||||
|
Rc::make_mut(&mut a).concat(b.as_ref());
|
||||||
|
Value::List(a)
|
||||||
|
}
|
||||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
}
|
}
|
||||||
@@ -288,9 +317,9 @@ impl<'vm> Value<'vm> {
|
|||||||
|
|
||||||
pub fn push_attr(&mut self, sym: usize, val: Self) -> &mut Self {
|
pub fn push_attr(&mut self, sym: usize, val: Self) -> &mut Self {
|
||||||
if let Value::AttrSet(attrs) = self {
|
if let Value::AttrSet(attrs) = self {
|
||||||
attrs.push_attr(sym, val)
|
Rc::make_mut(attrs).push_attr(sym, val)
|
||||||
} else if let Value::RecAttrSet(attrs) = self {
|
} else if let Value::RecAttrSet(attrs) = self {
|
||||||
attrs.push_attr(sym, val)
|
Rc::make_mut(attrs).push_attr(sym, val)
|
||||||
} else if let Value::Catchable(_) = self {
|
} else if let Value::Catchable(_) = self {
|
||||||
} else if let Value::Catchable(_) = val {
|
} else if let Value::Catchable(_) = val {
|
||||||
*self = val
|
*self = val
|
||||||
@@ -302,22 +331,30 @@ impl<'vm> Value<'vm> {
|
|||||||
|
|
||||||
pub fn update(self, other: Self) -> Self {
|
pub fn update(self, other: Self) -> Self {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(Value::AttrSet(a), Value::AttrSet(b)) => Value::AttrSet(a.update(b)),
|
(Value::AttrSet(mut a), Value::AttrSet(b)) => {
|
||||||
(Value::RecAttrSet(a), Value::AttrSet(b)) => Value::AttrSet(a.update_normal(b)),
|
Rc::make_mut(&mut a).update(b.as_ref());
|
||||||
(Value::AttrSet(a), Value::RecAttrSet(b)) => Value::AttrSet(a.update_rec(b)),
|
Value::AttrSet(a)
|
||||||
|
}
|
||||||
|
(Value::RecAttrSet(a), Value::AttrSet(b)) => {
|
||||||
|
Value::AttrSet(a.update_normal(b.as_ref()).into())
|
||||||
|
}
|
||||||
|
(Value::AttrSet(mut a), Value::RecAttrSet(b)) => {
|
||||||
|
Rc::make_mut(&mut a).update_rec(b.as_ref());
|
||||||
|
Value::AttrSet(a)
|
||||||
|
}
|
||||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select(&mut self, sym: usize) -> Result<&mut Self> {
|
pub fn select(&mut self, sym: usize, vm: &'vm VM<'_>) -> Result<&mut Self> {
|
||||||
let val = match self {
|
let val = match self {
|
||||||
Value::AttrSet(attrs) => attrs
|
Value::AttrSet(attrs) => attrs
|
||||||
.select(sym)
|
.select(sym)
|
||||||
.ok_or_else(|| Error::EvalError(format!("{sym:?} not found"))),
|
.ok_or_else(|| Error::EvalError(format!("{} not found", vm.get_sym(sym)))),
|
||||||
Value::RecAttrSet(attrs) => attrs
|
Value::RecAttrSet(attrs) => attrs
|
||||||
.select(sym)
|
.select(sym)
|
||||||
.ok_or_else(|| Error::EvalError(format!("{sym:?} not found"))),
|
.ok_or_else(|| Error::EvalError(format!("{} not found", vm.get_sym(sym)))),
|
||||||
Value::Catchable(_) => return Ok(self),
|
Value::Catchable(_) => return Ok(self),
|
||||||
_ => Err(Error::EvalError(format!(
|
_ => Err(Error::EvalError(format!(
|
||||||
"cannot select from {:?}",
|
"cannot select from {:?}",
|
||||||
@@ -390,23 +427,25 @@ impl<'vm> Value<'vm> {
|
|||||||
let _ = value.force_deep(vm)?;
|
let _ = value.force_deep(vm)?;
|
||||||
*self = value;
|
*self = value;
|
||||||
}
|
}
|
||||||
Value::List(list) => list.force_deep(vm)?,
|
Value::List(list) => Rc::make_mut(list).force_deep(vm)?,
|
||||||
Value::AttrSet(attrs) => attrs.force_deep(vm)?,
|
Value::AttrSet(attrs) => Rc::make_mut(attrs).force_deep(vm)?,
|
||||||
Value::RecAttrSet(attrs) => attrs.force_deep(vm)?,
|
Value::RecAttrSet(attrs) => Rc::make_mut(attrs).force_deep(vm)?,
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl ToPublic for Value<'_> {
|
pub fn to_public(&self, vm: &'vm VM, seen: &mut HashSet<Value<'vm>>) -> p::Value {
|
||||||
fn to_public(self, vm: &VM) -> p::Value {
|
if seen.contains(self) {
|
||||||
|
return p::Value::Repeated;
|
||||||
|
}
|
||||||
|
seen.insert(self.clone());
|
||||||
match self {
|
match self {
|
||||||
Value::AttrSet(attrs) => attrs.to_public(vm),
|
Value::AttrSet(attrs) => attrs.to_public(vm, seen),
|
||||||
Value::RecAttrSet(attrs) => attrs.to_public(vm),
|
Value::RecAttrSet(attrs) => attrs.to_public(vm, seen),
|
||||||
Value::List(list) => list.to_public(vm),
|
Value::List(list) => list.to_public(vm, seen),
|
||||||
Value::Catchable(catchable) => p::Value::Catchable(catchable),
|
Value::Catchable(catchable) => p::Value::Catchable(catchable.clone()),
|
||||||
Value::Const(cnst) => p::Value::Const(cnst.into()),
|
Value::Const(cnst) => p::Value::Const(cnst.clone().into()),
|
||||||
Value::Thunk(_) => p::Value::Thunk,
|
Value::Thunk(_) => p::Value::Thunk,
|
||||||
Value::ThunkRef(_) => p::Value::Thunk,
|
Value::ThunkRef(_) => p::Value::Thunk,
|
||||||
Value::PrimOp(primop) => p::Value::PrimOp(primop.name),
|
Value::PrimOp(primop) => p::Value::PrimOp(primop.name),
|
||||||
@@ -416,26 +455,22 @@ impl ToPublic for Value<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ToPublic {
|
|
||||||
fn to_public(self, vm: &VM) -> p::Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Thunk<'vm> {
|
pub struct Thunk<'vm> {
|
||||||
pub thunk: Rc<RefCell<_Thunk<'vm>>>,
|
pub thunk: RefCell<_Thunk<'vm>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, IsVariant, Unwrap, Clone)]
|
#[derive(Debug, IsVariant, Unwrap, Clone)]
|
||||||
pub enum _Thunk<'vm> {
|
pub enum _Thunk<'vm> {
|
||||||
Code(&'vm OpCodes, OnceCell<Rc<Env<'vm>>>),
|
Code(&'vm OpCodes, OnceCell<Rc<Env<'vm>>>),
|
||||||
SuspendedFrom(*const Thunk<'vm>),
|
SuspendedFrom(*const Thunk<'vm>),
|
||||||
Value(Box<Value<'vm>>),
|
Value(Value<'vm>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'vm> Thunk<'vm> {
|
impl<'vm> Thunk<'vm> {
|
||||||
pub fn new(opcodes: &'vm OpCodes) -> Self {
|
pub fn new(opcodes: &'vm OpCodes) -> Self {
|
||||||
Thunk {
|
Thunk {
|
||||||
thunk: Rc::new(RefCell::new(_Thunk::Code(opcodes, OnceCell::new()))),
|
thunk: RefCell::new(_Thunk::Code(opcodes, OnceCell::new())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -447,7 +482,7 @@ impl<'vm> Thunk<'vm> {
|
|||||||
|
|
||||||
pub fn force(&self, vm: &'vm VM<'_>) -> Result<Value<'vm>> {
|
pub fn force(&self, vm: &'vm VM<'_>) -> Result<Value<'vm>> {
|
||||||
match &*self.thunk.borrow() {
|
match &*self.thunk.borrow() {
|
||||||
_Thunk::Value(value) => return Ok(value.as_ref().clone()),
|
_Thunk::Value(value) => return Ok(value.clone()),
|
||||||
_Thunk::SuspendedFrom(from) => {
|
_Thunk::SuspendedFrom(from) => {
|
||||||
return Err(Error::EvalError(format!(
|
return Err(Error::EvalError(format!(
|
||||||
"thunk {:p} already suspended from {from:p} (infinite recursion encountered)",
|
"thunk {:p} already suspended from {from:p} (infinite recursion encountered)",
|
||||||
@@ -471,7 +506,7 @@ impl<'vm> Thunk<'vm> {
|
|||||||
|
|
||||||
pub fn value(&'vm self) -> Option<Value<'vm>> {
|
pub fn value(&'vm self) -> Option<Value<'vm>> {
|
||||||
match &*self.thunk.borrow() {
|
match &*self.thunk.borrow() {
|
||||||
_Thunk::Value(value) => Some(value.as_ref().clone()),
|
_Thunk::Value(value) => Some(value.clone()),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
use derive_more::Constructor;
|
use derive_more::Constructor;
|
||||||
|
|
||||||
use crate::vm::VM;
|
use crate::vm::VM;
|
||||||
@@ -8,7 +10,7 @@ use super::Value;
|
|||||||
#[derive(Debug, Clone, Constructor)]
|
#[derive(Debug, Clone, Constructor)]
|
||||||
pub struct PrimOp<'vm> {
|
pub struct PrimOp<'vm> {
|
||||||
pub name: &'static str,
|
pub name: &'static str,
|
||||||
arity: u8,
|
arity: usize,
|
||||||
func: fn(&'vm VM<'_>, Vec<Value<'vm>>) -> Result<Value<'vm>>,
|
func: fn(&'vm VM<'_>, Vec<Value<'vm>>) -> Result<Value<'vm>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,15 +21,15 @@ impl PartialEq for PrimOp<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'vm> PrimOp<'vm> {
|
impl<'vm> PrimOp<'vm> {
|
||||||
pub fn call(self, vm: &'vm VM<'_>, args: Vec<Value<'vm>>) -> Result<Value<'vm>> {
|
pub fn call(&self, vm: &'vm VM<'_>, args: Vec<Value<'vm>>) -> Result<Value<'vm>> {
|
||||||
if (args.len() as u8) < self.arity {
|
if (args.len()) < self.arity {
|
||||||
Value::PartialPrimOp(PartialPrimOp {
|
Value::PartialPrimOp(PartialPrimOp {
|
||||||
name: self.name,
|
name: self.name,
|
||||||
arity: self.arity - args.len() as u8,
|
arity: self.arity - args.len(),
|
||||||
args,
|
args,
|
||||||
func: self.func,
|
func: self.func,
|
||||||
}).ok()
|
}.into()).ok()
|
||||||
} else if args.len() as u8 == self.arity {
|
} else if args.len() == self.arity {
|
||||||
(self.func)(vm, args)
|
(self.func)(vm, args)
|
||||||
} else {
|
} else {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
@@ -38,7 +40,7 @@ impl<'vm> PrimOp<'vm> {
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct PartialPrimOp<'vm> {
|
pub struct PartialPrimOp<'vm> {
|
||||||
pub name: &'static str,
|
pub name: &'static str,
|
||||||
arity: u8,
|
arity: usize,
|
||||||
args: Vec<Value<'vm>>,
|
args: Vec<Value<'vm>>,
|
||||||
func: fn(&'vm VM<'_>, Vec<Value<'vm>>) -> Result<Value<'vm>>,
|
func: fn(&'vm VM<'_>, Vec<Value<'vm>>) -> Result<Value<'vm>>,
|
||||||
}
|
}
|
||||||
@@ -50,18 +52,17 @@ impl PartialEq for PartialPrimOp<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'vm> PartialPrimOp<'vm> {
|
impl<'vm> PartialPrimOp<'vm> {
|
||||||
pub fn call(mut self, vm: &'vm VM<'_>, args: Vec<Value<'vm>>) -> Result<Value<'vm>> {
|
pub fn call(self: &Rc<Self>, vm: &'vm VM<'_>, args: Vec<Value<'vm>>) -> Result<Value<'vm>> {
|
||||||
let len = args.len() as u8;
|
let len = args.len();
|
||||||
self.args.extend(args);
|
let mut self_clone = self.clone();
|
||||||
if len < self.arity {
|
let self_mut = Rc::make_mut(&mut self_clone);
|
||||||
Value::PartialPrimOp(PartialPrimOp {
|
self_mut.args.extend(args);
|
||||||
name: self.name,
|
self_mut.arity -= len;
|
||||||
arity: self.arity - len,
|
if self_mut.arity > 0 {
|
||||||
args: self.args,
|
Value::PartialPrimOp(self_clone).ok()
|
||||||
func: self.func,
|
} else if self_mut.arity == 0 {
|
||||||
}).ok()
|
let args = std::mem::replace(&mut self_mut.args, Vec::new());
|
||||||
} else if len == self.arity {
|
(self.func)(vm, args)
|
||||||
(self.func)(vm, self.args)
|
|
||||||
} else {
|
} else {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -112,6 +112,7 @@ pub enum Value {
|
|||||||
Func,
|
Func,
|
||||||
PrimOp(&'static str),
|
PrimOp(&'static str),
|
||||||
PartialPrimOp(&'static str),
|
PartialPrimOp(&'static str),
|
||||||
|
Repeated
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Value {
|
impl Display for Value {
|
||||||
@@ -126,6 +127,7 @@ impl Display for Value {
|
|||||||
Func => write!(f, "<LAMBDA>"),
|
Func => write!(f, "<LAMBDA>"),
|
||||||
PrimOp(x) => write!(f, "<PRIMOP {x}>"),
|
PrimOp(x) => write!(f, "<PRIMOP {x}>"),
|
||||||
PartialPrimOp(x) => write!(f, "<PARTIAL PRIMOP {x}>"),
|
PartialPrimOp(x) => write!(f, "<PARTIAL PRIMOP {x}>"),
|
||||||
|
Repeated => write!(f, "<REPEATED>")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::HashMap;
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
@@ -34,9 +34,9 @@ pub fn run(prog: Program, jit: Pin<Box<JITContext<'_>>>) -> Result<p::Value> {
|
|||||||
jit
|
jit
|
||||||
);
|
);
|
||||||
let env = env(&vm);
|
let env = env(&vm);
|
||||||
let temp = vm.eval(prog.top_level.into_iter(), env)?;
|
let mut seen = HashSet::new();
|
||||||
let temp = temp.to_public(&vm);
|
let value = vm.eval(prog.top_level.into_iter(), env)?.to_public(&vm, &mut seen);
|
||||||
Ok(temp)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Constructor)]
|
#[derive(Constructor)]
|
||||||
@@ -99,7 +99,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
|
|||||||
OpCode::Illegal => panic!("illegal opcode"),
|
OpCode::Illegal => panic!("illegal opcode"),
|
||||||
OpCode::Const { idx } => stack.push(Value::Const(self.consts[idx].clone()))?,
|
OpCode::Const { idx } => stack.push(Value::Const(self.consts[idx].clone()))?,
|
||||||
OpCode::LoadThunk { idx } => {
|
OpCode::LoadThunk { idx } => {
|
||||||
stack.push(Value::Thunk(Thunk::new(self.get_thunk(idx))))?
|
stack.push(Value::Thunk(Thunk::new(self.get_thunk(idx)).into()))?
|
||||||
}
|
}
|
||||||
OpCode::CaptureEnv => {
|
OpCode::CaptureEnv => {
|
||||||
match stack.tos()? {
|
match stack.tos()? {
|
||||||
@@ -132,7 +132,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
|
|||||||
}
|
}
|
||||||
OpCode::Func { idx } => {
|
OpCode::Func { idx } => {
|
||||||
let func = self.get_func(idx);
|
let func = self.get_func(idx);
|
||||||
stack.push(Value::Func(Func::new(func, env.clone(), None)))?;
|
stack.push(Value::Func(Func::new(func, env.clone(), None).into()))?;
|
||||||
}
|
}
|
||||||
OpCode::UnOp { op } => {
|
OpCode::UnOp { op } => {
|
||||||
use UnOp::*;
|
use UnOp::*;
|
||||||
@@ -171,17 +171,17 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
|
|||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
OpCode::List => {
|
OpCode::List => {
|
||||||
stack.push(Value::List(List::empty()))?;
|
stack.push(Value::List(List::empty().into()))?;
|
||||||
}
|
}
|
||||||
OpCode::PushElem => {
|
OpCode::PushElem => {
|
||||||
let elem = stack.pop();
|
let elem = stack.pop();
|
||||||
stack.tos_mut()?.push(elem);
|
stack.tos_mut()?.push(elem);
|
||||||
}
|
}
|
||||||
OpCode::AttrSet => {
|
OpCode::AttrSet => {
|
||||||
stack.push(Value::AttrSet(AttrSet::empty()))?;
|
stack.push(Value::AttrSet(AttrSet::empty().into()))?;
|
||||||
}
|
}
|
||||||
OpCode::FinalizeRec => {
|
OpCode::FinalizeRec => {
|
||||||
env.enter(stack.tos()?.clone().unwrap_attr_set().into_inner());
|
env.enter(stack.tos()?.clone().unwrap_attr_set().as_inner().clone());
|
||||||
stack
|
stack
|
||||||
.tos_mut()?
|
.tos_mut()?
|
||||||
.as_mut()
|
.as_mut()
|
||||||
@@ -200,7 +200,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
|
|||||||
stack.tos_mut()?.push_attr(sym, val);
|
stack.tos_mut()?.push_attr(sym, val);
|
||||||
}
|
}
|
||||||
OpCode::Select { sym } => {
|
OpCode::Select { sym } => {
|
||||||
stack.tos_mut()?.force(self)?.select(sym)?;
|
stack.tos_mut()?.force(self)?.select(sym, self)?;
|
||||||
}
|
}
|
||||||
OpCode::SelectOrDefault { sym } => {
|
OpCode::SelectOrDefault { sym } => {
|
||||||
let default = stack.pop();
|
let default = stack.pop();
|
||||||
@@ -214,7 +214,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
|
|||||||
val.force(self)?;
|
val.force(self)?;
|
||||||
val.coerce_to_string();
|
val.coerce_to_string();
|
||||||
let sym = self.new_sym(val.unwrap_const().unwrap_string());
|
let sym = self.new_sym(val.unwrap_const().unwrap_string());
|
||||||
stack.tos_mut()?.force(self)?.select(sym)?;
|
stack.tos_mut()?.force(self)?.select(sym, self)?;
|
||||||
}
|
}
|
||||||
OpCode::SelectDynamicOrDefault => {
|
OpCode::SelectDynamicOrDefault => {
|
||||||
let default = stack.pop();
|
let default = stack.pop();
|
||||||
@@ -243,7 +243,8 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
|
|||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
OpCode::EnterEnv => match stack.pop() {
|
OpCode::EnterEnv => match stack.pop() {
|
||||||
Value::AttrSet(attrs) => env.enter(attrs.into_inner()),
|
Value::AttrSet(attrs) => env.enter(attrs.as_inner().clone()),
|
||||||
|
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
},
|
},
|
||||||
OpCode::LeaveEnv => {
|
OpCode::LeaveEnv => {
|
||||||
|
|||||||
Reference in New Issue
Block a user