From 3bee0f35fac5b1d1e53ce1abb1e1ec407779d4e0 Mon Sep 17 00:00:00 2001 From: Ruben Dashyan Date: Fri, 2 Nov 2018 18:15:07 +0100 Subject: [PATCH] [poincare] Add UntypedBuilder Function and Symbol UntypedBuilders moreover retrieve Expression value from context. --- poincare/include/poincare/function.h | 9 +++++++++ poincare/include/poincare/symbol.h | 8 ++++++++ poincare/include/poincare/symbol_abstract.h | 5 +++++ 3 files changed, 22 insertions(+) diff --git a/poincare/include/poincare/function.h b/poincare/include/poincare/function.h index bd49e8fd8..7cb1e0c01 100644 --- a/poincare/include/poincare/function.h +++ b/poincare/include/poincare/function.h @@ -51,6 +51,15 @@ public: Function(const char * name, size_t length, Expression child) : Function(name, length) { 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 + Function f(name, length, child); + if (SymbolAbstract::ValidInContext(f, context)) { + return f; + } + return Expression(); + } + Expression replaceSymbolWithExpression(const SymbolAbstract & symbol, const Expression & expression); Expression shallowReduce(Context & context, Preferences::AngleUnit angleUnit, bool replaceSymbols = true); diff --git a/poincare/include/poincare/symbol.h b/poincare/include/poincare/symbol.h index 87d51c6d7..07f7389ae 100644 --- a/poincare/include/poincare/symbol.h +++ b/poincare/include/poincare/symbol.h @@ -57,6 +57,14 @@ public: Symbol(char name); Symbol(const SymbolNode * node) : SymbolAbstract(node) {} + static Expression UntypedBuilder(const char * name, size_t length, Context * context) { + // create an expression only if it is not in the context or defined as a symbol + Symbol s(name, length); + if (SymbolAbstract::ValidInContext(s, context)) { + return s; + } + return Expression(); + } // Symbol properties static bool isSeriesSymbol(const char * c); static bool isRegressionSymbol(const char * c); diff --git a/poincare/include/poincare/symbol_abstract.h b/poincare/include/poincare/symbol_abstract.h index 7b34f371e..a8ead1f62 100644 --- a/poincare/include/poincare/symbol_abstract.h +++ b/poincare/include/poincare/symbol_abstract.h @@ -56,6 +56,11 @@ class SymbolAbstract : public Expression { public: const char * name() const { return node()->name(); } static size_t TruncateExtension(char * dst, const char * src, size_t len); + static bool ValidInContext(SymbolAbstract & s, Context * context) { + // Retrive from context the expression corresponding to s + Expression f = context ? context->expressionForSymbol(s) : Expression(); + return f.isUninitialized() || f.type() == s.type(); + } constexpr static size_t k_maxNameSize = 8; protected: SymbolAbstract(const SymbolAbstractNode * node) : Expression(node) {}