feat: usable?

This commit is contained in:
2025-05-05 11:31:46 +08:00
parent eea4a4ce9f
commit b9dcc83c39
24 changed files with 688 additions and 244 deletions

View File

@@ -1,12 +1,14 @@
use derive_more::Constructor;
use crate::vm::VM;
use super::Value;
#[derive(Debug, Clone, Constructor)]
pub struct PrimOp {
pub name: &'static str,
arity: u8,
func: fn(Vec<Value>) -> Value,
func: fn(&VM, Vec<Value>) -> Value,
}
impl PartialEq for PrimOp {
@@ -16,7 +18,7 @@ impl PartialEq for PrimOp {
}
impl PrimOp {
pub fn call(self, args: Vec<Value>) -> Value {
pub fn call(self, vm: &VM, args: Vec<Value>) -> Value {
if (args.len() as u8) < self.arity {
Value::PartialPrimOp(PartialPrimOp {
name: self.name,
@@ -25,7 +27,7 @@ impl PrimOp {
func: self.func,
})
} else if args.len() as u8 == self.arity {
(self.func)(args)
(self.func)(vm, args)
} else {
unimplemented!()
}
@@ -37,7 +39,7 @@ pub struct PartialPrimOp {
pub name: &'static str,
arity: u8,
args: Vec<Value>,
func: fn(Vec<Value>) -> Value,
func: fn(&VM, Vec<Value>) -> Value,
}
impl PartialEq for PartialPrimOp {
@@ -47,7 +49,7 @@ impl PartialEq for PartialPrimOp {
}
impl PartialPrimOp {
pub fn call(mut self, args: Vec<Value>) -> Value {
pub fn call(mut self, vm: &VM, args: Vec<Value>) -> Value {
let len = args.len() as u8;
self.args.extend(args);
if len < self.arity {
@@ -58,10 +60,9 @@ impl PartialPrimOp {
func: self.func,
})
} else if len == self.arity {
(self.func)(self.args)
(self.func)(vm, self.args)
} else {
unimplemented!()
}
}
}