Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <exprtk.hpp>
- typedef double Type;
- typedef exprtk::symbol_table<Type> SymbolTableT;
- typedef exprtk::expression<Type> ExpressionT;
- typedef exprtk::parser<Type> ParserT;
- typedef exprtk::function_compositor<Type> CompositorT;
- typedef exprtk::parser_error::type ErrorT;
- typedef std::unordered_map<std::string, Type> VariableMapT;
- typedef std::vector<ScannerDetailsT < ExpressionT>> ExpressionContainerT;
- typedef double EXPRTKTYPE;
- template<class T>
- struct ScannerDetailsT
- {
- uint64_t Key{};
- std::shared_ptr<T> Expression;
- std::string Query;
- std::string UserID;
- };
- class MathParser
- {
- public:
- MathParser();
- virtual ~MathParser();
- template<typename Function>
- void AddFunction(std::string name, Function function)
- {
- this->SymbolTable->add_function(name, function);
- }
- void AddVariable(const std::string &name, Type value);
- bool ParseExpression(uint64_t key, const std::string &expression, const std::string &userID);
- Type GetOutput(const std::shared_ptr<ExpressionT> &Expression);
- protected:
- std::shared_ptr<SymbolTableT> SymbolTable;
- std::shared_ptr<CompositorT> Compositor;
- ParserT Parser;
- VariableMapT VariableMap;
- ExpressionContainerT ExpressionContainer;
- int count = {0};
- std::vector<EXPRTKTYPE> variable;
- void ParserError(const ParserT &parser, const std::string &expression);
- pthread_mutex_t OrderLock;
- };
- #include "../include/MathParser.hpp"
- #include <iostream>
- MathParser::MathParser() : OrderLock(PTHREAD_MUTEX_INITIALIZER)
- {
- this->Compositor = std::make_shared<CompositorT>();
- this->SymbolTable = std::make_shared<SymbolTableT>();
- this->SymbolTable->add_constants();
- if (pthread_mutex_init(&OrderLock, nullptr) != 0)
- {
- printf("Order Mutex lock did not initialized for service\n");
- }
- }
- MathParser::~MathParser()
- {
- this->Compositor = nullptr;
- this->SymbolTable = nullptr;
- }
- void MathParser::ParserError(const ParserT &parser, const std::string &expression)
- {
- std::cout << "Error : [" << parser.error() << "] Expression : [" << expression << "]" << std::endl;
- for (std::size_t i = 0; i < parser.error_count(); ++i)
- {
- ErrorT error = parser.get_error(i);
- std::cout << "\tError :[" << i
- << "] Position : [" << error.token.position
- << "] Type : [" << exprtk::parser_error::to_str(error.mode)
- << "] Message : [ " << error.diagnostic
- << "] Expression : [" << expression << "]" << std::endl;
- }
- }
- void MathParser::AddVariable(const std::string &name, Type value)
- {
- VariableMapT::iterator iterator = this->VariableMap.find(name);
- if (iterator == this->VariableMap.end())
- {
- std::pair<VariableMapT::iterator, bool> success = this->VariableMap.insert(VariableMapT::value_type(name, value));
- this->SymbolTable->add_variable(name, success.first->second);
- } else
- {
- this->SymbolTable->get_variable(name)->ref() = value;
- }
- }
- bool MathParser::ParseExpression(uint64_t key, const std::string &expression, const std::string &userID)
- {
- bool return_(false);
- if ((pthread_mutex_lock(&OrderLock) == 0))
- {
- std::shared_ptr<ExpressionT> Expression = std::make_shared<ExpressionT>();
- SymbolTableT symbolTable = *this->SymbolTable;
- Expression->register_symbol_table(symbolTable);
- this->variable.push_back(1);
- Expression->get_symbol_table().add_variable("token", this->variable[this->count]);
- //std::shared_ptr<ParserT> Parser = std::make_shared<ParserT>();
- /**
- Generally crashes on this line Parser.compile()
- */
- if (!Parser.compile(expression, *Expression))
- {
- this->ParserError(Parser, expression);
- Expression = nullptr;
- pthread_mutex_unlock(&OrderLock);
- } else
- {
- ++this->count;
- ScannerDetailsT<ExpressionT> scannerDetails;
- scannerDetails.Key = key;
- scannerDetails.Expression = Expression;
- scannerDetails.Query = expression;
- scannerDetails.UserID = userID;
- this->ExpressionContainer.push_back(scannerDetails);
- return_ = true;
- pthread_mutex_unlock(&OrderLock);
- }
- }
- return return_;
- }
- Type MathParser::GetOutput(const std::shared_ptr<ExpressionT> &Expression)
- {
- return Expression->value();
- }
Add Comment
Please, Sign In to add comment