rstm_core/macros/
program.rs

1/*
2    Appellation: program <module>
3    Created At: 2025.08.30:17:01:39
4    Contrib: @FL03
5*/
6#![cfg(feature = "macros")]
7
8/// The [`program!`] macro facilitates the creation of new [`Program`](crate::programs::Program)
9/// instances using familiar syntax
10///
11/// ```ignore
12/// program! {
13///     #[default_state(initial_state)] // optional
14///     rules: {(state, symbol) -> direction(next_state, write_symbol); ...};
15/// }
16/// ```
17///
18/// ## Basic Usage
19///
20/// The following example demonstrates the usage of the macro to create a program using three
21/// states `{-1, 0, 1}` and two symbols `{0, 1}`.
22///
23/// ```rust
24/// let rule = rstm_core::program! {
25///     #[default_state(0)]
26///     rules: {
27///         (0, 0) -> Right(1, 1),
28///         (0, 1) -> Left(-1, 0),
29///         (1, 0) -> Right(1, 1),
30///         (1, 1) -> Left(-1, 1),
31///         (-1, 0) -> Right(0, 0),
32///         (-1, 1) -> Left(0, 1),
33///     };
34/// };
35/// ```
36#[cfg(feature = "alloc")]
37#[macro_export]
38macro_rules! program {
39    {
40        $(#[default_state($ds:expr)])?
41        rules: {$(($state:expr, $symbol:expr) -> $direction:ident($next:expr, $write:expr)),* $(,)?} $(;)?
42    } => {
43        $crate::programs::Program::from_iter(
44            $crate::ruleset! [$(($state, $symbol) -> $direction($next, $write)),*]
45        )
46        $(.with_default_state($ds))?
47    };
48}