diff --git a/src/parser/expression/expression.rs b/src/parser/expression/expression.rs index 11a6e21..6eeff90 100644 --- a/src/parser/expression/expression.rs +++ b/src/parser/expression/expression.rs @@ -46,6 +46,41 @@ impl Expression { } } + // True if this is a power operator applied to a constant or variable + // and an integer. + // Examples: pi^2, x ^ 3 + pub fn is_poly_power(&self) -> bool { + match self { + Expression::Operator(_, Operator::Power, a) => { + // Assuming len(a) = 2, which should be true in this case + assert!(a.len() == 2); + + let base = &a[0]; + let power = &a[1]; + + // Make sure base ks const or variable + match base { + Expression::Constant(_, _) + | Expression::Variable(_, _) + => {}, + + _ => { return false; } + }; + + // Make sure power is an integer + match power { + Expression::Quantity(_, q) => { + return q.unitless() && q.fract().is_zero(); + }, + _ => { return false; } + } + + + }, + _ => { return false; } + }; + } + #[inline(always)] pub fn get_args_mut(&mut self) -> Option<&mut VecDeque> { match self { diff --git a/src/parser/expression/operator.rs b/src/parser/expression/operator.rs index fa84056..1aee6f6 100644 --- a/src/parser/expression/operator.rs +++ b/src/parser/expression/operator.rs @@ -272,7 +272,11 @@ impl Operator { p.unitless() && !q.unitless() } else if let Expression::Constant(_, _) = b { true - } else {false} + } else if let Expression::Variable(_, _) = b { + true + } else { + b.is_poly_power() + } } else {false} };