diff --git a/poincare/src/expression_node.cpp b/poincare/src/expression_node.cpp index b1399f073..66ba5d07e 100644 --- a/poincare/src/expression_node.cpp +++ b/poincare/src/expression_node.cpp @@ -12,8 +12,14 @@ TreeNode * ExpressionNode::FailedAllocationStaticNode() { ExpressionReference ExpressionNode::replaceSymbolWithExpression(char symbol, ExpressionReference expression) { ExpressionReference reference(this); - for (int i = 0; i < reference.numberOfChildren(); i++) { - reference.replaceTreeChildAtIndex(i, reference.childAtIndex(i).node()->replaceSymbolWithExpression(symbol, expression)); + int nbChildren = reference.numberOfChildren(); + for (int i = 0; i < nbChildren; i++) { + ExpressionReference newChild = reference.childAtIndex(i).node()->replaceSymbolWithExpression(symbol, expression); + if (reference.numberOfChildren() < nbChildren) { + reference.addChildTreeAtIndex(newChild, i, nbChildren - 1); + } else { + reference.replaceTreeChildAtIndex(i, newChild); + } } return reference; } diff --git a/poincare/src/expression_reference.cpp b/poincare/src/expression_reference.cpp index 4b09b17af..6af3a826d 100644 --- a/poincare/src/expression_reference.cpp +++ b/poincare/src/expression_reference.cpp @@ -107,7 +107,7 @@ bool ExpressionReference::getLinearCoefficients(char * variables, ExpressionRefe } x++; } - ExpressionReference equation = *this; + ExpressionReference equation = clone(); x = variables; int index = 0; ExpressionReference polynomialCoefficients[k_maxNumberOfPolynomialCoefficients]; @@ -198,16 +198,28 @@ void ExpressionReference::Simplify(ExpressionReference * expressionAddress, Cont } ExpressionReference ExpressionReference::deepReduce(Context & context, Preferences::AngleUnit angleUnit) { - for (int i = 0; i < this->numberOfChildren(); i++) { - replaceTreeChildAtIndex(i, childAtIndex(i).deepReduce(context, angleUnit)); + int nbChildren = this->numberOfChildren(); + for (int i = 0; i < nbChildren; i++) { + ExpressionReference reducedChild = childAtIndex(i).deepReduce(context, angleUnit); + if (numberOfChildren() < nbChildren) { + addChildTreeAtIndex(reducedChild, i, nbChildren-1); + } else { + replaceTreeChildAtIndex(i, reducedChild); + } } return shallowReduce(context, angleUnit); } ExpressionReference ExpressionReference::deepBeautify(Context & context, Preferences::AngleUnit angleUnit) { ExpressionReference beautifiedExpression = shallowBeautify(context, angleUnit); - for (int i = 0; i < beautifiedExpression.numberOfChildren(); i++) { - beautifiedExpression.replaceTreeChildAtIndex(i, beautifiedExpression.childAtIndex(i).deepBeautify(context, angleUnit)); + int nbChildren = beautifiedExpression.numberOfChildren(); + for (int i = 0; i < nbChildren; i++) { + ExpressionReference beautifiedChild = beautifiedExpression.childAtIndex(i).deepBeautify(context, angleUnit); + if (beautifiedExpression.numberOfChildren() < nbChildren) { + beautifiedExpression.addChildTreeAtIndex(beautifiedChild, i, nbChildren-1); + } else { + beautifiedExpression.replaceTreeChildAtIndex(i, beautifiedChild); + } } return beautifiedExpression; } diff --git a/poincare/src/rational.cpp b/poincare/src/rational.cpp index fca5c59a6..b394678ec 100644 --- a/poincare/src/rational.cpp +++ b/poincare/src/rational.cpp @@ -127,11 +127,12 @@ int RationalNode::simplificationOrderSameType(const ExpressionNode * e, bool can // Simplification ExpressionReference RationalNode::shallowBeautify(Context & context, Preferences::AngleUnit angleUnit) { + ExpressionReference reference(this); if (m_negative) { m_negative = false; - return OppositeReference(ExpressionReference(this)); + return OppositeReference(reference); } - return ExpressionReference(this); + return reference; } ExpressionReference RationalNode::cloneDenominator(Context & context, Preferences::AngleUnit angleUnit) const {