Macro system done in theory

too afraid to begin debugging, resting for a moment
This commit is contained in:
2025-09-03 16:05:26 +02:00
parent 051b5e666f
commit 7031f3a7d8
51 changed files with 1463 additions and 458 deletions

View File

@@ -1,14 +1,11 @@
use std::borrow::Cow;
use std::pin::Pin;
use futures::AsyncWrite;
use never::Never;
use orchid_extension::atom::{Atomic, TypAtom};
use orchid_extension::atom_owned::{DeserializeCtx, OwnedAtom, OwnedVariant, get_own_instance};
use orchid_extension::atom_owned::{OwnedAtom, OwnedVariant, own};
use orchid_extension::conv::{ToExpr, TryFromExpr};
use orchid_extension::expr::Expr;
use orchid_extension::gen_expr::GExpr;
use orchid_extension::system::SysCtx;
use crate::macros::mactree::{MacTok, MacTree, map_mactree};
@@ -24,16 +21,7 @@ impl Atomic for InstantiateTplCall {
}
impl OwnedAtom for InstantiateTplCall {
async fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(()) }
/// TODO: get serialization done for mactree
type Refs = Vec<Expr>;
async fn serialize(
&self,
ctx: SysCtx,
write: Pin<&mut (impl AsyncWrite + ?Sized)>,
) -> Self::Refs {
todo!()
}
async fn deserialize(ctx: impl DeserializeCtx, refs: Self::Refs) -> Self { todo!() }
type Refs = Never;
// Technically must be supported but shouldn't actually ever be called
async fn call_ref(&self, arg: Expr) -> GExpr {
eprintln!(
@@ -43,20 +31,19 @@ impl OwnedAtom for InstantiateTplCall {
self.clone().call(arg).await
}
async fn call(mut self, arg: Expr) -> GExpr {
let arg = match TypAtom::try_from_expr(arg).await {
Err(e) => return Err::<Never, _>(e).to_expr(),
Ok(t) => get_own_instance(t).await,
match TypAtom::<MacTree>::try_from_expr(arg).await {
Err(e) => return Err::<Never, _>(e).to_expr().await,
Ok(t) => self.argv.push(own(t).await),
};
self.argv.push(arg);
if self.argv.len() < self.argc {
return self.to_expr();
return self.to_expr().await;
}
let mut args = self.argv.into_iter();
map_mactree(&self.tpl, &mut false, &mut |tpl| match &*tpl.tok {
MacTok::Ph(_) => panic!("instantiate_tpl received a placeholder"),
MacTok::Slot => Some(args.next().expect("Not enough arguments to fill all slots!")),
let ret = map_mactree(&self.tpl, &mut false, &mut |mt| match mt.tok() {
MacTok::Slot => Some(args.next().expect("Not enough arguments to fill all slots")),
_ => None,
})
.to_expr()
});
assert!(args.next().is_none(), "Too many arguments for all slots");
ret.to_expr().await
}
}