1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #ifndef __INTERPKERNELEXPRPARSER_HXX__
22 #define __INTERPKERNELEXPRPARSER_HXX__
24 #include "INTERPKERNELDefines.hxx"
25 #include "InterpKernelUnit.hxx"
26 #include "InterpKernelException.hxx"
27 #include "InterpKernelFunction.hxx"
34 namespace INTERP_KERNEL
38 class INTERPKERNEL_EXPORT LeafExpr
42 virtual void fillValue(Value *val) const throw(INTERP_KERNEL::Exception) = 0;
43 virtual void compileX86(std::vector<std::string>& ass) const = 0;
44 virtual void compileX86_64(std::vector<std::string>& ass) const = 0;
45 virtual void replaceValues(const std::vector<double>& valuesInExpr) throw(INTERP_KERNEL::Exception) = 0;
46 static LeafExpr *buildInstanceFrom(const std::string& expr) throw(INTERP_KERNEL::Exception);
49 class INTERPKERNEL_EXPORT LeafExprVal : public LeafExpr
52 LeafExprVal(double value);
54 void compileX86(std::vector<std::string>& ass) const;
55 void compileX86_64(std::vector<std::string>& ass) const;
56 void fillValue(Value *val) const throw(INTERP_KERNEL::Exception);
57 void replaceValues(const std::vector<double>& valuesInExpr) throw(INTERP_KERNEL::Exception);
62 class INTERPKERNEL_EXPORT LeafExprVar : public LeafExpr
65 LeafExprVar(const std::string& var);
67 void compileX86(std::vector<std::string>& ass) const;
68 void compileX86_64(std::vector<std::string>& ass) const;
69 void fillValue(Value *val) const throw(INTERP_KERNEL::Exception);
70 std::string getVar() const { return _var_name; }
71 void prepareExprEvaluation(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo) const throw(INTERP_KERNEL::Exception);
72 void prepareExprEvaluationVec() const throw(INTERP_KERNEL::Exception);
73 void replaceValues(const std::vector<double>& valuesInExpr) throw(INTERP_KERNEL::Exception);
74 static bool isRecognizedKeyVar(const std::string& var, int& pos);
76 static const char END_OF_RECOGNIZED_VAR[];
78 mutable int _fast_pos;
79 std::string _var_name;
82 class INTERPKERNEL_EXPORT ExprParser
85 ExprParser(const char *expr, ExprParser *father=0);
86 ExprParser(const char *expr, int lgth, ExprParser *father=0);
88 void parse() throw(INTERP_KERNEL::Exception);
89 bool isParsingSuccessfull() const { return _is_parsing_ok; }
90 double evaluate() const throw(INTERP_KERNEL::Exception);
91 DecompositionInUnitBase evaluateUnit() const throw(INTERP_KERNEL::Exception);
92 void prepareExprEvaluation(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo) const throw(INTERP_KERNEL::Exception);
93 void evaluateExpr(int szOfOutParam, const double *inParam, double *outParam) const throw(INTERP_KERNEL::Exception);
94 void prepareExprEvaluationVec() const throw(INTERP_KERNEL::Exception);
95 void getSetOfVars(std::set<std::string>& vars) const;
96 void getTrueSetOfVars(std::set<std::string>& vars) const;
98 char *compileX86() const;
99 char *compileX86_64() const;
100 void compileX86LowLev(std::vector<std::string>& ass) const;
101 void compileX86_64LowLev(std::vector<std::string>& ass) const;
102 int getStackSizeToPlayX86(const ExprParser *asker) const;
104 static std::string buildStringFromFortran(const char *expr, int lgth);
105 static std::string deleteWhiteSpaces(const std::string& expr);
107 Value *evaluateLowLev(Value *valGen) const throw(INTERP_KERNEL::Exception);
109 void prepareExprEvaluationVecLowLev() const throw(INTERP_KERNEL::Exception);
110 bool tryToInterpALeaf() throw(INTERP_KERNEL::Exception);
111 void parseUnaryFunc() throw(INTERP_KERNEL::Exception);
112 void parseForCmp() throw(INTERP_KERNEL::Exception);
113 void parseForAddMin() throw(INTERP_KERNEL::Exception);
114 void parseForMulDiv() throw(INTERP_KERNEL::Exception);
115 void parseForPow() throw(INTERP_KERNEL::Exception);
116 void parseDeeper() throw(INTERP_KERNEL::Exception);
117 bool simplify() throw(INTERP_KERNEL::Exception);
118 void releaseFunctions();
119 void checkBracketsParity() const throw(INTERP_KERNEL::Exception);
120 void fillValuesInExpr(std::vector<double>& valuesInExpr) throw(INTERP_KERNEL::Exception);
121 void replaceValues(const std::vector<double>& valuesInExpr) throw(INTERP_KERNEL::Exception);
122 static double ReplaceAndTraduce(std::string& expr, int id, std::size_t bg, std::size_t end, int& delta) throw(INTERP_KERNEL::Exception);
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);
131 std::list<ExprParser> _sub_expr;
132 std::list<Function *> _func_btw_sub_expr;
134 static const int MAX_X86_FP_ST=8;
135 static const char WHITE_SPACES[];
136 static const char EXPR_PARSE_ERR_MSG[];