Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / INTERP_KERNEL / ExprEval / InterpKernelExprParser.hxx
1 // Copyright (C) 2007-2012  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.
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
20 #ifndef __INTERPKERNELEXPRPARSER_HXX__
21 #define __INTERPKERNELEXPRPARSER_HXX__
22
23 #include "INTERPKERNELDefines.hxx"
24 #include "InterpKernelUnit.hxx"
25 #include "InterpKernelException.hxx"
26 #include "InterpKernelFunction.hxx"
27
28 #include <string>
29 #include <list>
30 #include <map>
31 #include <set>
32
33 namespace INTERP_KERNEL
34 {
35   class ValueDouble;
36
37   class INTERPKERNEL_EXPORT LeafExpr
38   {
39   public:
40     virtual ~LeafExpr();
41     virtual void fillValue(Value *val) const throw(INTERP_KERNEL::Exception) = 0;
42     virtual void compileX86(std::vector<std::string>& ass) const = 0;
43     virtual void compileX86_64(std::vector<std::string>& ass) const = 0;
44     virtual void replaceValues(const std::vector<double>& valuesInExpr) throw(INTERP_KERNEL::Exception) = 0;
45     static LeafExpr *buildInstanceFrom(const std::string& expr) throw(INTERP_KERNEL::Exception);
46   };
47
48   class INTERPKERNEL_EXPORT LeafExprVal : public LeafExpr
49   {
50   public:
51     LeafExprVal(double value);
52     ~LeafExprVal();
53     void compileX86(std::vector<std::string>& ass) const;
54     void compileX86_64(std::vector<std::string>& ass) const;
55     void fillValue(Value *val) const throw(INTERP_KERNEL::Exception);
56     void replaceValues(const std::vector<double>& valuesInExpr) throw(INTERP_KERNEL::Exception);
57   private:
58     double _value;
59   };
60
61   class INTERPKERNEL_EXPORT LeafExprVar : public LeafExpr
62   {
63   public:
64     LeafExprVar(const std::string& var);
65     ~LeafExprVar();
66     void compileX86(std::vector<std::string>& ass) const;
67     void compileX86_64(std::vector<std::string>& ass) const;
68     void fillValue(Value *val) const throw(INTERP_KERNEL::Exception);
69     std::string getVar() const { return _var_name; }
70     void prepareExprEvaluation(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo) const throw(INTERP_KERNEL::Exception);
71     void prepareExprEvaluationVec() const throw(INTERP_KERNEL::Exception);
72     void replaceValues(const std::vector<double>& valuesInExpr) throw(INTERP_KERNEL::Exception);
73     static bool isRecognizedKeyVar(const std::string& var, int& pos);
74   public:
75     static const char END_OF_RECOGNIZED_VAR[];
76   private:
77     mutable int _fast_pos;
78     std::string _var_name;
79   };
80
81   class INTERPKERNEL_EXPORT ExprParser
82   {
83   public:
84     ExprParser(const char *expr, ExprParser *father=0);
85     ExprParser(const char *expr, int lgth, ExprParser *father=0);
86     ~ExprParser();
87     void parse() throw(INTERP_KERNEL::Exception);
88     bool isParsingSuccessfull() const { return _is_parsing_ok; }
89     double evaluate() const throw(INTERP_KERNEL::Exception);
90     DecompositionInUnitBase evaluateUnit() const throw(INTERP_KERNEL::Exception);
91     void prepareExprEvaluation(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo) const throw(INTERP_KERNEL::Exception);
92     void evaluateExpr(int szOfOutParam, const double *inParam, double *outParam) const throw(INTERP_KERNEL::Exception);
93     void prepareExprEvaluationVec() const throw(INTERP_KERNEL::Exception);
94     void getSetOfVars(std::set<std::string>& vars) const;
95     void getTrueSetOfVars(std::set<std::string>& vars) const;
96     //
97     char *compileX86() const;
98     char *compileX86_64() const;
99     void compileX86LowLev(std::vector<std::string>& ass) const;
100     void compileX86_64LowLev(std::vector<std::string>& ass) const;
101     int getStackSizeToPlayX86(const ExprParser *asker) const;
102     //
103     static std::string buildStringFromFortran(const char *expr, int lgth);
104     static std::string deleteWhiteSpaces(const std::string& expr);
105   private:
106     Value *evaluateLowLev(Value *valGen) const throw(INTERP_KERNEL::Exception);
107   private:
108     void prepareExprEvaluationVecLowLev() const throw(INTERP_KERNEL::Exception);
109     bool tryToInterpALeaf() throw(INTERP_KERNEL::Exception);
110     void parseUnaryFunc() throw(INTERP_KERNEL::Exception);
111     void parseForCmp() throw(INTERP_KERNEL::Exception);
112     void parseForAddMin() throw(INTERP_KERNEL::Exception);
113     void parseForMulDiv() throw(INTERP_KERNEL::Exception);
114     void parseForPow() throw(INTERP_KERNEL::Exception);
115     void parseDeeper() throw(INTERP_KERNEL::Exception);
116     bool simplify() throw(INTERP_KERNEL::Exception);
117     void releaseFunctions();
118     void checkBracketsParity() const throw(INTERP_KERNEL::Exception);
119     void fillValuesInExpr(std::vector<double>& valuesInExpr) throw(INTERP_KERNEL::Exception);
120     void replaceValues(const std::vector<double>& valuesInExpr) throw(INTERP_KERNEL::Exception);
121     static double ReplaceAndTraduce(std::string& expr, int id, std::size_t bg, std::size_t end, int& delta) throw(INTERP_KERNEL::Exception);
122     static std::size_t FindCorrespondingOpenBracket(const std::string& expr, std::size_t posOfCloseBracket);
123     static void LocateError(std::ostream& stringToDisp, const std::string& srcOfErr, int posOfErr);
124   private:
125     ExprParser *_father;
126     bool _is_parsed;
127     LeafExpr *_leaf;
128     bool _is_parsing_ok;
129     std::string _expr;
130     std::list<ExprParser> _sub_expr;
131     std::list<Function *> _func_btw_sub_expr;
132   private:
133     static const int MAX_X86_FP_ST=8;
134     static const char WHITE_SPACES[];
135     static const char EXPR_PARSE_ERR_MSG[];
136   };
137 }
138
139 #endif