From 4e7f8d81bf9cfdf00a9ed1d4a43979bea404618f Mon Sep 17 00:00:00 2001 From: Felix Raimundo Date: Mon, 11 Apr 2016 16:40:05 +0200 Subject: [PATCH] Fix the recursiveness of simplification. Change-Id: I090f8bd14a85ff46d6cb9807e5a1204894a5c152 --- poincare/src/expression.cpp | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/poincare/src/expression.cpp b/poincare/src/expression.cpp index 35ae077c3..be72325ed 100644 --- a/poincare/src/expression.cpp +++ b/poincare/src/expression.cpp @@ -34,23 +34,28 @@ Expression * Expression::simplify() { return this->clone(); } - /* We recursively simplify the children expressions. - * Note that we are sure to get the samne number of children as we had before - */ - Expression ** simplifiedOperands = (Expression**) malloc(this->numberOfOperands() * sizeof(Expression*)); - for (int i = 0; i < this->numberOfOperands(); i++) { - simplifiedOperands[i] = this->operand(i)->simplify(); - } - - /* Note that we don't need to clone the simplified children because they are - * already cloned before. */ - Expression * result = this->cloneWithDifferentOperands(simplifiedOperands, this->numberOfOperands(), false); - - // The table is no longer needed. - free(simplifiedOperands); + Expression * result = this->clone(); + Expression * tmp = nullptr; bool simplification_pass_was_useful = true; while (simplification_pass_was_useful) { + /* We recursively simplify the children expressions. + * Note that we are sure to get the samne number of children as we had before + */ + Expression ** simplifiedOperands = (Expression**) malloc(result->numberOfOperands() * sizeof(Expression*)); + for (int i = 0; i < result->numberOfOperands(); i++) { + simplifiedOperands[i] = result->operand(i)->simplify(); + } + + /* Note that we don't need to clone the simplified children because they are + * already cloned before. */ + tmp = result->cloneWithDifferentOperands(simplifiedOperands, result->numberOfOperands(), false); + delete result; + result = tmp; + + // The table is no longer needed. + free(simplifiedOperands); + simplification_pass_was_useful = false; for (int i=0; i