From 4750bd40dd84286ee2b66c3d140ac614a00975df Mon Sep 17 00:00:00 2001 From: Eivind Kvedalen Date: Sun, 29 Jan 2017 22:19:39 +0100 Subject: [PATCH] Expressions: Generalized FunctionExpression::simplify(...) function. --- src/App/Expression.cpp | 46 +++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/src/App/Expression.cpp b/src/App/Expression.cpp index 6574243976..d068f7da1f 100644 --- a/src/App/Expression.cpp +++ b/src/App/Expression.cpp @@ -1116,39 +1116,29 @@ Expression * FunctionExpression::eval() const Expression *FunctionExpression::simplify() const { - Expression * v1 = args[0]->simplify(); + size_t numerics = 0; + std::vector a; - // Argument simplified to numeric expression? Then return evaluate and return - if (freecad_dynamic_cast(v1)) { - switch (f) { - case ATAN2: - case MOD: - case POW: { - Expression * v2 = args[1]->simplify(); + // Try to simplify each argument to function + for (auto it = args.begin(); it != args.end(); ++it) { + Expression * v = (*it)->simplify(); + + if (freecad_dynamic_cast(v)) + ++numerics; + a.push_back(v); + } + + if (numerics == args.size()) { + // All constants, then evaluation must also be constant + + // Clean-up + for (auto it = args.begin(); it != args.end(); ++it) + delete *it; - if (freecad_dynamic_cast(v2)) { - delete v1; - delete v2; - return eval(); - } - else { - std::vector a; - a.push_back(v1); - a.push_back(v2); - return new FunctionExpression(owner, f, a); - } - } - default: - break; - } - delete v1; return eval(); } - else { - std::vector a; - a.push_back(v1); + else return new FunctionExpression(owner, f, a); - } } /**