wip: dsl refactor

This commit is contained in:
🪞👃🪞 2025-08-15 21:03:49 +03:00
parent a1190a24a1
commit f626860924
5 changed files with 285 additions and 236 deletions

View file

@ -1,51 +1,5 @@
use crate::*;
/// Namespace. Currently linearly searched.
pub struct DslNs<'t, T: 't>(pub &'t [(&'t str, T)]);
/// Namespace where keys are symbols.
pub trait DslSymNs<'t, T: 't>: 't {
const SYMS: DslNs<'t, fn (&'t Self)->T>;
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)
}
}
#[macro_export] macro_rules! dsl_sym (
(|$state:ident:$State:ty| -> $type:ty {$($lit:literal => $exp:expr),* $(,)?})=>{
impl<'t> DslSymNs<'t, $type> for $State {
const SYMS: DslNs<'t, fn (&'t $State)->$type> =
DslNs(&[$(($lit, |$state: &$State|$exp)),*]); } });
pub trait DslExpNs<'t, T: 't>: 't {
const EXPS: DslNs<'t, fn (&'t Self, &str)->T>;
fn from_exp <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
if let Some(exp) = dsl.exp()? {
for (key, value) in Self::EXPS.0.iter() {
if exp.head() == Ok(Some(key)) {
return Ok(Some(value(self, exp.tail()?.unwrap_or(""))))
}
}
}
return Ok(None)
}
}
#[macro_export] macro_rules! dsl_exp (
(|$state:ident:$State:ty$(, $tail:ident)?|->$type:ty { $(
($key:literal $(/ $sub:ident: $Sub:ty)? $(, $arg:ident $(?)? :$argtype:ty)*) => $body:expr
),* $(,)? }) => {
impl<'t> DslExpNs<'t, $type> for $State {
const EXPS: DslNs<'t, fn (&'t $State, &str)->$type> =
DslNs(&[]); } });
// TODO DEPRECATE:
/// `T` + [Dsl] -> `Self`.