Guest User

exprtkClass

a guest
May 14th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1. #include <exprtk.hpp>
  2. typedef double Type;
  3. typedef exprtk::symbol_table<Type> SymbolTableT;
  4. typedef exprtk::expression<Type> ExpressionT;
  5. typedef exprtk::parser<Type> ParserT;
  6. typedef exprtk::function_compositor<Type> CompositorT;
  7. typedef exprtk::parser_error::type ErrorT;
  8. typedef std::unordered_map<std::string, Type> VariableMapT;
  9. typedef std::vector<ScannerDetailsT < ExpressionT>> ExpressionContainerT;
  10. typedef double EXPRTKTYPE;
  11.  
  12. template<class T>
  13. struct ScannerDetailsT
  14. {
  15.     uint64_t Key{};
  16.     std::shared_ptr<T> Expression;
  17.     std::string Query;
  18.     std::string UserID;
  19. };
  20.  
  21. class MathParser
  22. {
  23. public:
  24.     MathParser();
  25.     virtual ~MathParser();
  26.  
  27.     template<typename Function>
  28.     void AddFunction(std::string name, Function function)
  29.     {
  30.         this->SymbolTable->add_function(name, function);
  31.     }
  32.  
  33.     void AddVariable(const std::string &name, Type value);
  34.     bool ParseExpression(uint64_t key, const std::string &expression, const std::string &userID);
  35.     Type GetOutput(const std::shared_ptr<ExpressionT> &Expression);
  36.  
  37. protected:
  38.     std::shared_ptr<SymbolTableT> SymbolTable;
  39.     std::shared_ptr<CompositorT> Compositor;
  40.     ParserT Parser;
  41.     VariableMapT VariableMap;
  42.     ExpressionContainerT ExpressionContainer;
  43.     int count = {0};
  44.     std::vector<EXPRTKTYPE> variable;
  45.     void ParserError(const ParserT &parser, const std::string &expression);
  46.     pthread_mutex_t OrderLock;
  47. };
  48.  
  49.  
  50. #include "../include/MathParser.hpp"
  51. #include <iostream>
  52.  
  53. MathParser::MathParser() : OrderLock(PTHREAD_MUTEX_INITIALIZER)
  54. {
  55.     this->Compositor = std::make_shared<CompositorT>();
  56.     this->SymbolTable = std::make_shared<SymbolTableT>();
  57.     this->SymbolTable->add_constants();
  58.  
  59.     if (pthread_mutex_init(&OrderLock, nullptr) != 0)
  60.     {
  61.         printf("Order Mutex lock did not initialized for service\n");
  62.     }
  63. }
  64.  
  65. MathParser::~MathParser()
  66. {
  67.     this->Compositor = nullptr;
  68.     this->SymbolTable = nullptr;
  69. }
  70.  
  71. void MathParser::ParserError(const ParserT &parser, const std::string &expression)
  72. {
  73.     std::cout << "Error : [" << parser.error() << "] Expression : [" << expression << "]" << std::endl;
  74.  
  75.     for (std::size_t i = 0; i < parser.error_count(); ++i)
  76.     {
  77.         ErrorT error = parser.get_error(i);
  78.         std::cout << "\tError :[" << i
  79.                   << "] Position : [" << error.token.position
  80.                   << "] Type : [" << exprtk::parser_error::to_str(error.mode)
  81.                   << "] Message : [ " << error.diagnostic
  82.                   << "] Expression : [" << expression << "]" << std::endl;
  83.     }
  84. }
  85.  
  86. void MathParser::AddVariable(const std::string &name, Type value)
  87. {
  88.     VariableMapT::iterator iterator = this->VariableMap.find(name);
  89.     if (iterator == this->VariableMap.end())
  90.     {
  91.         std::pair<VariableMapT::iterator, bool> success = this->VariableMap.insert(VariableMapT::value_type(name, value));
  92.         this->SymbolTable->add_variable(name, success.first->second);
  93.     } else
  94.     {
  95.         this->SymbolTable->get_variable(name)->ref() = value;
  96.     }
  97. }
  98.  
  99. bool MathParser::ParseExpression(uint64_t key, const std::string &expression, const std::string &userID)
  100. {
  101.     bool return_(false);
  102.     if ((pthread_mutex_lock(&OrderLock) == 0))
  103.     {
  104.         std::shared_ptr<ExpressionT> Expression = std::make_shared<ExpressionT>();
  105.         SymbolTableT symbolTable = *this->SymbolTable;
  106.         Expression->register_symbol_table(symbolTable);
  107.         this->variable.push_back(1);
  108.         Expression->get_symbol_table().add_variable("token", this->variable[this->count]);
  109.  
  110.         //std::shared_ptr<ParserT> Parser = std::make_shared<ParserT>();
  111.         /**
  112.             Generally crashes on this line Parser.compile()
  113.         */
  114.         if (!Parser.compile(expression, *Expression))
  115.         {
  116.             this->ParserError(Parser, expression);
  117.             Expression = nullptr;
  118.             pthread_mutex_unlock(&OrderLock);
  119.         } else
  120.         {
  121.             ++this->count;
  122.  
  123.             ScannerDetailsT<ExpressionT> scannerDetails;
  124.             scannerDetails.Key = key;
  125.             scannerDetails.Expression = Expression;
  126.             scannerDetails.Query = expression;
  127.             scannerDetails.UserID = userID;
  128.             this->ExpressionContainer.push_back(scannerDetails);
  129.             return_ = true;
  130.             pthread_mutex_unlock(&OrderLock);
  131.         }
  132.     }
  133.     return return_;
  134. }
  135.  
  136. Type MathParser::GetOutput(const std::shared_ptr<ExpressionT> &Expression)
  137. {
  138.     return Expression->value();
  139. }
Add Comment
Please, Sign In to add comment