Guest User

updated expression

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