calculatex/expr/
unit_expr.rs1use crate::expr::unit::Unit;
2
3#[derive(Debug, Clone)]
4pub enum UnitExpr {
5 Atom(Unit),
6 Cons(UnitOp, Vec<UnitExpr>),
7}
8
9#[derive(Debug, Clone)]
10pub enum UnitOp {
11 Mul,
12 Div,
13 Exp(i64),
14}
15
16impl UnitExpr {
17 pub fn eval(&self) -> Unit {
18 match self {
19 UnitExpr::Atom(u) => u.clone(),
20 UnitExpr::Cons(op, xs) => match (op, xs.as_slice()) {
21 (UnitOp::Mul, [a, b]) => a.eval() * b.eval(),
22 (UnitOp::Div, [a, b]) => a.eval() / b.eval(),
23 (UnitOp::Exp(p), [a]) => a.eval().pow(*p as i64),
24 _ => panic!(),
25 },
26 }
27 }
28}