Updated macro system to process and return mactree and then lower it separately to gexpr
Some checks failed
Rust / build (push) Has been cancelled

This commit is contained in:
2026-04-11 11:00:22 +00:00
parent 9b4c7fa7d7
commit b44f3c1832
14 changed files with 460 additions and 469 deletions

View File

@@ -8,14 +8,16 @@ use itertools::{Itertools, chain};
use never::Never;
use orchid_base::{NameLike, OrcRes, Sym, VPath, is};
use orchid_extension::gen_expr::{GExpr, new_atom};
use orchid_extension::tree::{GenMember, MemKind, cnst, lazy};
use orchid_extension::tree::{GenMember, MemKind, lazy};
use orchid_extension::{
Atomic, ExecHandle, OwnedAtom, OwnedVariant, TAtom, ToExpr, ToExprFuture, TryFromExpr, exec,
Atomic, ExecHandle, OwnedAtom, OwnedVariant, TAtom, ToExpr, TryFromExpr, exec,
};
use substack::Substack;
use crate::macros::lower::lower;
use crate::macros::macro_value::{Macro, MacroData, Rule};
use crate::macros::mactree::MacTreeSeq;
use crate::macros::resolve::{ArgStack, resolve};
use crate::macros::resolve::resolve;
use crate::macros::rule::matcher::Matcher;
use crate::{MacTok, MacTree};
@@ -24,7 +26,6 @@ pub type Args = Vec<MacTree>;
#[derive(Clone)]
pub struct MacroBodyArgCollector {
argc: usize,
arg_stack: Option<ArgStack>,
args: Args,
cb: Rc<dyn for<'a> Fn(RuleCtx<'a>, Args) -> LocalBoxFuture<'a, GExpr>>,
}
@@ -42,20 +43,13 @@ impl OwnedAtom for MacroBodyArgCollector {
self.clone().call(arg).await
}
async fn call(mut self, arg: orchid_extension::Expr) -> GExpr {
if self.arg_stack.is_none() {
let atom = (TAtom::<ArgStack>::downcast(arg.handle()).await).unwrap_or_else(|_| {
panic!("The argstack is an intermediary value, the argument types are known in advance")
});
self.arg_stack = Some(atom.own().await);
} else {
let atom = (TAtom::<MacTree>::downcast(arg.handle()).await).unwrap_or_else(|_| {
panic!("This is an intermediary value, the argument types are known in advance")
});
self.args.push(atom.own().await);
}
let atom = (TAtom::<MacTree>::downcast(arg.handle()).await).unwrap_or_else(|_| {
panic!("This is an intermediary value, the argument types are known in advance")
});
self.args.push(atom.own().await);
if self.argc == self.args.len() {
exec(async move |handle| {
let rule_ctx = RuleCtx { arg_stk: self.arg_stack.unwrap(), handle };
let rule_ctx = RuleCtx { handle };
(self.cb)(rule_ctx, self.args).await
})
.await
@@ -68,15 +62,26 @@ impl OwnedAtom for MacroBodyArgCollector {
fn body_name(name: &str, counter: usize) -> String { format!("({name})::{counter}") }
pub struct RuleCtx<'a> {
arg_stk: ArgStack,
handle: ExecHandle<'a>,
}
impl RuleCtx<'_> {
pub fn recur(&self, mt: MacTree) -> impl ToExpr + use<> {
/// Recursively resolve a subexpression
pub async fn recur(&mut self, mt: MacTree) -> MacTree {
eprintln!("Recursion in macro");
let arg_stk = self.arg_stk.clone();
ToExprFuture(resolve(mt, arg_stk))
resolve(&mut self.handle, mt).await
}
/// Recursively resolve a value from a delegate macro which is expected to
/// match only keywords and return any subexpressions in a datastructure
///
/// If this is used with syntax that matches an expression macro, names bound
/// in the enclosing scope will not be correctly matched and the conversion at
/// the end will fail.
pub async fn recur_call<T: TryFromExpr>(&mut self, mt: MacTree) -> OrcRes<T> {
let resolved = self.recur(mt).await;
let lowered = lower(&resolved, Substack::Bottom).await;
self.exec(lowered).await
}
/// Normalize a value, run an expression to completion.
pub async fn exec<T: TryFromExpr>(&mut self, val: impl ToExpr) -> OrcRes<T> {
self.handle.exec(val).await
}
@@ -100,28 +105,33 @@ pub(crate) struct MacroBuilder {
body_consts: Vec<GenMember>,
}
impl MacroBuilder {
pub(crate) fn rule<const N: usize, R: ToExpr>(
pub(crate) fn rule<const N: usize>(
mut self,
pat: MacTreeSeq,
body: [impl AsyncFn(RuleCtx, [MacTree; N]) -> R + 'static; 1],
body: impl AsyncFn(RuleCtx, [MacTree; N]) -> OrcRes<MacTree> + 'static,
) -> Self {
let [body] = body;
let body = Rc::new(body);
let name = &body_name(self.own_kws[0], self.body_consts.len());
self.body_consts.extend(cnst(
true,
name,
new_atom(MacroBodyArgCollector {
argc: N,
arg_stack: None,
args: Vec::new(),
cb: Rc::new(move |rec, argv| {
let arr = argv.into_iter().collect_array::<N>().expect("argc should enforce the length");
let body = body.clone();
Box::pin(async move { body(rec, arr).await.to_gen().await })
}),
}),
));
self.body_consts.extend(lazy(true, name, async |_| {
MemKind::Const(if N == 0 {
exec(async move |handle| {
let empty = std::iter::empty().collect_array::<N>().unwrap();
Ok(new_atom(body(RuleCtx { handle }, empty).await?))
})
.await
} else {
new_atom(MacroBodyArgCollector {
argc: N,
args: Vec::new(),
cb: Rc::new(move |rec, argv| {
let arr =
argv.into_iter().collect_array::<N>().expect("argc should enforce the length");
let body = body.clone();
Box::pin(async move { body(rec, arr).await.map(new_atom).to_gen().await })
}),
})
})
}));
self.patterns.push(pat);
self
}
@@ -199,17 +209,26 @@ macro_rules! mactreev_impl {
}).at(orchid_base::Pos::Inherit));
$crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*);
};
(@RECUR $ret:ident "Val" $arg:expr) => {
$crate::macros::utils::mactreev_impl!(@RECUR $ret "Val" $arg ;)
};
(@RECUR $ret:ident "Val" $arg:expr ; $($tail:tt)*) => {
$ret.push(
$crate::macros::mactree::MacTok::Value($arg)
$crate::macros::mactree::MacTok::Value(orchid_extension::ToExpr::to_expr($arg).await)
.at(orchid_base::Pos::Inherit)
);
$crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*);
};
(@RECUR $ret:ident "push" $arg:expr) => {
$crate::macros::utils::mactreev_impl!(@RECUR $ret "push" $arg ;)
};
(@RECUR $ret:ident "push" $arg:expr ; $($tail:tt)*) => {
$ret.push($arg);
$crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*);
};
(@RECUR $ret:ident "pushv" $arg:expr) => {
$crate::macros::utils::mactreev_impl!(@RECUR $ret "pushv" $arg ;)
};
(@RECUR $ret:ident "pushv" $arg:expr ; $($tail:tt)*) => {
let $crate::macros::mactree::MacTok::S(_, body) = $arg.tok() else {
panic!("pushv used with non-vec value")
@@ -226,9 +245,15 @@ macro_rules! mactreev_impl {
).at(orchid_base::Pos::Inherit));
$crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*);
};
(@RECUR $ret:ident "l" $argh:tt $(:: $arg:tt)+ ($($body:tt)*) $($tail:tt)*) => {
(@RECUR $ret:ident "l" $arg:literal ($($body:tt)*) $($tail:tt)*) => {
assert!(
$arg.contains("::"),
"{} was treated as a name, but it doesn't have a namespace prefix",
$arg
);
let sym = orchid_base::Sym::parse($arg).await.expect("Empty string in sym literal in Rust");
$ret.push(MacTok::Lambda(
MacTok::Name(sym!($argh $(:: $arg)+).await).at(orchid_base::Pos::Inherit),
MacTok::Name(sym).at(orchid_base::Pos::Inherit),
$crate::macros::utils::mactreev!($($body)*)
).at(orchid_base::Pos::Inherit));
$crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*);
@@ -239,12 +264,9 @@ macro_rules! mactreev_impl {
"{} was treated as a name, but it doesn't have a namespace prefix",
$name
);
let sym = orchid_base::Sym::parse(
$name
).await.expect("Empty string in sym literal in Rust");
let sym = orchid_base::Sym::parse($name).await.expect("Empty string in sym literal in Rust");
$ret.push(
$crate::macros::mactree::MacTok::Name(sym)
.at(orchid_base::Pos::Inherit)
$crate::macros::mactree::MacTok::Name(sym).at(orchid_base::Pos::Inherit)
);
$crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*);
};
@@ -278,20 +300,6 @@ macro_rules! mactreev_impl {
);
$crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*);
};
(@RECUR $ret:ident $ns:ident :: $nhead:tt $($tail:tt)*) => {
$crate::macros::utils::mactreev_impl!(@NAME_MUNCHER $ret ($ns :: $nhead) $($tail)*)
};
(@NAME_MUNCHER $ret:ident ($($munched:tt)*) :: $name:tt $($tail:tt)*) => {
$crate::macros::utils::mactreev_impl!(@NAME_MUNCHER $ret ($($munched)* :: $name) $($tail)*)
};
(@NAME_MUNCHER $ret:ident ($($munched:tt)*) $($tail:tt)*) => {
let sym = orchid_base::sym!($($munched)*);
$ret.push(
$crate::macros::mactree::MacTok::Name(sym)
.at(orchid_base::Pos::Inherit)
);
$crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*);
};
() => { Vec::new() };
}
macro_rules! mactreev {