feat: function (WIP)

This commit is contained in:
2025-05-04 13:58:34 +08:00
parent 63fd380514
commit 6ecd20854a
12 changed files with 228 additions and 79 deletions

View File

@@ -39,30 +39,88 @@ pub enum Value {
Catchable(c::Catchable),
PrimOp(PrimOp),
PartialPrimOp(PartialPrimOp),
Func(Func),
// FuncWithEnv(Func)
}
#[derive(Debug, IsVariant, Unwrap, Clone, PartialEq)]
pub enum ValueAsRef<'a> {
Const(&'a Const),
Thunk(&'a Thunk),
AttrSet(&'a AttrSet),
List(&'a List),
Catchable(&'a c::Catchable),
PrimOp(&'a PrimOp),
PartialPrimOp(&'a PartialPrimOp),
Func(&'a Func),
}
#[derive(Debug, IsVariant, Unwrap, PartialEq)]
pub enum ValueAsMut<'a> {
Const(&'a mut Const),
Thunk(&'a mut Thunk),
AttrSet(&'a mut AttrSet),
List(&'a mut List),
Catchable(&'a mut c::Catchable),
PrimOp(&'a mut PrimOp),
PartialPrimOp(&'a mut PartialPrimOp),
Func(&'a mut Func),
}
impl Value {
pub fn as_ref(&self) -> ValueAsRef<'_> {
use Value::*;
use ValueAsRef as R;
match self {
Const(x) => R::Const(x),
Thunk(x) => R::Thunk(x),
AttrSet(x) => R::AttrSet(x),
List(x) => R::List(x),
Catchable(x) => R::Catchable(x),
PrimOp(x) => R::PrimOp(x),
PartialPrimOp(x) => R::PartialPrimOp(x),
Func(x) => R::Func(x),
}
}
pub fn as_mut(&mut self) -> ValueAsMut<'_> {
use Value::*;
use ValueAsMut as M;
match self {
Const(x) => M::Const(x),
Thunk(x) => M::Thunk(x),
AttrSet(x) => M::AttrSet(x),
List(x) => M::List(x),
Catchable(x) => M::Catchable(x),
PrimOp(x) => M::PrimOp(x),
PartialPrimOp(x) => M::PartialPrimOp(x),
Func(x) => M::Func(x),
}
}
}
use Value::Const as VmConst;
impl Value {
pub fn callable(&self) -> bool {
match self {
Value::PrimOp(_) | Value::PartialPrimOp(_) | Value::Const(Const::Func(_)) => true,
Value::PrimOp(_) | Value::PartialPrimOp(_) | Value::Func(_) => true,
Value::AttrSet(_) => todo!(),
_ => false,
}
}
pub fn call(self, args: Vec<Value>) -> Value {
use Value as V;
use Value::*;
match self {
V::PrimOp(func) => func.call(args),
V::PartialPrimOp(func) => func.call(args),
mut func @ Value::Const(Const::Func(_)) => {
PrimOp(func) => func.call(args),
PartialPrimOp(func) => func.call(args),
mut func @ Value::Func(_) => {
let mut iter = args.into_iter();
while let Some(arg) = iter.next() {
func = match func {
V::PrimOp(func) => return func.call([arg].into_iter().chain(iter).collect()),
V::PartialPrimOp(func) => return func.call([arg].into_iter().chain(iter).collect()),
V::Const(Const::Func(func)) => func.call(arg),
PrimOp(func) => return func.call([arg].into_iter().chain(iter).collect()),
PartialPrimOp(func) => return func.call([arg].into_iter().chain(iter).collect()),
Func(func) => func.call(arg),
_ => todo!()
}
}
@@ -267,8 +325,9 @@ impl ToPublic for Value {
Value::Catchable(catchable) => p::Value::Catchable(catchable),
Value::Const(cnst) => p::Value::Const(cnst.into()),
Value::Thunk(_) => p::Value::Thunk,
Value::PrimOp(_) => p::Value::PrimOp,
Value::PartialPrimOp(_) => p::Value::PartialPrimOp,
Value::PrimOp(primop) => p::Value::PrimOp(primop.name),
Value::PartialPrimOp(primop) => p::Value::PartialPrimOp(primop.name),
Value::Func(_) => p::Value::Func,
}
}
}