mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
dsl: add dsl_words, dsl_exprs, from_literal
This commit is contained in:
parent
2557a0d253
commit
d92e5efdd0
1 changed files with 60 additions and 32 deletions
|
|
@ -289,22 +289,16 @@ fn ok_flat <T> (x: Option<DslPerhaps<T>>) -> DslPerhaps<T> {
|
||||||
// Special form for numeric types
|
// Special form for numeric types
|
||||||
(num |$state:ident : $State: ty| -> $Type:ty { $( $pat:tt => $body:expr ),* $(,)? }) => {
|
(num |$state:ident : $State: ty| -> $Type:ty { $( $pat:tt => $body:expr ),* $(,)? }) => {
|
||||||
impl<'t> DslNs<'t, $Type> for $State {
|
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 }}),*]);
|
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 }}),*]);
|
DslNsMap::new(&[$(dsl_ns!{@exp ($state: $State) -> $Type { $pat => $body }}),*]);
|
||||||
fn from <D: Dsl> (&'t self, dsl: D) -> Perhaps<$Type> {
|
fn from_literal <D: Dsl> (&self, dsl: &D) -> Perhaps<$Type> {
|
||||||
if let Ok(Some(src)) = dsl.src() {
|
Ok(if let Some(src) = dsl.src()? {
|
||||||
if let Ok(num) = to_number(src) {
|
Some(to_number(src)? as $Type)
|
||||||
Ok(Some(num as $Type))
|
|
||||||
} else if let Ok(Some(src)) = src.word() {
|
|
||||||
self.from_word(src)
|
|
||||||
} else {
|
} else {
|
||||||
self.from_expr(src)
|
None
|
||||||
}
|
})
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -315,9 +309,9 @@ fn ok_flat <T> (x: Option<DslPerhaps<T>>) -> DslPerhaps<T> {
|
||||||
// Regular form for single type
|
// Regular form for single type
|
||||||
(|$state:ident : $State: ty| -> $Type:ty { $( $pat:tt => $body:expr ),* $(,)? }) => {
|
(|$state:ident : $State: ty| -> $Type:ty { $( $pat:tt => $body:expr ),* $(,)? }) => {
|
||||||
impl<'t> DslNs<'t, $Type> for $State {
|
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 }}),*]);
|
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 }}),*]);
|
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 {
|
pub trait DslNs<'t, T: 't>: 't {
|
||||||
/// Known symbols.
|
/// 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.
|
/// 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.
|
/// Resolve an expression or symbol.
|
||||||
fn from <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
fn from <D: Dsl> (&self, dsl: &D) -> Perhaps<T> {
|
||||||
if let Ok(Some(src)) = dsl.src() {
|
if let Ok(Some(literal)) = self.from_literal(dsl) {
|
||||||
if let Ok(Some(src)) = src.word() {
|
Ok(Some(literal))
|
||||||
self.from_word(src)
|
} else if let Ok(Some(meaning)) = self.from_word(dsl) {
|
||||||
|
Ok(Some(meaning))
|
||||||
} else {
|
} else {
|
||||||
self.from_expr(src)
|
self.from_expr(dsl)
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
/// Resolve as literal if valid.
|
||||||
|
fn from_literal <D: Dsl> (&self, dsl: &D) -> Perhaps<T> {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
/// Resolve a symbol if known.
|
/// 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()? {
|
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)
|
return Ok(None)
|
||||||
}
|
}
|
||||||
/// Resolve an expression if known.
|
/// Resolve an expression if known.
|
||||||
fn from_expr <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
fn from_expr <D: Dsl> (&self, dsl: &D) -> Perhaps<T> {
|
||||||
if let Some(head) = dsl.expr().head()? {
|
if let Some(expr) = dsl.expr()? {
|
||||||
for (key, value) in Self::EXPRS.0.iter() {
|
for (key, get) in Self::EXPRS.0.iter() {
|
||||||
if head == *key {
|
if Some(*key) == expr.head()? {
|
||||||
return value(self, dsl.expr().tail()?.unwrap_or(""))
|
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.
|
/// Populate a namespace with pre-existing values.
|
||||||
pub const fn new (data: &'t [(&'t str, T)]) -> Self { Self(data) /* TODO a search trie */ }
|
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))
|
||||||
|
})),*
|
||||||
|
)? ]);
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue