forked from Orchid/orchid
Macro system done in theory
too afraid to begin debugging, resting for a moment
This commit is contained in:
@@ -1,3 +1,9 @@
|
||||
//! #TODO: redefine this in terms of [crate::coroutine_exec::exec]. Try
|
||||
//! differentiating between eager and lazy arguments. [ExprFunc] can remain with
|
||||
//! tweaks but other techniques probably must go.
|
||||
//!
|
||||
//! Notice also that Func must still be resumable
|
||||
use std::any::TypeId;
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
use std::future::Future;
|
||||
@@ -20,6 +26,7 @@ use trait_set::trait_set;
|
||||
use crate::atom::Atomic;
|
||||
use crate::atom_owned::{DeserializeCtx, OwnedAtom, OwnedVariant};
|
||||
use crate::conv::ToExpr;
|
||||
use crate::coroutine_exec::{ExecHandle, exec};
|
||||
use crate::expr::Expr;
|
||||
use crate::gen_expr::GExpr;
|
||||
use crate::system::{SysCtx, SysCtxEntry};
|
||||
@@ -29,13 +36,37 @@ trait_set! {
|
||||
}
|
||||
|
||||
pub trait ExprFunc<I, O>: Clone + 'static {
|
||||
const ARITY: u8;
|
||||
fn apply(&self, v: Vec<Expr>) -> impl Future<Output = OrcRes<GExpr>>;
|
||||
fn argtyps() -> &'static [TypeId];
|
||||
fn apply<'a>(&self, hand: ExecHandle<'a>, v: Vec<Expr>) -> impl Future<Output = OrcRes<GExpr>>;
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct FunsCtx(Mutex<HashMap<Sym, (u8, Rc<dyn FunCB>)>>);
|
||||
struct FunsCtx(Mutex<HashMap<Sym, FunRecord>>);
|
||||
impl SysCtxEntry for FunsCtx {}
|
||||
#[derive(Clone)]
|
||||
struct FunRecord {
|
||||
argtyps: &'static [TypeId],
|
||||
fun: Rc<dyn FunCB>,
|
||||
}
|
||||
|
||||
async fn process_args<I, O, F: ExprFunc<I, O>>(f: F) -> FunRecord {
|
||||
let argtyps = F::argtyps();
|
||||
let fun = Rc::new(move |v: Vec<Expr>| {
|
||||
clone!(f, v mut);
|
||||
exec(async move |mut hand| {
|
||||
let mut norm_args = Vec::with_capacity(v.len());
|
||||
for (expr, typ) in v.into_iter().zip(argtyps) {
|
||||
if *typ != TypeId::of::<Expr>() {
|
||||
norm_args.push(hand.exec(expr).await?);
|
||||
}
|
||||
}
|
||||
f.apply(hand, norm_args).await
|
||||
})
|
||||
.map(Ok)
|
||||
.boxed_local()
|
||||
});
|
||||
FunRecord { argtyps, fun }
|
||||
}
|
||||
|
||||
/// An Atom representing a partially applied named native function. These
|
||||
/// partial calls are serialized into the name of the native function and the
|
||||
@@ -46,23 +77,22 @@ impl SysCtxEntry for FunsCtx {}
|
||||
pub(crate) struct Fun {
|
||||
path: Sym,
|
||||
args: Vec<Expr>,
|
||||
arity: u8,
|
||||
fun: Rc<dyn FunCB>,
|
||||
record: FunRecord,
|
||||
}
|
||||
impl Fun {
|
||||
pub async fn new<I, O, F: ExprFunc<I, O>>(path: Sym, ctx: SysCtx, f: F) -> Self {
|
||||
let funs: &FunsCtx = ctx.get_or_default();
|
||||
let mut fung = funs.0.lock().await;
|
||||
let fun = if let Some(x) = fung.get(&path) {
|
||||
x.1.clone()
|
||||
let record = if let Some(record) = fung.get(&path) {
|
||||
record.clone()
|
||||
} else {
|
||||
let fun = Rc::new(move |v| clone!(f; async move { f.apply(v).await }.boxed_local()));
|
||||
fung.insert(path.clone(), (F::ARITY, fun.clone()));
|
||||
fun
|
||||
let record = process_args(f).await;
|
||||
fung.insert(path.clone(), record.clone());
|
||||
record
|
||||
};
|
||||
Self { args: vec![], arity: F::ARITY, path, fun }
|
||||
Self { args: vec![], path, record }
|
||||
}
|
||||
pub fn arity(&self) -> u8 { self.arity }
|
||||
pub fn arity(&self) -> u8 { self.record.argtyps.len() as u8 }
|
||||
}
|
||||
impl Atomic for Fun {
|
||||
type Data = ();
|
||||
@@ -74,11 +104,10 @@ impl OwnedAtom for Fun {
|
||||
async fn call_ref(&self, arg: Expr) -> GExpr {
|
||||
std::io::Write::flush(&mut std::io::stderr()).unwrap();
|
||||
let new_args = self.args.iter().cloned().chain([arg]).collect_vec();
|
||||
if new_args.len() == self.arity.into() {
|
||||
(self.fun)(new_args).await.to_expr()
|
||||
if new_args.len() == self.record.argtyps.len() {
|
||||
(self.record.fun)(new_args).await.to_expr().await
|
||||
} else {
|
||||
Self { args: new_args, arity: self.arity, fun: self.fun.clone(), path: self.path.clone() }
|
||||
.to_expr()
|
||||
Self { args: new_args, record: self.record.clone(), path: self.path.clone() }.to_expr().await
|
||||
}
|
||||
}
|
||||
async fn call(self, arg: Expr) -> GExpr { self.call_ref(arg).await }
|
||||
@@ -89,11 +118,13 @@ impl OwnedAtom for Fun {
|
||||
async fn deserialize(mut ctx: impl DeserializeCtx, args: Self::Refs) -> Self {
|
||||
let sys = ctx.sys();
|
||||
let path = Sym::from_api(ctx.decode().await, sys.i()).await;
|
||||
let (arity, fun) = sys.get_or_default::<FunsCtx>().0.lock().await.get(&path).unwrap().clone();
|
||||
Self { args, arity, path, fun }
|
||||
let record = (sys.get::<FunsCtx>().0.lock().await.get(&path))
|
||||
.expect("Function missing during deserialization")
|
||||
.clone();
|
||||
Self { args, path, record }
|
||||
}
|
||||
async fn print<'a>(&'a self, _: &'a (impl FmtCtx + ?Sized + 'a)) -> FmtUnit {
|
||||
format!("{}:{}/{}", self.path, self.args.len(), self.arity).into()
|
||||
async fn print_atom<'a>(&'a self, _: &'a (impl FmtCtx + ?Sized + 'a)) -> FmtUnit {
|
||||
format!("{}:{}/{}", self.path, self.args.len(), self.arity()).into()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,13 +135,11 @@ impl OwnedAtom for Fun {
|
||||
#[derive(Clone)]
|
||||
pub struct Lambda {
|
||||
args: Vec<Expr>,
|
||||
arity: u8,
|
||||
fun: Rc<dyn FunCB>,
|
||||
record: FunRecord,
|
||||
}
|
||||
impl Lambda {
|
||||
pub fn new<I, O, F: ExprFunc<I, O>>(f: F) -> Self {
|
||||
let fun = Rc::new(move |v| clone!(f; async move { f.apply(v).await }.boxed_local()));
|
||||
Self { args: vec![], arity: F::ARITY, fun }
|
||||
pub async fn new<I, O, F: ExprFunc<I, O>>(f: F) -> Self {
|
||||
Self { args: vec![], record: process_args(f).await }
|
||||
}
|
||||
}
|
||||
impl Atomic for Lambda {
|
||||
@@ -122,53 +151,59 @@ impl OwnedAtom for Lambda {
|
||||
async fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(()) }
|
||||
async fn call_ref(&self, arg: Expr) -> GExpr {
|
||||
let new_args = self.args.iter().cloned().chain([arg]).collect_vec();
|
||||
if new_args.len() == self.arity.into() {
|
||||
(self.fun)(new_args).await.to_expr()
|
||||
if new_args.len() == self.record.argtyps.len() {
|
||||
(self.record.fun)(new_args).await.to_expr().await
|
||||
} else {
|
||||
Self { args: new_args, arity: self.arity, fun: self.fun.clone() }.to_expr()
|
||||
Self { args: new_args, record: self.record.clone() }.to_expr().await
|
||||
}
|
||||
}
|
||||
async fn call(self, arg: Expr) -> GExpr { self.call_ref(arg).await }
|
||||
}
|
||||
|
||||
mod expr_func_derives {
|
||||
use std::any::TypeId;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use orchid_base::error::OrcRes;
|
||||
|
||||
use super::ExprFunc;
|
||||
use crate::conv::{ToExpr, TryFromExpr};
|
||||
use crate::func_atom::Expr;
|
||||
use crate::func_atom::{ExecHandle, Expr};
|
||||
use crate::gen_expr::GExpr;
|
||||
|
||||
macro_rules! expr_func_derive {
|
||||
($arity: tt, $($t:ident),*) => {
|
||||
($($t:ident),*) => {
|
||||
pastey::paste!{
|
||||
impl<
|
||||
$($t: TryFromExpr, )*
|
||||
$($t: TryFromExpr + 'static, )*
|
||||
Out: ToExpr,
|
||||
Func: AsyncFn($($t,)*) -> Out + Clone + Send + Sync + 'static
|
||||
> ExprFunc<($($t,)*), Out> for Func {
|
||||
const ARITY: u8 = $arity;
|
||||
async fn apply(&self, v: Vec<Expr>) -> OrcRes<GExpr> {
|
||||
assert_eq!(v.len(), Self::ARITY.into(), "Arity mismatch");
|
||||
fn argtyps() -> &'static [TypeId] {
|
||||
static STORE: OnceLock<Vec<TypeId>> = OnceLock::new();
|
||||
&*STORE.get_or_init(|| vec![$(TypeId::of::<$t>()),*])
|
||||
}
|
||||
async fn apply<'a>(&self, _: ExecHandle<'a>, v: Vec<Expr>) -> OrcRes<GExpr> {
|
||||
assert_eq!(v.len(), Self::argtyps().len(), "Arity mismatch");
|
||||
let [$([< $t:lower >],)*] = v.try_into().unwrap_or_else(|_| panic!("Checked above"));
|
||||
Ok(self($($t::try_from_expr([< $t:lower >]).await?,)*).await.to_expr())
|
||||
Ok(self($($t::try_from_expr([< $t:lower >]).await?,)*).await.to_expr().await)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
expr_func_derive!(1, A);
|
||||
expr_func_derive!(2, A, B);
|
||||
expr_func_derive!(3, A, B, C);
|
||||
expr_func_derive!(4, A, B, C, D);
|
||||
expr_func_derive!(5, A, B, C, D, E);
|
||||
expr_func_derive!(6, A, B, C, D, E, F);
|
||||
expr_func_derive!(7, A, B, C, D, E, F, G);
|
||||
expr_func_derive!(8, A, B, C, D, E, F, G, H);
|
||||
expr_func_derive!(9, A, B, C, D, E, F, G, H, I);
|
||||
expr_func_derive!(10, A, B, C, D, E, F, G, H, I, J);
|
||||
expr_func_derive!(11, A, B, C, D, E, F, G, H, I, J, K);
|
||||
expr_func_derive!(12, A, B, C, D, E, F, G, H, I, J, K, L);
|
||||
expr_func_derive!(13, A, B, C, D, E, F, G, H, I, J, K, L, M);
|
||||
expr_func_derive!(14, A, B, C, D, E, F, G, H, I, J, K, L, M, N);
|
||||
expr_func_derive!(A);
|
||||
expr_func_derive!(A, B);
|
||||
expr_func_derive!(A, B, C);
|
||||
expr_func_derive!(A, B, C, D);
|
||||
expr_func_derive!(A, B, C, D, E);
|
||||
// expr_func_derive!(A, B, C, D, E, F);
|
||||
// expr_func_derive!(A, B, C, D, E, F, G);
|
||||
// expr_func_derive!(A, B, C, D, E, F, G, H);
|
||||
// expr_func_derive!(A, B, C, D, E, F, G, H, I);
|
||||
// expr_func_derive!(A, B, C, D, E, F, G, H, I, J);
|
||||
// expr_func_derive!(A, B, C, D, E, F, G, H, I, J, K);
|
||||
// expr_func_derive!(A, B, C, D, E, F, G, H, I, J, K, L);
|
||||
// expr_func_derive!(A, B, C, D, E, F, G, H, I, J, K, L, M);
|
||||
// expr_func_derive!(A, B, C, D, E, F, G, H, I, J, K, L, M, N);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user