[apps/poincare] Expressions are not reduced before storing in Storage

This commit is contained in:
Léa Saviot
2018-11-14 11:55:09 +01:00
committed by Émilie Feral
parent 1c859fd1df
commit ff20cb840d
4 changed files with 14 additions and 21 deletions

View File

@@ -21,7 +21,7 @@ const char * ExpressionModel::text() const {
Poincare::Expression ExpressionModel::expression(Poincare::Context * context) const {
if (m_expression.isUninitialized()) {
m_expression = PoincareHelpers::ParseAndSimplify(m_text, *context);
m_expression = Expression::parse(m_text);
}
return m_expression;
}

View File

@@ -69,11 +69,8 @@ Ion::Storage::Record::ErrorStatus StorageExpressionModel::setContent(const char
Expression expressionToStore;
// if c = "", we want to reinit the Expression
if (c && *c != 0) {
/* We do not want to replace any symbols in the stored expression, so we don't
* need the current context. */
GlobalContext context;
// Compute the expression to store
expressionToStore = PoincareHelpers::ParseAndSimplify(c, context, false);
// Compute the expression to store, without replacing symbols
expressionToStore = Expression::parse(c);
if (!expressionToStore.isUninitialized()) {
Symbol xUnknown = Symbol(Symbol::SpecialSymbols::UnknownX);
expressionToStore = expressionToStore.replaceSymbolWithExpression(Symbol("x", 1), xUnknown);

View File

@@ -16,11 +16,11 @@ extern "C" {
namespace Poincare {
void StoreNode::reduceChildren(Context & context, Preferences::AngleUnit angleUnit, bool replaceSymbols) {
Expression(this).defaultReduceChildren(context, angleUnit, false);
return;
}
void StoreNode::deepReduceChildren(Context & context, Preferences::AngleUnit angleUnit, bool replaceSymbols) {
Expression(this).defaultDeepReduceChildren(context, angleUnit, false);
return;
}
Expression StoreNode::shallowReduce(Context & context, Preferences::AngleUnit angleUnit, bool replaceSymbols) {
@@ -45,11 +45,8 @@ Evaluation<T> StoreNode::templatedApproximate(Context& context, Preferences::Ang
* Otherwise, it would have been replaced by its symbol. We thus have to
* setExpressionForSymbol. */
Store s(this);
Expression expressionToStore = s.value().clone().simplify(context, angleUnit, false);
if (expressionToStore.isUninitialized() || expressionToStore.isUndefined()) {
expressionToStore = s.value();
}
context.setExpressionForSymbol(expressionToStore, s.symbol(), context);
assert(!s.value().isUninitialized());
context.setExpressionForSymbol(s.value(), s.symbol(), context);
Expression e = context.expressionForSymbol(s.symbol());
if (e.isUninitialized()) {
return Complex<T>::Undefined();
@@ -69,11 +66,8 @@ Expression Store::shallowReduce(Context & context, Preferences::AngleUnit angleU
assert(symbol().type() == ExpressionNode::Type::Symbol);
finalValue = childAtIndex(0);
}
Expression expressionToStore = finalValue.clone().simplify(context, angleUnit, false);
if (expressionToStore.isUninitialized() || expressionToStore.isUndefined()) {
expressionToStore = finalValue;
}
context.setExpressionForSymbol(expressionToStore, symbol(), context);
assert(!finalValue.isUninitialized());
context.setExpressionForSymbol(finalValue, symbol(), context);
Expression e = context.expressionForSymbol(symbol());
if (e.isUninitialized()) {
return Undefined();

View File

@@ -127,6 +127,8 @@ Expression SymbolNode::replaceReplaceableSymbols(Context & context) {
template<typename T>
Evaluation<T> 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);
if (e.isUninitialized()) {
return Complex<T>::Undefined();
@@ -168,9 +170,9 @@ Expression Symbol::shallowReduce(Context & context, Preferences::AngleUnit angle
return *this;
}
Expression result = context.expressionForSymbol(*this);
// The stored expression is beautified, so we need to call reduce
/* First, replace all the symbols iteratively. This prevents a memory
* failure symbols are defined circularly. */
/* 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);
if (result.isUninitialized()) {
return *this;