feat: function (WIP)
This commit is contained in:
@@ -3,15 +3,12 @@ use derive_more::{IsVariant, Unwrap};
|
||||
|
||||
use ecow::EcoString;
|
||||
|
||||
use super::Func;
|
||||
|
||||
#[derive(Debug, Clone, IsVariant, Unwrap)]
|
||||
pub enum Const {
|
||||
Bool(bool),
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
String(EcoString),
|
||||
Func(Func),
|
||||
}
|
||||
|
||||
impl From<bool> for Const {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use ecow::EcoString;
|
||||
|
||||
use crate::ty::internal::Value;
|
||||
use crate::bytecode::{OpCodes, ThunkIdx};
|
||||
use crate::ty::internal::Value;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Param {
|
||||
@@ -15,14 +15,65 @@ pub enum Param {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Func {
|
||||
pub param: Param,
|
||||
pub opcodes: OpCodes
|
||||
pub param: Option<Param>,
|
||||
pub opcodes: OpCodes,
|
||||
}
|
||||
|
||||
impl Func {
|
||||
pub fn new(opcodes: OpCodes) -> Func {
|
||||
Func {
|
||||
param: None,
|
||||
opcodes,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call(self, _arg: Value) -> Value {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn push_ident_param(&mut self, param: EcoString) {
|
||||
self.param = Some(Param::Ident(param))
|
||||
}
|
||||
|
||||
pub fn push_formal_param(&mut self, param: EcoString) {
|
||||
let Param::Formals { formals, .. } = self.param.get_or_insert_with(|| Param::Formals {
|
||||
formals: Vec::with_capacity(1),
|
||||
ellipsis: false,
|
||||
alias: None,
|
||||
}) else {
|
||||
panic!()
|
||||
};
|
||||
formals.push((param, None));
|
||||
}
|
||||
|
||||
pub fn push_default_param(&mut self, default: ThunkIdx) {
|
||||
let Param::Formals { formals, .. } = self.param.as_mut().unwrap() else {
|
||||
panic!()
|
||||
};
|
||||
formals.last_mut().unwrap().1 = Some(default)
|
||||
}
|
||||
|
||||
pub fn set_ellipsis(&mut self) {
|
||||
let Param::Formals { ellipsis, .. } = self.param.get_or_insert_with(|| Param::Formals {
|
||||
formals: Vec::new(),
|
||||
ellipsis: false,
|
||||
alias: None,
|
||||
}) else {
|
||||
panic!()
|
||||
};
|
||||
*ellipsis = true;
|
||||
}
|
||||
|
||||
pub fn set_alias(&mut self, sym: EcoString) {
|
||||
let Param::Formals { alias, .. } = self.param.get_or_insert_with(|| Param::Formals {
|
||||
formals: Vec::new(),
|
||||
ellipsis: false,
|
||||
alias: None,
|
||||
}) else {
|
||||
panic!()
|
||||
};
|
||||
*alias = Some(sym);
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Func {
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user