Salome HOME
Update copyrights 2014.
[tools/medcoupling.git] / src / INTERP_KERNEL / ExprEval / InterpKernelExprParser.hxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #ifndef __INTERPKERNELEXPRPARSER_HXX__
22 #define __INTERPKERNELEXPRPARSER_HXX__
23
24 #include "INTERPKERNELDefines.hxx"
25 #include "InterpKernelUnit.hxx"
26 #include "InterpKernelException.hxx"
27 #include "InterpKernelFunction.hxx"
28
29 #include <string>
30 #include <list>
31 #include <map>
32 #include <set>
33
34 namespace INTERP_KERNEL
35 {
36   class ValueDouble;
37
38   class LeafExpr
39   {
40   public:
41     INTERPKERNEL_EXPORT virtual ~LeafExpr();
42     INTERPKERNEL_EXPORT virtual void fillValue(Value *val) const = 0;
43     INTERPKERNEL_EXPORT virtual void compileX86(std::vector<std::string>& ass) const = 0;
44     INTERPKERNEL_EXPORT virtual void compileX86_64(std::vector<std::string>& ass) const = 0;
45     INTERPKERNEL_EXPORT virtual void replaceValues(const std::vector<double>& valuesInExpr) = 0;
46     INTERPKERNEL_EXPORT static LeafExpr *buildInstanceFrom(const std::string& expr);
47   };
48
49   class LeafExprVal : public LeafExpr
50   {
51   public:
52     INTERPKERNEL_EXPORT LeafExprVal(double value);
53     INTERPKERNEL_EXPORT ~LeafExprVal();
54     INTERPKERNEL_EXPORT void compileX86(std::vector<std::string>& ass) const;
55     INTERPKERNEL_EXPORT void compileX86_64(std::vector<std::string>& ass) const;
56     INTERPKERNEL_EXPORT void fillValue(Value *val) const;
57     INTERPKERNEL_EXPORT void replaceValues(const std::vector<double>& valuesInExpr);
58   private:
59     double _value;
60   };
61
62   class LeafExprVar : public LeafExpr
63   {
64   public:
65     INTERPKERNEL_EXPORT LeafExprVar(const std::string& var);
66     INTERPKERNEL_EXPORT ~LeafExprVar();
67     INTERPKERNEL_EXPORT void compileX86(std::vector<std::string>& ass) const;
68     INTERPKERNEL_EXPORT void compileX86_64(std::vector<std::string>& ass) const;
69     INTERPKERNEL_EXPORT void fillValue(Value *val) const;
70     INTERPKERNEL_EXPORT std::string getVar() const { return _var_name; }
71     INTERPKERNEL_EXPORT void prepareExprEvaluation(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo) const;
72     INTERPKERNEL_EXPORT void prepareExprEvaluationVec() const;
73     INTERPKERNEL_EXPORT void replaceValues(const std::vector<double>& valuesInExpr);
74     INTERPKERNEL_EXPORT static bool isRecognizedKeyVar(const std::string& var, int& pos);
75   public:
76     static const char END_OF_RECOGNIZED_VAR[];
77   private:
78     mutable int _fast_pos;
79     std::string _var_name;
80   };
81
82   class ExprParser
83   {
84   public:
85     INTERPKERNEL_EXPORT ExprParser(const std::string& expr, ExprParser *father=0);
86     INTERPKERNEL_EXPORT ExprParser(const char *expr, int lgth, ExprParser *father=0);
87     INTERPKERNEL_EXPORT ~ExprParser();
88     INTERPKERNEL_EXPORT void parse();
89     INTERPKERNEL_EXPORT bool isParsingSuccessfull() const { return _is_parsing_ok; }
90     INTERPKERNEL_EXPORT double evaluate() const;
91     INTERPKERNEL_EXPORT DecompositionInUnitBase evaluateUnit() const;
92     INTERPKERNEL_EXPORT void prepareExprEvaluation(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo) const;
93     INTERPKERNEL_EXPORT void evaluateExpr(int szOfOutParam, const double *inParam, double *outParam) const;
94     INTERPKERNEL_EXPORT void prepareExprEvaluationVec() const;
95     INTERPKERNEL_EXPORT void getSetOfVars(std::set<std::string>& vars) const;
96     INTERPKERNEL_EXPORT void getTrueSetOfVars(std::set<std::string>& vars) const;
97     //
98     INTERPKERNEL_EXPORT char *compileX86() const;
99     INTERPKERNEL_EXPORT char *compileX86_64() const;
100     INTERPKERNEL_EXPORT void compileX86LowLev(std::vector<std::string>& ass) const;
101     INTERPKERNEL_EXPORT void compileX86_64LowLev(std::vector<std::string>& ass) const;
102     INTERPKERNEL_EXPORT int getStackSizeToPlayX86(const ExprParser *asker) const;
103     //
104     INTERPKERNEL_EXPORT static std::string buildStringFromFortran(const char *expr, int lgth);
105     INTERPKERNEL_EXPORT static std::string deleteWhiteSpaces(const std::string& expr);
106   private:
107     Value *evaluateLowLev(Value *valGen) const;
108   private:
109     void prepareExprEvaluationVecLowLev() const;
110     bool tryToInterpALeaf();
111     void parseUnaryFunc();
112     void parseForCmp();
113     void parseForAddMin();
114     void parseForMulDiv();
115     void parseForPow();
116     void parseDeeper();
117     bool simplify();
118     void releaseFunctions();
119     void checkBracketsParity() const;
120     void fillValuesInExpr(std::vector<double>& valuesInExpr);
121     void replaceValues(const std::vector<double>& valuesInExpr);
122     static double ReplaceAndTraduce(std::string& expr, int id, std::size_t bg, std::size_t end, int& delta);
123     static std::size_t FindCorrespondingOpenBracket(const std::string& expr, std::size_t posOfCloseBracket);
124     static void LocateError(std::ostream& stringToDisp, const std::string& srcOfErr, int posOfErr);
125   private:
126     ExprParser *_father;
127     bool _is_parsed;
128     LeafExpr *_leaf;
129     bool _is_parsing_ok;
130     std::string _expr;
131     std::list<ExprParser> _sub_expr;
132     std::list<Function *> _func_btw_sub_expr;
133   private:
134     static const int MAX_X86_FP_ST=8;
135     static const char WHITE_SPACES[];
136     static const char EXPR_PARSE_ERR_MSG[];
137   };
138 }
139
140 #endif