Debian Codegraph @

tests/run-pass/arithmetic_infix.rs

raw symbols SCIP
1
2 extern crate peg;
3
4 peg::parser!( grammar arithmetic() for str {
5 rule number() -> i64
6 = n:$(['0'..='9']+) { n.parse().unwrap() }
7
8 pub(crate) rule calculate() -> i64 = precedence!{
9 x:(@) "+" y:@ { x + y }
10 x:(@) "-" y:@ { x - y }
11 "-" v:@ { - v }
12 --
13 x:(@) "*" y:@ { x * y }
14 x:(@) "/" y:@ { x / y }
15 --
16 x:@ "^" y:(@) { x.pow(y as u32) }
17 v:@ "!" { (1..v+1).product() }
18 --
19 "(" v:calculate() ")" { v }
20 n:number() {n}
21 }
22 });
23
24 fn main() {
25 assert_eq!(arithmetic::calculate("3+3*3+3"), Ok(15));
26 assert_eq!(arithmetic::calculate("2+2^2^2^2/2+2"), Ok(32772));
27 assert_eq!(arithmetic::calculate("1024/2/2/2+1"), Ok(129));
28 assert_eq!(arithmetic::calculate("1024/(1+1)/2/2+1"), Ok(129));
29 assert_eq!(arithmetic::calculate("-1-2*-2"), Ok(3));
30 assert_eq!(arithmetic::calculate("1+3!+1"), Ok(8));
31 }