[poincare] In context, return 0 if constant has not been assigned

Change-Id: I3a338a7de54c55500982a8a0dd32a1193f8db504
This commit is contained in:
Émilie Feral
2016-11-15 11:57:40 +01:00
parent fc10dd1410
commit 2d2aa03bcb
2 changed files with 15 additions and 1 deletions

View File

@@ -3,6 +3,9 @@
#include <poincare/expression.h>
#include <poincare/symbol.h>
#include <poincare/float.h>
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

View File

@@ -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];
}