diff --git a/poincare/include/poincare/symbol.h b/poincare/include/poincare/symbol.h index a91cfabe5..f61023106 100644 --- a/poincare/include/poincare/symbol.h +++ b/poincare/include/poincare/symbol.h @@ -82,6 +82,7 @@ public: Expression replaceReplaceableSymbols(Context & context); private: SymbolNode * node() const { return static_cast(Expression::node()); } + Expression expand(Context & context) const; }; } diff --git a/poincare/src/symbol.cpp b/poincare/src/symbol.cpp index 6454ef600..f15f1145b 100644 --- a/poincare/src/symbol.cpp +++ b/poincare/src/symbol.cpp @@ -126,10 +126,7 @@ Expression SymbolNode::replaceReplaceableSymbols(Context & context) { template Evaluation SymbolNode::templatedApproximate(Context& context, Preferences::AngleUnit angleUnit) const { - Expression e = context.expressionForSymbol(Symbol(this)); - /* Replace all the symbols iteratively. This prevents a memory failure when - * symbols are defined circularly. */ - e = Expression::ExpressionWithoutSymbols(e, context); + Expression e = Symbol(this).expand(context); if (e.isUninitialized()) { return Complex::Undefined(); } @@ -159,8 +156,7 @@ bool Symbol::isRegressionSymbol(const char * c) { } bool Symbol::matches(ExpressionTest test, Context & context) const { - Expression e = context.expressionForSymbol(*this); - e = ExpressionWithoutSymbols(e, context); + Expression e = expand(context); return !e.isUninitialized() && test(e, context, true); } @@ -168,15 +164,12 @@ Expression Symbol::shallowReduce(Context & context, Preferences::AngleUnit angle if (!replaceSymbols) { return *this; } - Expression result = context.expressionForSymbol(*this); - /* The stored expression is as entered by the user, so we need to call reduce - * First, replace all the symbols iteratively. This prevents a memory failure - * when symbols are defined circularly. */ - result = ExpressionWithoutSymbols(result, context); + Expression result = expand(context); if (result.isUninitialized()) { return *this; } replaceWithInPlace(result); + // The stored expression is as entered by the user, so we need to call reduce return result.deepReduce(context, angleUnit); } @@ -215,4 +208,11 @@ Expression Symbol::replaceReplaceableSymbols(Context & context) { return e; } +Expression Symbol::expand(Context & context) const { + /* Replace all the symbols iteratively. This prevents a memory failure when + * symbols are defined circularly. */ + Expression e = context.expressionForSymbol(*this); + return ExpressionWithoutSymbols(e, context); +} + }