dsl: some ns progress
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-08-15 21:24:13 +03:00
parent f626860924
commit d7884f6289

View file

@ -1,64 +1,68 @@
use crate::*; use crate::*;
/// Namespace where keys are symbols.
pub trait DslSymNs<'t, T: 't>: 't {
/// Known symbold.
const SYMS: DslNs<'t, fn (&'t Self)->Perhaps<T>> = DslNs(&[]);
/// Resolve a symbol if known.
fn from_sym <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
if let Some(dsl) = dsl.sym()? {
for (sym, get) in Self::SYMS.0 { if dsl == *sym { return get(self) } }
}
return Ok(None)
}
}
/// Define a symbol namespace. /// Define a symbol namespace.
#[macro_export] macro_rules! dsl_sym_ns ( #[macro_export] macro_rules! dsl_sym_ns (
(|$state:ident:$State:ty| -> $type:ty {$($lit:literal => $exp:expr),* $(,)?})=>{ (|$state:ident:$State:ty| -> $Type:ty {$($lit:literal => $exp:expr),* $(,)?})=>{
impl<'t> DslSymNs<'t, $type> for $State { impl<'t> DslSymNs<'t, $Type> for $State {
const SYMS: DslNs<'t, fn (&'t $State)->$type> = DslNs(&[ const SYMS: DslNs<'t, fn (&'t $State)->Perhaps<$Type>> = DslNs(&[
$(($lit, |$state: &$State|$exp)),* $(($lit, |$state: &$State|Ok(Some($exp)))),*
]); ]);
} }
}); });
/// Define an expression namespace. /// Namespace where the keys are heads of expressions.
#[macro_export] macro_rules! dsl_exp_ns ( pub trait DslExpNs<'t, T: 't>: 't {
(|$state:ident:$State:ty|->$type:ty { $( ( /// Known expressions.
$key:literal const EXPS: DslNs<'t, fn (&'t Self, &str)->Perhaps<T>> = DslNs(&[]);
$(/ $sub:ident : $Sub:ty)? /// Resolve an expression if known.
$(, $arg:ident $(?)? : $argtype:ty)* fn from_exp <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
$(, ..$tail:ident)? if let Some(exp) = dsl.exp()? {
) => $body:expr),* $(,)? }) => { //panic!("exp\n{:?}\n{:?}\n{:?}\n{:?}",
impl<'t> DslExpNs<'t, fn (&'t $State, &str)->$type> for $State { //exp.src(), exp.head(), exp.head().src(), Self::EXPS.0);
const EXPS: DslNs<'t, fn (&'t $State, &str)->$type> = DslNs(&[ $( for (key, value) in Self::EXPS.0.iter() {
($key, |$state: &$State, mut tail: &str|{ println!("{key} {:?}", exp.head());
$(let $arg = dsl_next(&mut tail)?;)* if exp.head() == Ok(Some(key)) { return value(self, exp.tail()?.unwrap_or("")) }
$(let $tail = tail;)?
$body
})
),* ]);
}
});
/// Namespace where keys are symbols.
pub trait DslSymNs<'t, T: 't>: 't {
const SYMS: DslNs<'t, fn (&'t Self)->T> = DslNs(&[]);
fn from_sym <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
if let Some(dsl) = dsl.sym()? {
for (sym, get) in Self::SYMS.0 {
if dsl == *sym {
return Ok(Some(get(self)))
}
} }
} }
return Ok(None) return Ok(None)
} }
} }
/// Namespace where the keys are heads of expressions. /// Define an expression namespace.
pub trait DslExpNs<'t, T: 't>: 't { #[macro_export] macro_rules! dsl_exp_ns (
const EXPS: DslNs<'t, fn (&'t Self, &str)->T> = DslNs(&[]); (|$state:ident:$State:ty|->$Type:ty { $( (
fn from_exp <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> { $key:literal $(/ $sub:ident : $Sub:ty)? $(, $arg:ident:$ty:ty)* $(, ..$tail:ident)?
if let Some(exp) = dsl.exp()? { ) => $body:expr),* $(,)? }) => {
panic!("exp\n{:?}\n{:?}\n{:?}\n{:?}", exp.src(), exp.head(), exp.head().src(), Self::EXPS.0); impl<'t> DslExpNs<'t, $Type> for $State {
for (key, value) in Self::EXPS.0.iter() { const EXPS: DslNs<'t, fn (&'t $State, &str)->Perhaps<$Type>> = DslNs(&[ $(
println!("{key} {:?}", exp.head()); ($key, |$state: &$State, mut tail: &str|{
if exp.head() == Ok(Some(key)) { $(let $arg: $ty = if let Some(val) = dsl_next(&mut tail)? {
return Ok(Some(value(self, exp.tail()?.unwrap_or("")))) val
} } else {
} return Err(format!("missing arg: {}", stringify!($arg)).into())
};)*
$(let $tail = tail;)?
Ok(Some($body))
})
),* ]);
} }
return Ok(None) });
}
pub fn dsl_next <T> (dsl: &mut impl Dsl) -> DslPerhaps<T> {
todo!()
} }
/// Namespace. /// Namespace.