From 97bcac3fce0416e8161757d51279bc98a135e55d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Wed, 27 Sep 2017 16:44:04 +0200 Subject: [PATCH] [poincare] Improve rulegen: create other selectors Change-Id: Ib80ae188378c589213356cc6e82f53592f9b91e8 --- poincare/src/simplification/rulegen/Makefile | 2 +- poincare/src/simplification/rulegen/node.cpp | 45 +++++++++++++++++++- poincare/src/simplification/rulegen/node.h | 2 + 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/poincare/src/simplification/rulegen/Makefile b/poincare/src/simplification/rulegen/Makefile index 8c507e822..b7f01de3f 100644 --- a/poincare/src/simplification/rulegen/Makefile +++ b/poincare/src/simplification/rulegen/Makefile @@ -25,7 +25,7 @@ products += $(rulegen_objs) $(RULEGEN) $(addprefix $(prefix)/,\ rules_lexer.cpp\ ) -GENERATOR_CXXFLAGS = -std=c++11 -Wno-deprecated-register +GENERATOR_CXXFLAGS = -std=c++11 -Wno-deprecated-register -Iion/include $(rulegen_objs): %.o: %.cpp @echo "HOSTCC $@" diff --git a/poincare/src/simplification/rulegen/node.cpp b/poincare/src/simplification/rulegen/node.cpp index 52ad943f9..e9f395087 100644 --- a/poincare/src/simplification/rulegen/node.cpp +++ b/poincare/src/simplification/rulegen/node.cpp @@ -3,6 +3,7 @@ #include #include #include +#include void Node::setChildren(std::vector * children) { delete m_children; @@ -43,7 +44,20 @@ void Node::generateSelector(Rule * rule) { } std::cout << "};" << std::endl; } - std::cout << "constexpr TypeSelector " << identifier() << "(Expression::Type::" << *m_name << ", " << rule->indexOfIdentifierInTransform(*m_identifier); + std::cout << "constexpr "; + if (name().compare("Any") == 0) { + std::cout << "AnySelector " << identifier() << "("; + } else if (name().compare("SameAs") == 0) { + assert(value().compare("NOVALUE") != 0); + std::cout << "SameAsSelector " << identifier() << "(" << value() << ", "; + } else { + if (m_value) { + std::cout << "TypeAndIdentifierSelector " << identifier() << "(Expression::Type::" << name() << ", " << intValue() << ", "; + } else { + std::cout << "TypeSelector " << identifier() << "(Expression::Type::" << name() << ", "; + } + } + std::cout << rule->indexOfIdentifierInTransform(*m_identifier); if (m_children->size() > 0) { std::cout << ", " << identifier() <<"Children, " << m_children->size(); } @@ -59,6 +73,12 @@ int Node::indexOfChildrenWithIdentifier(std::string identifier) { return -1; } +std::string Node::value() { + if (m_value) { + return *m_value; + } + return "NOVALUE"; +} std::string Node::identifier() { if (m_identifier) { @@ -67,7 +87,28 @@ std::string Node::identifier() { return "NOIDEA"; } - +int Node::intValue() { + assert(m_value); + // TODO: handle m_value = "Pi", "e", "i" ... + if (m_value->compare("Pi") == 0) { + return Ion::Charset::SmallPi; + } + if (m_value->compare("i") == 0) { + return Ion::Charset::IComplex; + } + // read number + int result = 0; + int base = 1; + for (int i=m_value->size()-1; i>=0; i--) { + if (m_value->at(i) == '-') { + return -result; + } + int digit = m_value->at(i)-'0'; + result = result+base*digit; + base *= 10; + } + return result; +} #if 0 diff --git a/poincare/src/simplification/rulegen/node.h b/poincare/src/simplification/rulegen/node.h index 3cda0570f..79e7e7019 100644 --- a/poincare/src/simplification/rulegen/node.h +++ b/poincare/src/simplification/rulegen/node.h @@ -21,6 +21,8 @@ public: int indexOfChildrenWithIdentifier(std::string identifier); std::string identifier(); std::string name() { return *m_name; } + std::string value(); + int intValue(); int numberOfChildren() { return m_children->size(); } private: int selectorCaptureIndexInRule(Rule * rule);