mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
dsl: add sexpr syntaces; prefix modules
This commit is contained in:
parent
844681d6ad
commit
3d01f5558c
7 changed files with 234 additions and 178 deletions
84
dsl/src/dsl_macros.rs
Normal file
84
dsl/src/dsl_macros.rs
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
use crate::*;
|
||||
|
||||
/// Static iteration helper.
|
||||
#[macro_export] macro_rules! iterate {
|
||||
($expr:expr => $arg: pat => $body:expr) => {
|
||||
let mut iter = $expr;
|
||||
while let Some(($arg, next)) = iter.next() {
|
||||
$body;
|
||||
iter = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Implement the const iterator pattern.
|
||||
#[macro_export] macro_rules! const_iter {
|
||||
($(<$l:lifetime>)?|$self:ident: $Struct:ty| => $Item:ty => $expr:expr) => {
|
||||
impl$(<$l>)? Iterator for $Struct {
|
||||
type Item = $Item;
|
||||
fn next (&mut $self) -> Option<$Item> { $expr }
|
||||
}
|
||||
impl$(<$l>)? ConstIntoIter for $Struct {
|
||||
type Kind = IsIteratorKind;
|
||||
type Item = $Item;
|
||||
type IntoIter = Self;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export] macro_rules! expose {
|
||||
($([$self:ident:$State:ty] $(($Type:ty $(: $(($pat:literal $expr:expr))*)?))*)*) => {
|
||||
$(expose!(@impl [$self: $State] { $([$Type] => { $($($pat => $expr),*)? })* });)*
|
||||
};
|
||||
($([$self:ident:$State:ty] { $([$($Type:tt)*] => { $($pat:pat => $expr:expr),* $(,)? })* })*) => {
|
||||
$(expose!(@impl [$self: $State] { $([$($Type)*] => { $($pat => $expr),* })* });)*
|
||||
};
|
||||
(@impl [$self:ident:$State:ty] { $([$($Type:tt)*] => { $($pat:pat => $expr:expr),* $(,)? })* }) => {
|
||||
$(expose!(@type [$($Type)*] [$self: $State] => { $($pat => $expr),* });)*
|
||||
};
|
||||
(@type [bool] [$self:ident: $State:ty] => { $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
provide_bool!(bool: |$self: $State| { $($pat => $expr),* });
|
||||
};
|
||||
(@type [u16] [$self:ident: $State:ty] => { $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
provide_num!(u16: |$self: $State| { $($pat => $expr),* });
|
||||
};
|
||||
(@type [usize] [$self:ident: $State:ty] => { $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
provide_num!(usize: |$self: $State| { $($pat => $expr),* });
|
||||
};
|
||||
(@type [isize] [$self:ident: $State:ty] => { $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
provide_num!(isize: |$self: $State| { $($pat => $expr),* });
|
||||
};
|
||||
(@type [$Type:ty] [$self:ident: $State:ty] => { $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
provide!($Type: |$self: $State| { $($pat => $expr),* });
|
||||
};
|
||||
}
|
||||
|
||||
#[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))* });)*
|
||||
};
|
||||
([$self:ident:$Struct:ty] { $($Command:ty => $variants:tt)* }) => {
|
||||
$(atom_command!($Command: |$self: $Struct| $variants);)*
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export] macro_rules! get_value {
|
||||
($state:expr => $token:expr) => {
|
||||
if let Some(value) = $state.get(&$token.value) {
|
||||
value
|
||||
} else {
|
||||
panic!("no value corresponding to {:?}", &$token.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export] macro_rules! get_content {
|
||||
($state:expr => $token:expr) => {
|
||||
if let Some(content) = $state.get_content(&$token.value) {
|
||||
content
|
||||
} else {
|
||||
panic!("no content corresponding to {:?}", &$token.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue