feat: simple functions
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
use ecow::EcoString;
|
||||
use rpds::HashTrieMap;
|
||||
|
||||
use crate::bytecode::{OpCodes, ThunkIdx};
|
||||
use crate::ty::internal::Value;
|
||||
|
||||
use crate::vm::{LockedEnv, VM};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Param {
|
||||
Ident(EcoString),
|
||||
@@ -15,20 +18,31 @@ pub enum Param {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Func {
|
||||
pub env: LockedEnv,
|
||||
pub param: Option<Param>,
|
||||
pub opcodes: OpCodes,
|
||||
}
|
||||
|
||||
impl Func {
|
||||
pub fn new(opcodes: OpCodes) -> Func {
|
||||
pub fn new(env: LockedEnv, opcodes: OpCodes) -> Func {
|
||||
Func {
|
||||
param: None,
|
||||
env,
|
||||
opcodes,
|
||||
param: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call(self, _arg: Value) -> Value {
|
||||
todo!()
|
||||
pub fn call(self, vm: &VM, arg: Value) -> Value {
|
||||
use Param::*;
|
||||
|
||||
let mut env = self.env.unlocked();
|
||||
|
||||
match self.param.unwrap() {
|
||||
Ident(ident) => env.enter(HashTrieMap::new_sync().insert(ident.into(), arg)),
|
||||
Formals { .. } => todo!()
|
||||
}
|
||||
|
||||
vm.eval(self.opcodes, &mut env).unwrap()
|
||||
}
|
||||
|
||||
pub fn push_ident_param(&mut self, param: EcoString) {
|
||||
|
||||
@@ -109,7 +109,7 @@ impl Value {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call(self, args: Vec<Value>) -> Value {
|
||||
pub fn call(self, vm: &VM, args: Vec<Value>) -> Value {
|
||||
use Value::*;
|
||||
match self {
|
||||
PrimOp(func) => func.call(args),
|
||||
@@ -120,7 +120,7 @@ impl Value {
|
||||
func = match func {
|
||||
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),
|
||||
Func(func) => func.call(vm, arg),
|
||||
_ => todo!()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user