From 17eaadb280fff998ecbd4d6fd526d91d7e34f559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Fri, 9 Nov 2018 14:22:14 +0100 Subject: [PATCH] [poincare] Replace functions when replacing symbols iteratively --- poincare/include/poincare/function.h | 6 ++++-- poincare/src/function.cpp | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/poincare/include/poincare/function.h b/poincare/include/poincare/function.h index 7cb1e0c01..1d2f7c18b 100644 --- a/poincare/include/poincare/function.h +++ b/poincare/include/poincare/function.h @@ -38,6 +38,7 @@ private: int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override; // Simplification Expression shallowReduce(Context & context, Preferences::AngleUnit angleUnit, bool replaceSymbols = true) override; + Expression replaceReplaceableSymbols(Context & context) override; // Evaluation Evaluation approximate(SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override; Evaluation approximate(DoublePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override; @@ -52,7 +53,8 @@ public: replaceChildAtIndexInPlace(0, child); } static Expression UntypedBuilder(const char * name, size_t length, Expression child, Context * context) { - // create an expression only if it is not in the context or defined as a function + /* Create an expression only if it is not in the context or defined as a + * function */ Function f(name, length, child); if (SymbolAbstract::ValidInContext(f, context)) { return f; @@ -60,9 +62,9 @@ public: return Expression(); } - Expression replaceSymbolWithExpression(const SymbolAbstract & symbol, const Expression & expression); Expression shallowReduce(Context & context, Preferences::AngleUnit angleUnit, bool replaceSymbols = true); + Expression replaceReplaceableSymbols(Context & context); }; } diff --git a/poincare/src/function.cpp b/poincare/src/function.cpp index 8655e91a3..c2b4e3311 100644 --- a/poincare/src/function.cpp +++ b/poincare/src/function.cpp @@ -73,6 +73,10 @@ Expression FunctionNode::shallowReduce(Context & context, Preferences::AngleUnit return Function(this).shallowReduce(context, angleUnit, replaceSymbols); // This uses Symbol::shallowReduce } +Expression FunctionNode::replaceReplaceableSymbols(Context & context) { + return Function(this).replaceReplaceableSymbols(context); +} + Evaluation FunctionNode::approximate(SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const { return templatedApproximate(context, angleUnit); } @@ -134,4 +138,13 @@ Expression Function::shallowReduce(Context & context, Preferences::AngleUnit ang return *this; } +Expression Function::replaceReplaceableSymbols(Context & context) { + Expression e = context.expressionForSymbol(*this); + if (e.isUninitialized()) { + return *this; + } + replaceWithInPlace(e); + return e; +} + }