1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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
41 INTERPKERNEL_EXPORT virtual ~LeafExpr();
42 INTERPKERNEL_EXPORT virtual double getDoubleValue() const = 0;
43 INTERPKERNEL_EXPORT virtual void fillValue(Value *val) const = 0;
44 INTERPKERNEL_EXPORT virtual void compileX86(std::vector<std::string>& ass) const = 0;
45 INTERPKERNEL_EXPORT virtual void compileX86_64(std::vector<std::string>& ass) const = 0;
46 INTERPKERNEL_EXPORT virtual void replaceValues(const std::vector<double>& valuesInExpr) = 0;
47 INTERPKERNEL_EXPORT virtual LeafExpr *deepCopy() const = 0;
48 INTERPKERNEL_EXPORT static LeafExpr *buildInstanceFrom(const std::string& expr);
51 class LeafExprVal : public LeafExpr
54 INTERPKERNEL_EXPORT LeafExprVal(double value);
55 INTERPKERNEL_EXPORT ~LeafExprVal();
56 INTERPKERNEL_EXPORT double getDoubleValue() const;
57 INTERPKERNEL_EXPORT void compileX86(std::vector<std::string>& ass) const;
58 INTERPKERNEL_EXPORT void compileX86_64(std::vector<std::string>& ass) const;
59 INTERPKERNEL_EXPORT void fillValue(Value *val) const;
60 INTERPKERNEL_EXPORT void replaceValues(const std::vector<double>& valuesInExpr);
61 INTERPKERNEL_EXPORT LeafExprVal *deepCopy() const;
66 class LeafExprVar : public LeafExpr
69 INTERPKERNEL_EXPORT LeafExprVar(const LeafExprVar& other):_fast_pos(other._fast_pos),_ref_pos(other._ref_pos),_var_name(other._var_name),_val(other._val) { }
70 INTERPKERNEL_EXPORT LeafExprVar(const std::string& var);
71 INTERPKERNEL_EXPORT ~LeafExprVar();
72 INTERPKERNEL_EXPORT double getDoubleValue() const;
73 INTERPKERNEL_EXPORT void compileX86(std::vector<std::string>& ass) const;
74 INTERPKERNEL_EXPORT void compileX86_64(std::vector<std::string>& ass) const;
75 INTERPKERNEL_EXPORT void fillValue(Value *val) const;
76 INTERPKERNEL_EXPORT std::string getVar() const { return _var_name; }
77 INTERPKERNEL_EXPORT void prepareExprEvaluation(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo) const;
78 INTERPKERNEL_EXPORT void prepareExprEvaluationDouble(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo, int refPos, const double *ptOfInputStart, const double *ptOfInputEnd) const;
79 INTERPKERNEL_EXPORT void prepareExprEvaluationVec() const;
80 INTERPKERNEL_EXPORT void replaceValues(const std::vector<double>& valuesInExpr);
81 INTERPKERNEL_EXPORT static bool isRecognizedKeyVar(const std::string& var, int& pos);
82 INTERPKERNEL_EXPORT LeafExprVar *deepCopy() const;
84 static const char END_OF_RECOGNIZED_VAR[];
86 mutable int _fast_pos;
88 std::string _var_name;
89 mutable const double *_val;
92 class ExprParserOfEval
95 ExprParserOfEval():_leaf(0) { }
96 ExprParserOfEval(LeafExpr *leaf, const std::vector<ExprParserOfEval>& subParts, const std::vector<Function *>& funcs):_leaf(leaf),_sub_parts(subParts),_funcs(funcs) { }
97 void evaluateDoubleInternal(std::vector<double>& stck) const
100 stck.push_back(_leaf->getDoubleValue());
102 for(std::vector<ExprParserOfEval>::const_iterator iter=_sub_parts.begin();iter!=_sub_parts.end();iter++)
103 (*iter).evaluateDoubleInternal(stck);
104 for(std::vector<Function *>::const_iterator iter3=_funcs.begin();iter3!=_funcs.end();iter3++)
105 (*iter3)->operateStackOfDouble(stck);
107 void evaluateDoubleInternalSafe(std::vector<double>& stck) const
110 stck.push_back(_leaf->getDoubleValue());
112 for(std::vector<ExprParserOfEval>::const_iterator iter=_sub_parts.begin();iter!=_sub_parts.end();iter++)
113 (*iter).evaluateDoubleInternalSafe(stck);
114 for(std::vector<Function *>::const_iterator iter3=_funcs.begin();iter3!=_funcs.end();iter3++)
115 (*iter3)->operateStackOfDoubleSafe(stck);
117 void clearSortedMemory();
121 std::vector<ExprParserOfEval> _sub_parts;
122 std::vector<Function *> _funcs;
128 INTERPKERNEL_EXPORT ExprParser(const std::string& expr, ExprParser *father=0);
129 INTERPKERNEL_EXPORT ExprParser(const char *expr, int lgth, ExprParser *father=0);
130 INTERPKERNEL_EXPORT ~ExprParser();
131 INTERPKERNEL_EXPORT void parse();
132 INTERPKERNEL_EXPORT bool isParsingSuccessfull() const { return _is_parsing_ok; }
133 INTERPKERNEL_EXPORT double evaluate() const;
134 INTERPKERNEL_EXPORT DecompositionInUnitBase evaluateUnit() const;
135 INTERPKERNEL_EXPORT void prepareExprEvaluation(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo) const;
136 INTERPKERNEL_EXPORT void prepareExprEvaluationDouble(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo, int refPos, const double *ptOfInputStart, const double *ptOfInputEnd) const;
137 INTERPKERNEL_EXPORT void prepareFastEvaluator() const;
138 INTERPKERNEL_EXPORT void prepareExprEvaluationVec() const;
139 INTERPKERNEL_EXPORT double evaluateDouble() const;
140 INTERPKERNEL_EXPORT void evaluateDoubleInternal(std::vector<double>& stck) const { _for_eval.evaluateDoubleInternal(stck); }
141 INTERPKERNEL_EXPORT void evaluateDoubleInternalSafe(std::vector<double>& stck) const { _for_eval.evaluateDoubleInternalSafe(stck); }
142 INTERPKERNEL_EXPORT void checkForEvaluation() const;
143 INTERPKERNEL_EXPORT void evaluateExpr(int szOfOutParam, const double *inParam, double *outParam) const;
144 INTERPKERNEL_EXPORT void getSetOfVars(std::set<std::string>& vars) const;
145 INTERPKERNEL_EXPORT void getTrueSetOfVars(std::set<std::string>& vars) const;
147 INTERPKERNEL_EXPORT char *compileX86() const;
148 INTERPKERNEL_EXPORT char *compileX86_64() const;
149 INTERPKERNEL_EXPORT void compileX86LowLev(std::vector<std::string>& ass) const;
150 INTERPKERNEL_EXPORT void compileX86_64LowLev(std::vector<std::string>& ass) const;
151 INTERPKERNEL_EXPORT int getStackSizeToPlayX86(const ExprParser *asker) const;
153 INTERPKERNEL_EXPORT static std::string buildStringFromFortran(const char *expr, int lgth);
154 INTERPKERNEL_EXPORT static std::string deleteWhiteSpaces(const std::string& expr);
156 Value *evaluateLowLev(Value *valGen) const;
158 ExprParserOfEval convertMeTo() const;
160 void prepareExprEvaluationVecLowLev() const;
161 bool tryToInterpALeaf();
162 void parseUnaryFunc();
164 void parseForAddMin();
165 void parseForMulDiv();
169 void releaseFunctions();
170 void checkBracketsParity() const;
171 void fillValuesInExpr(std::vector<double>& valuesInExpr);
172 void replaceValues(const std::vector<double>& valuesInExpr);
173 static double ReplaceAndTraduce(std::string& expr, int id, std::size_t bg, std::size_t end, int& delta);
174 static std::size_t FindCorrespondingOpenBracket(const std::string& expr, std::size_t posOfCloseBracket);
175 static void LocateError(std::ostream& stringToDisp, const std::string& srcOfErr, int posOfErr);
182 mutable ExprParserOfEval _for_eval;
183 std::vector<ExprParser> _sub_expr;
184 std::vector<Function *> _func_btw_sub_expr;
186 static const int MAX_X86_FP_ST=8;
187 static const char WHITE_SPACES[];
188 static const char EXPR_PARSE_ERR_MSG[];