From 1351babe375e290c7f518dbc7127c7ad22623741 Mon Sep 17 00:00:00 2001 From: Ruben Dashyan Date: Fri, 20 Mar 2020 10:25:16 +0100 Subject: [PATCH] [poincare] Move Power vs Unit checks to Power::shallowReduce --- poincare/src/power.cpp | 15 ++++++++++++--- poincare/src/unit.cpp | 6 ------ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/poincare/src/power.cpp b/poincare/src/power.cpp index d6f27e7bb..d66ab2273 100644 --- a/poincare/src/power.cpp +++ b/poincare/src/power.cpp @@ -387,9 +387,18 @@ Expression Power::shallowReduce(ExpressionNode::ReductionContext reductionContex Expression base = childAtIndex(0); Expression index = childAtIndex(1); - // Step 1: There should be no unit in the index! - if (index.hasUnit()) { - return replaceWithUndefinedInPlace(); + // Step 1: Handle the units + { + if (index.hasUnit()) { + // There must be no unit in the exponent + return replaceWithUndefinedInPlace(); + } + if (base.hasUnit()) { + if (index.type() != ExpressionNode::Type::Rational || !static_cast(index).isInteger()) { + // The exponent must be an Integer + return replaceWithUndefinedInPlace(); + } + } } // Step 2: Handle matrices diff --git a/poincare/src/unit.cpp b/poincare/src/unit.cpp index f8617f0df..d461ccb17 100644 --- a/poincare/src/unit.cpp +++ b/poincare/src/unit.cpp @@ -323,12 +323,7 @@ Expression Unit::shallowBeautify(ExpressionNode::ReductionContext reductionConte m.addChildAtIndexInPlace(*this, 1, 1); return std::move(m); } - // Check that the exponent, if any, of a Unit is an integer if (!ancestor.isUninitialized() && ancestor.type() == ExpressionNode::Type::Power) { - Expression exponent = ancestor.childAtIndex(1); - if (!(exponent.type() == ExpressionNode::Type::Rational && static_cast(exponent).isInteger())) { - goto UnitCheckUnsuccessful; - } ancestor = ancestor.parent(); } /* Check homogeneity: at this point, ancestor must be @@ -344,7 +339,6 @@ Expression Unit::shallowBeautify(ExpressionNode::ReductionContext reductionConte if (ancestor.isUninitialized()) { return *this; } - UnitCheckUnsuccessful: /* If the latter checks are not successfully passed, then the function * returns replaceWithUndefinedInPlace. * TODO Something else should be returned in order to report a more