feat: function (WIP)

This commit is contained in:
2025-05-03 21:35:36 +08:00
parent cc88e7c65f
commit 63fd380514
4 changed files with 45 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
use ecow::EcoString;
use crate::ty::internal::Value;
use crate::bytecode::{OpCodes, ThunkIdx};
#[derive(Debug, Clone)]
@@ -18,6 +19,12 @@ pub struct Func {
pub opcodes: OpCodes
}
impl Func {
pub fn call(self, _arg: Value) -> Value {
todo!()
}
}
impl PartialEq for Func {
fn eq(&self, _: &Self) -> bool {
false

View File

@@ -43,10 +43,31 @@ pub enum Value {
use Value::Const as VmConst;
impl Value {
pub fn call(self, args: Vec<Value>) -> Value {
pub fn callable(&self) -> bool {
match self {
Value::PrimOp(func) => func.call(args),
Value::PartialPrimOp(func) => func.call(args),
Value::PrimOp(_) | Value::PartialPrimOp(_) | Value::Const(Const::Func(_)) => true,
Value::AttrSet(_) => todo!(),
_ => false,
}
}
pub fn call(self, args: Vec<Value>) -> Value {
use Value as V;
match self {
V::PrimOp(func) => func.call(args),
V::PartialPrimOp(func) => func.call(args),
mut func @ Value::Const(Const::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),
_ => todo!()
}
}
func
}
_ => todo!(),
}
}