1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
use crate::parser::fn_call::parse_fn_call; use crate::parser::naive_string::parse_naive_string; use crate::CalcError; use pest::iterators::{Pair, Pairs}; use crate::{ expr::val::Val, expr::{Expr, Op}, parser::{parse_unit_expr, Rule}, }; use crate::latex::ToLaTeX; pub fn parse_expr(r: Pair<Rule>) -> Result<Expr, CalcError> { assert_eq!(r.as_rule(), Rule::expression); fn expr_bp(inp: &mut Pairs<Rule>, bp: u8) -> Result<Expr, CalcError> { if let Some(nx) = inp.next() { let mut lhs = match nx.as_rule() { Rule::number => Expr::Atom(Val::empty(nx.as_str().trim().parse::<f64>().unwrap())), Rule::ident => Expr::Ident(nx.as_str().trim().to_string()), Rule::fn_call => Expr::FnCall(parse_fn_call(nx)?), Rule::expression => parse_expr(nx)?, _ => { dbg!(nx); unreachable!(); } }; while let Some(nx) = inp.peek() { let op = match nx.as_rule() { Rule::operation => match nx.as_str().trim() { "+" => Op::Plus, "-" => Op::Minus, "*" => Op::Mul, "/" => Op::Div, "^" => Op::Exp, _ => panic!("Bad operator {}", nx.as_str().trim()), }, Rule::unit_expr => { let naive_expr = parse_naive_string(nx.clone())?.to_latex()?; let unit_expr = parse_unit_expr(nx)?; Op::AddUnit(unit_expr.eval(), naive_expr.to_string()) } _ => todo!(), }; if let Some((l_bp, ())) = postfix_binding_power(&op) { if l_bp < bp { break; } inp.next(); lhs = Expr::Cons(op, vec![lhs]); continue; } let (l_bp, r_bp) = infix_binding_power(&op); if l_bp < bp { break; } inp.next(); let rhs = expr_bp(inp, r_bp)?; lhs = Expr::Cons(op, vec![lhs, rhs]); } Ok(lhs) } else { unreachable!() } } expr_bp(&mut r.into_inner(), 0) } fn postfix_binding_power(op: &Op) -> Option<(u8, ())> { Some(match op { Op::AddUnit(_, _) => (6, ()), _ => return None, }) } fn infix_binding_power(op: &Op) -> (u8, u8) { match op { Op::Plus | Op::Minus => (1, 2), Op::Mul | Op::Div => (3, 4), Op::Exp => (7, 8), _ => panic!(), } } #[cfg(test)] mod test { use super::*; use crate::{ expr::unit::Unit, parser::{expr::parse_expr, MathParser, Rule}, statement::Scope, }; use pest::Parser; fn full_eval(s: &str) -> Val { parse_expr( MathParser::parse(Rule::expression, s) .unwrap() .next() .unwrap(), ) .unwrap() .eval(&Scope::default()) .unwrap() } impl PartialEq<&str> for Val { fn eq(&self, s: &&str) -> bool { &self.to_string().as_str() == s } } #[test] fn basic_sub() { assert_eq!(full_eval("5 - 3"), "2"); assert_eq!(full_eval("5 m - 1 m"), "4 m"); assert_eq!(full_eval("5 grams - 3 g"), "2 g"); } #[test] fn basic_add() { assert_eq!(full_eval("5 + 3"), "8"); assert_eq!(full_eval("5 m + 1 m"), "6 m"); assert_eq!(full_eval("5 grams + 3 g"), "8 g"); } #[test] fn basic_div() { assert_eq!(full_eval("4 / 2"), "2"); assert_eq!(full_eval("9 m / 3 meters"), "3"); assert_eq!(full_eval("12 grams / 4 g"), "3"); } #[test] fn basic_mult() { assert_eq!(full_eval("4 * 2"), "8"); assert_eq!(full_eval("2 m * 3 meters"), "6 m^2"); assert_eq!(full_eval("1 grams * 4 g"), "4 g^2"); } #[test] fn complex_sub() { assert_eq!(full_eval("2 N - 0.5 N"), "1500 m g s^-2"); assert_eq!(full_eval("2 N"), "2000 m g s^-2"); assert_eq!(full_eval("2 kN - 1 centinewton"), "1999990 m g s^-2"); } #[test] fn complex_add() { assert_eq!(full_eval("2 N + 0.5 N"), "2500 m g s^-2"); assert_eq!(full_eval("2 kN + 1 centinewton"), "2000010 m g s^-2"); } #[test] fn complex_add_neg() { assert_eq!(full_eval("2 N + -0.5 N"), "1500 m g s^-2"); assert_eq!(full_eval("2 kN + -1 centinewton"), "1999990 m g s^-2"); } }