From 2d2aa03bcb8cb69b0235cc3ade4baad0f14cc5d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Tue, 15 Nov 2016 11:57:40 +0100 Subject: [PATCH] [poincare] In context, return 0 if constant has not been assigned Change-Id: I3a338a7de54c55500982a8a0dd32a1193f8db504 --- poincare/include/poincare/context.h | 4 ++++ poincare/src/context.cpp | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/poincare/include/poincare/context.h b/poincare/include/poincare/context.h index 49160a702..81ad82af8 100644 --- a/poincare/include/poincare/context.h +++ b/poincare/include/poincare/context.h @@ -3,6 +3,9 @@ #include #include +#include + +class Integer; //TODO: We should probably make a COPY of the expressions we store @@ -16,6 +19,7 @@ class Context { int symbolIndex(const Symbol * symbol) const; static constexpr uint16_t k_maxNumberOfExpressions = 26; Expression * m_expressions[k_maxNumberOfExpressions]; + static Float * defaultExpression(); }; #endif diff --git a/poincare/src/context.cpp b/poincare/src/context.cpp index b2e65f126..4b51f1bbc 100644 --- a/poincare/src/context.cpp +++ b/poincare/src/context.cpp @@ -8,14 +8,24 @@ Context::Context() } } +Float * Context::defaultExpression() { + static Float * defaultExpression = new Float(0); + return defaultExpression; +} + int Context::symbolIndex(const Symbol * symbol) const { int index = symbol->name() - 'A'; - assert(index < k_maxNumberOfExpressions); return index; } const Expression * Context::expressionForSymbol(const Symbol * symbol) { int index = symbolIndex(symbol); + if (index < 0 || index >= k_maxNumberOfExpressions) { + return nullptr; + } + if (m_expressions[index] == nullptr) { + return defaultExpression(); + } return m_expressions[index]; }