dsl: add dsl_words, dsl_exprs, from_literal

This commit is contained in:
🪞👃🪞 2025-08-23 12:19:02 +03:00
parent 2557a0d253
commit d92e5efdd0

View file

@ -289,22 +289,16 @@ fn ok_flat <T> (x: Option<DslPerhaps<T>>) -> DslPerhaps<T> {
// Special form for numeric types
(num |$state:ident : $State: ty| -> $Type:ty { $( $pat:tt => $body:expr ),* $(,)? }) => {
impl<'t> DslNs<'t, $Type> for $State {
const WORDS: DslNsMap<'t, fn (&'t $State)->Perhaps<$Type>> =
const WORDS: DslNsMap<'t, fn (&$State)->Perhaps<$Type>> =
DslNsMap::new(&[$(dsl_ns!{@word ($state: $State) -> $Type { $pat => $body }}),*]);
const EXPRS: DslNsMap<'t, fn (&'t $State, &str)->Perhaps<$Type>> =
const EXPRS: DslNsMap<'t, fn (&$State, &str)->Perhaps<$Type>> =
DslNsMap::new(&[$(dsl_ns!{@exp ($state: $State) -> $Type { $pat => $body }}),*]);
fn from <D: Dsl> (&'t self, dsl: D) -> Perhaps<$Type> {
if let Ok(Some(src)) = dsl.src() {
if let Ok(num) = to_number(src) {
Ok(Some(num as $Type))
} else if let Ok(Some(src)) = src.word() {
self.from_word(src)
} else {
self.from_expr(src)
}
fn from_literal <D: Dsl> (&self, dsl: &D) -> Perhaps<$Type> {
Ok(if let Some(src) = dsl.src()? {
Some(to_number(src)? as $Type)
} else {
Ok(None)
}
None
})
}
}
};
@ -315,9 +309,9 @@ fn ok_flat <T> (x: Option<DslPerhaps<T>>) -> DslPerhaps<T> {
// Regular form for single type
(|$state:ident : $State: ty| -> $Type:ty { $( $pat:tt => $body:expr ),* $(,)? }) => {
impl<'t> DslNs<'t, $Type> for $State {
const WORDS: DslNsMap<'t, fn (&'t $State)->Perhaps<$Type>> =
const WORDS: DslNsMap<'t, fn (&$State)->Perhaps<$Type>> =
DslNsMap::new(&[$(dsl_ns!{@word ($state: $State) -> $Type { $pat => $body }}),*]);
const EXPRS: DslNsMap<'t, fn (&'t $State, &str)->Perhaps<$Type>> =
const EXPRS: DslNsMap<'t, fn (&$State, &str)->Perhaps<$Type>> =
DslNsMap::new(&[$(dsl_ns!{@exp ($state: $State) -> $Type { $pat => $body }}),*]);
}
};
@ -358,34 +352,36 @@ fn ok_flat <T> (x: Option<DslPerhaps<T>>) -> DslPerhaps<T> {
pub trait DslNs<'t, T: 't>: 't {
/// Known symbols.
const WORDS: DslNsMap<'t, fn (&'t Self)->Perhaps<T>> = DslNsMap::new(&[]);
const WORDS: DslNsMap<'t, fn (&Self)->Perhaps<T>> = DslNsMap::new(&[]);
/// Known expressions.
const EXPRS: DslNsMap<'t, fn (&'t Self, &str)->Perhaps<T>> = DslNsMap::new(&[]);
const EXPRS: DslNsMap<'t, fn (&Self, &str)->Perhaps<T>> = DslNsMap::new(&[]);
/// Resolve an expression or symbol.
fn from <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
if let Ok(Some(src)) = dsl.src() {
if let Ok(Some(src)) = src.word() {
self.from_word(src)
} else {
self.from_expr(src)
}
fn from <D: Dsl> (&self, dsl: &D) -> Perhaps<T> {
if let Ok(Some(literal)) = self.from_literal(dsl) {
Ok(Some(literal))
} else if let Ok(Some(meaning)) = self.from_word(dsl) {
Ok(Some(meaning))
} else {
Ok(None)
self.from_expr(dsl)
}
}
/// Resolve as literal if valid.
fn from_literal <D: Dsl> (&self, dsl: &D) -> Perhaps<T> {
Ok(None)
}
/// Resolve a symbol if known.
fn from_word <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
fn from_word <D: Dsl> (&self, dsl: &D) -> Perhaps<T> {
if let Some(dsl) = dsl.word()? {
for (word, get) in Self::WORDS.0 { if dsl == *word { return get(self) } }
for (key, get) in Self::WORDS.0 { if dsl == *key { return get(self) } }
}
return Ok(None)
}
/// Resolve an expression if known.
fn from_expr <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
if let Some(head) = dsl.expr().head()? {
for (key, value) in Self::EXPRS.0.iter() {
if head == *key {
return value(self, dsl.expr().tail()?.unwrap_or(""))
fn from_expr <D: Dsl> (&self, dsl: &D) -> Perhaps<T> {
if let Some(expr) = dsl.expr()? {
for (key, get) in Self::EXPRS.0.iter() {
if Some(*key) == expr.head()? {
return get(self, &expr.tail()?.unwrap_or(""))
}
}
}
@ -400,3 +396,35 @@ impl<'t, T: Debug + 't> DslNsMap<'t, T> {
/// Populate a namespace with pre-existing values.
pub const fn new (data: &'t [(&'t str, T)]) -> Self { Self(data) /* TODO a search trie */ }
}
#[macro_export] macro_rules!dsl_words((|$state:ident|->$Type:ty$({
$($word:literal => $body:expr),* $(,)?
})?)=>{
const WORDS: DslNsMap<'t, fn (&Self)->Perhaps<$Type>> = DslNsMap::new(&[$(
$(($word, |$state: &Self|{Ok(Some($body))})),*
)? ]);
});
#[macro_export] macro_rules!dsl_exprs((|$state:ident|->$Type:ty$({
$($name:literal ($($arg:ident:$ty:ty),* $(,)?) => $body:expr),* $(,)?
})?)=>{
const EXPRS: DslNsMap<'t, fn (&Self, &str)->Perhaps<$Type>> = DslNsMap::new(&[$(
$(($name, |$state: &Self, tail: &str|{
$(
let head = tail.head()?.unwrap_or_default();
let tail = tail.tail()?.unwrap_or_default();
let $arg: $ty = if let Some(arg) = $state.from(&head)? {
arg
} else {
return Err(format!("{}: arg \"{}\" ({}) got: {head} {tail}",
$name,
stringify!($arg),
stringify!($ty),
).into())
};
)*
Ok(Some($body))
})),*
)? ]);
});