macro_rules! postfix { // Terminating rule (() -> ($res:expr)) => {$res}; // Addition ((+ $($rest:tt)*) -> ($a:tt $b:tt $($stack:tt)*)) => { postfix!(($($rest)*) -> (($b + $a) $($stack)*)) }; // Subtraction ((- $($rest:tt)*) -> ($a:tt $b:tt $($stack:tt)*)) => { postfix!(($($rest)*) -> (($b - $a) $($stack)*)) }; // Multiplication ((* $($rest:tt)*) -> ($a:tt $b:tt $($stack:tt)*)) => { postfix!(($($rest)*) -> (($b * $a) $($stack)*)) }; // Divison ((/ $($rest:tt)*) -> ($a:tt $b:tt $($stack:tt)*)) => { postfix!(($($rest)*) -> (($b / $a) $($stack)*)) }; // Push operands onto the stack (($d:tt $($rest:tt)+) -> ($($stack:tt)*)) => { postfix!(($($rest)+) -> ($d $($stack)*)) }; // Start ($($ops:tt)+) => { postfix!(($($ops)+) -> ()) }; } fn main() { let res = postfix!(15 7 1 1 + - / 3 * 2 1 1 + + -); println!("{}", res); }