Last active
July 3, 2017 12:34
-
-
Save cympfh/44a535947bda0d2bae8ee1234759dadb to your computer and use it in GitHub Desktop.
S-expression in Rust macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro_rules! plus { | |
() => {0}; | |
($car:expr $(, $cdr:expr)*) => { $car + plus!($($cdr),*) }; | |
} | |
macro_rules! mul { | |
() => {1}; | |
($car:expr $(, $cdr:expr)*) => {$car * mul!($($cdr),*)}; | |
} | |
macro_rules! eval { | |
( [ $($e:tt)* ] ) => { eval!( $($e)* ) }; | |
(+ $($e:tt)*) => { | |
plus!($( eval!($e) ),*) | |
}; | |
(* $($e:tt)*) => { | |
mul!($( eval!($e) ),*) | |
}; | |
($e:expr) => { $e }; | |
} | |
fn main() { | |
let x = eval![* 1 2 [+ 1 2] [*]]; | |
println!("{}", x); | |
} |
Author
cympfh
commented
Jul 3, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment