[Sequence] Changed the way sequences are parsed

Change-Id: If19b46a317e4f336ac857690827ab08e147ac75a
This commit is contained in:
Arthur Camouseigt
2020-07-22 17:22:37 +02:00
committed by Émilie Feral
parent 8434da60ef
commit ed358590ce
18 changed files with 271 additions and 147 deletions

View File

@@ -19,29 +19,24 @@ CacheContext<T>::CacheContext(Context * parentContext) :
template<typename T>
const Expression CacheContext<T>::expressionForSymbolAbstract(const SymbolAbstract & symbol, bool clone) {
// [u|v|w](n(+1)?)
// u,v,w are reserved names. They can only be set through the sequence app
if (symbol.type() == ExpressionNode::Type::Symbol
&& symbol.name()[0] >= SequenceStore::k_sequenceNames[0][0]
&& symbol.name()[0] <= SequenceStore::k_sequenceNames[MaxNumberOfSequences-1][0]) {
assert((symbol.name()+1)[0] == '(');
if (symbol.type() == ExpressionNode::Type::Sequence) {
Symbol s = const_cast<Symbol &>(static_cast<const Symbol &>(symbol));
if (strcmp(symbol.name()+1, "(n)") == 0 || strcmp(symbol.name()+1, "(n+1)") == 0) {
return Float<T>::Builder(m_values[nameIndexForSymbol(s)][rankIndexForSymbol(s)]);
if (s.childAtIndex(0).type() == ExpressionNode::Type::Symbol) {
return Float<T>::Builder(m_values[nameIndexForSymbol(s)][0]);
} else if (s.childAtIndex(0).type() == ExpressionNode::Type::Addition) {
return Float<T>::Builder(m_values[nameIndexForSymbol(s)][1]);
} else {
Sequence seq = m_sequenceContext->sequenceStore()->sequenceAtIndex(nameIndexForSymbol(s));
// In case the sequence referenced is not defined, return NAN
if (seq.fullName() == nullptr) {
return Float<T>::Builder(NAN);
}
int numberOfDigits = 1;
constexpr int offset = 2; // 2 = 1 for ('u') + 1 for ('(')
while (symbol.name()[offset+numberOfDigits] != ')') {
numberOfDigits++;
Expression rank = symbol.childAtIndex(0);
if (rank.isNumber()) {
return Float<T>::Builder(seq.valueAtRank<T>(rank.approximateToScalar<double>(this, Poincare::Preferences::ComplexFormat::Cartesian, Poincare::Preferences::AngleUnit::Radian), m_sequenceContext));
} else {
return Float<T>::Builder(NAN);
}
// Get the value of k in u(k) and store it in x
Integer integer(symbol.name()+2, numberOfDigits, false);
T x = integer.approximate<T>();
return Float<T>::Builder(seq.valueAtRank<T>(x, m_sequenceContext));
}
}
return ContextWithParent::expressionForSymbolAbstract(symbol, clone);
@@ -54,7 +49,7 @@ void CacheContext<T>::setValueForSymbol(T value, const Poincare::Symbol & symbol
template<typename T>
int CacheContext<T>::nameIndexForSymbol(const Poincare::Symbol & symbol) {
assert(strlen(symbol.name()) >= 4); // [u|v|w](n(+1) or k ?)
assert(symbol.name()[0] >= 'u' && symbol.name()[0] <= 'w'); // [u|v|w]
char name = symbol.name()[0];
assert(name >= SequenceStore::k_sequenceNames[0][0] && name <= SequenceStore::k_sequenceNames[MaxNumberOfSequences-1][0]); // u, v or w
return name - 'u';