Skip to content

Instantly share code, notes, and snippets.

@cympfh
Last active July 3, 2017 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cympfh/44a535947bda0d2bae8ee1234759dadb to your computer and use it in GitHub Desktop.
Save cympfh/44a535947bda0d2bae8ee1234759dadb to your computer and use it in GitHub Desktop.
S-expression in Rust macro
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);
}
@cympfh
Copy link
Author

cympfh commented Jul 3, 2017

rustc -Z unstable-options --pretty expanded macro.lisp.rs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment