diff --git a/poincare/include/poincare/expression.h b/poincare/include/poincare/expression.h index 912bdf1a4..e5a48e375 100644 --- a/poincare/include/poincare/expression.h +++ b/poincare/include/poincare/expression.h @@ -15,62 +15,62 @@ class Evaluation; class Expression { public: enum class Type : uint8_t { - Integer = 0, - Complex, - Symbol, - Parenthesis, - Opposite, + AbsoluteValue = 0, Addition, - Subtraction, - Multiplication, - Division, - Power, - Sum, - Product, - DivisionQuotient, - DivisionRemainder, - GreatCommonDivisor, - LeastCommonMultiple, - Floor, - Ceiling, - Round, - FracPart, - AbsoluteValue, - Factorial, - ImaginaryPart, - RealPart, - ComplexArgument, - Conjugate, - Logarithm, - NaperianLogarithm, - SquareRoot, - NthRoot, - Cosine, - Sine, - Tangent, ArcCosine, ArcSine, ArcTangent, - HyperbolicCosine, - HyperbolicSine, - HyperbolicTangent, + BinomialCoefficient, + Ceiling, + Complex, + ComplexArgument, + ComplexMatrix, + ConfidenceInterval, + Conjugate, + Cosine, + Derivative, + Determinant, + Division, + DivisionQuotient, + DivisionRemainder, + ExpressionMatrix, + Factorial, + Floor, + FracPart, + GreatCommonDivisor, HyperbolicArcCosine, HyperbolicArcSine, HyperbolicArcTangent, - Derivative, + HyperbolicCosine, + HyperbolicSine, + HyperbolicTangent, + ImaginaryPart, + Integer, Integral, - BinomialCoefficient, - PermuteCoefficient, - ConfidenceInterval, - PredictionInterval, - ExpressionMatrix, - ComplexMatrix, + LeastCommonMultiple, + Logarithm, MatrixDimension, MatrixInverse, MatrixTrace, MatrixTranspose, - Determinant, + Multiplication, + NaperianLogarithm, + NthRoot, + Opposite, + Parenthesis, + PermuteCoefficient, + Power, + PredictionInterval, + Product, + RealPart, + Round, + Sine, + SquareRoot, Store, + Subtraction, + Sum, + Symbol, + Tangent, Undefined = 255 }; enum class FloatDisplayMode { diff --git a/poincare/src/simplification/rulegen/node.cpp b/poincare/src/simplification/rulegen/node.cpp index 444387de2..194fb030b 100644 --- a/poincare/src/simplification/rulegen/node.cpp +++ b/poincare/src/simplification/rulegen/node.cpp @@ -26,6 +26,7 @@ void Node::identifyAnonymousChildren(int * index) { // Generation void Node::generateSelector(Rule * rule) { + sort(); int i = 0; for (Node * child : *m_children) { @@ -110,6 +111,93 @@ int Node::intValue() const { return result; } +int Node::compareTo(Node * n) const { + if (m_name->compare("Any")) { + if (n->m_name->compare("Any")) { + if (identifier().compare(n->identifier()) == 0) { + return 0; + } else if (identifier().compare(n->identifier()) > 0) { + return 1; + } else { + return -1; + } + } else if (n->m_name->compare("SameAs")) { + if (identifier().compare(n->value()) == 0) { + return -1; + } else if (identifier().compare(n->value()) > 0) { + return 1; + } else { + return -1; + } + } else { + return 1; + } + } else if (m_name->compare("SameAs")) { + if (n->m_name->compare("Any")) { + if (value().compare(n->identifier()) == 0) { + return 1; + } else if (value().compare(n->identifier()) > 0) { + return 1; + } else { + return -1; + } + } else if (n->m_name->compare("SameAs")) { + if (value().compare(n->value()) == 0) { + return 0; + } else if (value().compare(n->value()) > 0) { + return 1; + } else { + return -1; + } + } else { + return 1; + } + } else { + if (n->m_name->compare("Any")) { + return -1; + } else if (n->m_name->compare("SameAs")) { + return -1; + } else { + if (name().compare(n->name()) != 0) { + if (name().compare(n->name()) > 0) { + return 1; + } else { + return -1; + } + } else { + if (intValue() == n->intValue()) { + return 0; + } else if (intValue() > n->intValue()) { + return 1; + } else { + return -1; + } + } + } + } +} + +void Node::sort() { + if (m_children->size() == 0) { + return; + } + for (Node * child : *m_children) { + child->sort(); + } + for (int i = m_children->size()-1; i > 0; i--) { + bool isSorted = true; + for (int j = 0; j < m_children->size()-1; j++) { + if (m_children->at(j)->compareTo(m_children->at(j+1)) > 0) { + std::swap(m_children->at(j), m_children->at(j+1)); + isSorted = false; + } + } + if (isSorted) { + return; + } + } +} + #if 0 std::string Node::generateSelectorConstructor(Rule * context) { diff --git a/poincare/src/simplification/rulegen/node.h b/poincare/src/simplification/rulegen/node.h index f21e96697..aa2e1e0d7 100644 --- a/poincare/src/simplification/rulegen/node.h +++ b/poincare/src/simplification/rulegen/node.h @@ -24,6 +24,8 @@ public: std::string value() const; int intValue() const; int numberOfChildren() { return m_children->size(); } + int compareTo(Node * node) const; + void sort(); private: int selectorCaptureIndexInRule(Rule * rule); int selectorIndexInRule(Rule * rule);