partway through fixes, macro system needs resdesign

This commit is contained in:
2026-04-08 18:02:20 +02:00
parent 0909524dee
commit 9b4c7fa7d7
76 changed files with 1391 additions and 1065 deletions

View File

@@ -1,24 +1,20 @@
use futures::{StreamExt, stream};
use orchid_base::{OrcRes, sym};
use orchid_extension::TAtom;
use orchid_extension::ToExpr;
use orchid_extension::exec;
use orchid_extension::Expr;
use orchid_extension::gen_expr::{GExpr, call, new_atom};
use orchid_extension::tree::{GenMember, fun, prefix};
use orchid_extension::{Expr, TAtom, ToExpr, exec};
use crate::macros::match_macros::MatcherAtom;
use crate::macros::utils::{build_macro, mactree, mactreev};
use crate::macros::utils::{RuleCtx, build_macro, mactree, mactreev};
use crate::{HomoTpl, MacTree, OrcOpt};
pub async fn gen_tuple_macro_lib() -> Vec<GenMember> {
prefix("std::tuple", [
build_macro(None, ["t"])
.rule(mactreev!(std::tuple::t [ "...$" elements 0 ]), [|[elements]: [_; _]| {
exec(async move |mut h| {
let tup = h
.exec::<HomoTpl<TAtom<MacTree>>>(call(sym!(macros::resolve),
new_atom(mactree!((macros::common::comma_list "push" elements ;))),
.rule(mactreev!(std::tuple::t [ "...$" elements 0 ]), [async |mut cx, [elements]| {
let tup: HomoTpl<TAtom<MacTree>> = cx
.exec(cx.recur(
mactree!((macros::common::comma_list "push" elements ;)),
))
.await?;
let val = stream::iter(&tup.0[..])
@@ -33,65 +29,52 @@ pub async fn gen_tuple_macro_lib() -> Vec<GenMember> {
})
.await;
Ok(val)
})
}])
.rule(
mactreev!(pattern::match_rule(std::tuple::t[ "...$" elements 0 macros::common::, macros::common::..])),
[async |[elements]: [_; _]| parse_tpl(elements, Some(mactree!(macros::common::_))).await],
[async |cx, [elements]| parse_tpl(cx, elements, Some(mactree!(macros::common::_))).await],
)
.rule(
mactreev!(pattern::match_rule(
std::tuple::t[ "...$" elements 1 macros::common::, macros::common::.. "...$" tail 0]
)),
[async |[elements, tail]: [_; _]| parse_tpl(elements, Some(tail)).await],
[async |cx, [elements, tail]| parse_tpl(cx, elements, Some(tail)).await],
)
.rule(mactreev!(pattern::match_rule(std::tuple::t[ "...$" elements 0])), [
|[elements]: [_; _]| parse_tpl(elements, None),
async |cx, [elements]| parse_tpl(cx, elements, None).await,
])
.finish(),
fun(false, "matcher_body", tuple_matcher_body),
])
}
fn parse_tpl(elements: MacTree, tail_matcher: Option<MacTree>) -> impl Future<Output = GExpr> {
exec(async move |mut h| -> OrcRes<GExpr> {
let tup = h
.exec::<HomoTpl<TAtom<MacTree>>>(call(sym!(macros::resolve), new_atom(
mactree!((macros::common::comma_list "push" elements ;)),
)))
.await?;
let mut subs = Vec::with_capacity(tup.0.len());
for mac_a in &tup.0[..] {
let sub = h
.exec::<TAtom<MatcherAtom>>(call(sym!(macros::resolve), new_atom(
mactree!(pattern::match_rule ("push" mac_a.own().await ;)),
)))
.await?;
subs.push(sub);
}
let tail_matcher = match tail_matcher {
Some(mac) => Some(
h.exec::<TAtom<MatcherAtom>>(call(sym!(macros::resolve), new_atom(
mactree!(pattern::match_rule "push" mac ;),
)))
.await?,
),
None => None,
};
Ok(new_atom(MatcherAtom {
keys: stream::iter(&subs[..])
.flat_map(|t| t.keys())
.chain(stream::iter(&tail_matcher).flat_map(|mat| mat.keys()))
.collect()
.await,
matcher: call(sym!(std::tuple::matcher_body), (
HomoTpl(subs),
OrcOpt(tail_matcher),
))
async fn parse_tpl(
mut cx: RuleCtx<'_>,
elements: MacTree,
tail_matcher: Option<MacTree>,
) -> OrcRes<GExpr> {
let tup: HomoTpl<TAtom<MacTree>> =
cx.exec(cx.recur(mactree!((macros::common::comma_list "push" elements ;)))).await?;
let mut subs = Vec::with_capacity(tup.0.len());
for mac_a in &tup.0[..] {
let sub: TAtom<MatcherAtom> =
cx.exec(cx.recur(mactree!(pattern::match_rule ("push" mac_a.own().await ;)))).await?;
subs.push(sub);
}
let tail_matcher: Option<TAtom<MatcherAtom>> = match tail_matcher {
Some(mac) => Some(cx.exec(cx.recur(mactree!(pattern::match_rule "push" mac ;))).await?),
None => None,
};
Ok(new_atom(MatcherAtom {
keys: stream::iter(&subs[..])
.flat_map(|t| t.keys())
.chain(stream::iter(&tail_matcher).flat_map(|mat| mat.keys()))
.collect()
.await,
matcher: call(sym!(std::tuple::matcher_body), (HomoTpl(subs), OrcOpt(tail_matcher)))
.to_expr()
.await,
}))
})
}))
}
fn tuple_matcher_body(