#ifndef ATOOLS_Math_Algebra_Interpreter_H #define ATOOLS_Math_Algebra_Interpreter_H #ifdef USING__Calc_only #include "Tools.H" #include "Node.H" #include "Term.H" #else #include "ATOOLS/Org/STL_Tools.H" #include "ATOOLS/Org/Node.H" #include "ATOOLS/Math/Term.H" #endif #include #include #include namespace ATOOLS { class Algebra_Interpreter; class Function { protected: std::string m_tag; public: // constructor Function(const std::string &tag); // destructor virtual ~Function(); // member functions virtual Term *Evaluate(Algebra_Interpreter *const interpreter, const std::vector &args) const; // inline functions inline const std::string &Tag() const { return m_tag; } };// end of class Function inline bool operator<(const Function &f1,const Function &f2) { return f1.Tag() String_Map; typedef std::pair Function_Pair; typedef std::map Function_Map; typedef std::pair Operator_Pair; typedef std::multimap Operator_Map; typedef std::map Interpreter_Map; typedef std::vector Term_Vector; private: Function_Map m_functions, m_leafs; static Function_Map s_functions; static Operator_Map s_operators; struct Global_Functions { Global_Functions(); ~Global_Functions(); inline void AddFunction(Function *const f) { s_functions.insert(Function_Pair(f->Tag(),f)); } inline void AddOperator(Operator *const b) { s_operators.insert(Operator_Pair(b->Priority(),b)); } };// end of struct Global_Functions static Global_Functions s_global; Interpreter_Map m_interpreters; Tag_Replacer *p_replacer; String_Map m_tags; Term_Vector m_terms; Node *p_root; std::vector m_argvs; void PrintNode(Node *const node) const; void AddArgs(Node *const node); Term *Iterate(Node *const node,size_t &n); public: // constructors Algebra_Interpreter(const bool standard=true); // destructor virtual ~Algebra_Interpreter(); // member functions std::string Interprete(const std::string &expr); std::string Iterate(const std::string &expr); Term *Calculate(); void AddFunction(Function *const f); void AddLeaf(Function *const f); void AddTerm(Term *const t); std::string ReplaceTags(std::string &expr) const; Term *ReplaceTags(Term *expr) const; void AddTag(const std::string &tag,const std::string &value); void SetTags(const String_Map &tags); Node *ExtractLeaf(const std::string &expr) const; // inline functions inline void SetTagReplacer(Tag_Replacer *const replacer) { p_replacer=replacer; } inline const Function_Map &Functions() const { return s_functions; } inline const Operator_Map &Operators() const { return s_operators; } inline const Function_Map &Locals() const { return m_functions; } inline Tag_Replacer * TagReplacer() const { return p_replacer; } inline void PrintEquation() const { PrintNode(p_root); } };// end of class Algebra_Interpreter }// end of namespace ATOOLS #define DEFINE_FUNCTION(NAME,TAG) \ namespace ATOOLS { class NAME: public Function { \ public: \ NAME(); \ Term *Evaluate(Algebra_Interpreter *const interpreter, \ const std::vector &args) const; \ }; } \ NAME::NAME(): Function(TAG) {} #define DEFINE_UNARY_OPERATOR(NAME,TAG,PRIORITY) \ namespace ATOOLS { class NAME: public Operator { \ public: \ NAME(); \ Term *Evaluate(Algebra_Interpreter *const interpreter, \ const std::vector &args) const; \ }; } \ NAME::NAME(): Operator(TAG,PRIORITY,false) {} #define DEFINE_BINARY_OPERATOR(NAME,TAG,PRIORITY) \ namespace ATOOLS { class NAME: public Operator { \ public: \ NAME(); \ Term *Evaluate(Algebra_Interpreter *const interpreter, \ const std::vector &args) const; \ }; } \ NAME::NAME(): Operator(TAG,PRIORITY,true) {} #define DEFINE_UNARY_BINARY_OPERATOR(NAME,TAG,PRIORITY,TYPE) \ namespace ATOOLS { class NAME: public Operator { \ public: \ NAME(); \ Term *Evaluate(Algebra_Interpreter *const interpreter, \ const std::vector &args) const; \ size_t FindTag(Algebra_Interpreter *const interpreter, \ const std::string &expr, \ const bool fwd,size_t cpos) const; \ }; } \ NAME::NAME(): Operator(TAG,PRIORITY,TYPE) {} #define DEFINE_INTERPRETER_FUNCTION(NAME) \ namespace ATOOLS { class NAME: public Interpreter_Function { \ public: \ inline NAME(Algebra_Interpreter *interpreter): \ Interpreter_Function(interpreter) {} \ std::string Interprete(const std::string &expr); \ }; } \ std::string NAME::Interprete(const std::string &expr) #endif