mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
dsl: InputLayerCond; collect macros
This commit is contained in:
parent
0d4ba4a54e
commit
3df1938626
6 changed files with 121 additions and 110 deletions
|
|
@ -24,6 +24,7 @@
|
|||
}
|
||||
}
|
||||
|
||||
/// Implement `Context` for one or more base structs, types, and keys. */
|
||||
#[macro_export] macro_rules! expose {
|
||||
($([$self:ident:$State:ty] $(([$($Type:tt)*] $(($pat:literal $expr:expr))*))*)*) => {
|
||||
$(expose!(@impl [$self: $State] { $([$($Type)*] => { $($pat => $expr),* })* });)*
|
||||
|
|
@ -51,6 +52,90 @@
|
|||
};
|
||||
}
|
||||
|
||||
/// Implement `Context` for a context and type.
|
||||
#[macro_export] macro_rules! provide {
|
||||
// Provide a value to the EDN template
|
||||
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl Context<$type> for $State {
|
||||
#[allow(unreachable_code)]
|
||||
fn get (&$self, dsl: &Value) -> Option<$type> {
|
||||
use Value::*;
|
||||
Some(match dsl { $(Sym($pat) => $expr,)* _ => return None })
|
||||
}
|
||||
}
|
||||
};
|
||||
// Provide a value more generically
|
||||
($lt:lifetime: $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<$lt> Context<$lt, $type> for $State {
|
||||
#[allow(unreachable_code)]
|
||||
fn get (&$lt $self, dsl: &Value) -> Option<$type> {
|
||||
use Value::*;
|
||||
Some(match dsl { $(Sym($pat) => $expr,)* _ => return None })
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Implement `Context` for a context and numeric type.
|
||||
///
|
||||
/// This enables support for numeric literals.
|
||||
#[macro_export] macro_rules! provide_num {
|
||||
// Provide a value that may also be a numeric literal in the EDN, to a generic implementation.
|
||||
($type:ty:|$self:ident:<$T:ident:$Trait:path>|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<$T: $Trait> Context<$type> for $T {
|
||||
fn get (&$self, dsl: &Value) -> Option<$type> {
|
||||
use Value::*;
|
||||
Some(match dsl { $(Sym($pat) => $expr,)* Num(n) => *n as $type, _ => return None })
|
||||
}
|
||||
}
|
||||
};
|
||||
// Provide a value that may also be a numeric literal in the EDN, to a concrete implementation.
|
||||
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl Context<$type> for $State {
|
||||
fn get (&$self, dsl: &Value) -> Option<$type> {
|
||||
use Value::*;
|
||||
Some(match dsl { $(Sym($pat) => $expr,)* Num(n) => *n as $type, _ => return None })
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Implement `Context` for a context and the boolean type.
|
||||
///
|
||||
/// This enables support for boolean literals.
|
||||
#[macro_export] macro_rules! provide_bool {
|
||||
// Provide a value that may also be a numeric literal in the EDN, to a generic implementation.
|
||||
($type:ty:|$self:ident:<$T:ident:$Trait:path>|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<$T: $Trait> Context<$type> for $T {
|
||||
fn get (&$self, dsl: &Value) -> Option<$type> {
|
||||
use Value::*;
|
||||
Some(match dsl {
|
||||
Num(n) => match *n { 0 => false, _ => true },
|
||||
Sym(":false") | Sym(":f") => false,
|
||||
Sym(":true") | Sym(":t") => true,
|
||||
$(Sym($pat) => $expr,)*
|
||||
_ => return Context::get(self, dsl)
|
||||
})
|
||||
}
|
||||
}
|
||||
};
|
||||
// Provide a value that may also be a numeric literal in the EDN, to a concrete implementation.
|
||||
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl Context<$type> for $State {
|
||||
fn get (&$self, dsl: &Value) -> Option<$type> {
|
||||
use Value::*;
|
||||
Some(match dsl {
|
||||
Num(n) => match *n { 0 => false, _ => true },
|
||||
Sym(":false") | Sym(":f") => false,
|
||||
Sym(":true") | Sym(":t") => true,
|
||||
$(Sym($pat) => $expr,)*
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export] macro_rules! impose {
|
||||
([$self:ident:$Struct:ty] $(($Command:ty : $(($cmd:literal $args:tt $result:expr))*))*) => {
|
||||
$(atom_command!($Command: |$self: $Struct| { $(($cmd $args $result))* });)*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue