Back to index

salome-med  6.5.0
InterpKernelExprParser.hxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #ifndef __INTERPKERNELEXPRPARSER_HXX__
00021 #define __INTERPKERNELEXPRPARSER_HXX__
00022 
00023 #include "INTERPKERNELDefines.hxx"
00024 #include "InterpKernelUnit.hxx"
00025 #include "InterpKernelException.hxx"
00026 #include "InterpKernelFunction.hxx"
00027 
00028 #include <string>
00029 #include <list>
00030 #include <map>
00031 #include <set>
00032 
00033 namespace INTERP_KERNEL
00034 {
00035   class ValueDouble;
00036 
00037   class INTERPKERNEL_EXPORT LeafExpr
00038   {
00039   public:
00040     virtual ~LeafExpr();
00041     virtual void fillValue(Value *val) const throw(INTERP_KERNEL::Exception) = 0;
00042     virtual void compileX86(std::vector<std::string>& ass) const = 0;
00043     virtual void compileX86_64(std::vector<std::string>& ass) const = 0;
00044     virtual void replaceValues(const std::vector<double>& valuesInExpr) throw(INTERP_KERNEL::Exception) = 0;
00045     static LeafExpr *buildInstanceFrom(const std::string& expr) throw(INTERP_KERNEL::Exception);
00046   };
00047 
00048   class INTERPKERNEL_EXPORT LeafExprVal : public LeafExpr
00049   {
00050   public:
00051     LeafExprVal(double value);
00052     ~LeafExprVal();
00053     void compileX86(std::vector<std::string>& ass) const;
00054     void compileX86_64(std::vector<std::string>& ass) const;
00055     void fillValue(Value *val) const throw(INTERP_KERNEL::Exception);
00056     void replaceValues(const std::vector<double>& valuesInExpr) throw(INTERP_KERNEL::Exception);
00057   private:
00058     double _value;
00059   };
00060 
00061   class INTERPKERNEL_EXPORT LeafExprVar : public LeafExpr
00062   {
00063   public:
00064     LeafExprVar(const std::string& var);
00065     ~LeafExprVar();
00066     void compileX86(std::vector<std::string>& ass) const;
00067     void compileX86_64(std::vector<std::string>& ass) const;
00068     void fillValue(Value *val) const throw(INTERP_KERNEL::Exception);
00069     std::string getVar() const { return _var_name; }
00070     void prepareExprEvaluation(const std::vector<std::string>& vars) const throw(INTERP_KERNEL::Exception);
00071     void prepareExprEvaluationVec() const throw(INTERP_KERNEL::Exception);
00072     void replaceValues(const std::vector<double>& valuesInExpr) throw(INTERP_KERNEL::Exception);
00073     static bool isRecognizedKeyVar(const std::string& var, int& pos);
00074   public:
00075     static const char END_OF_RECOGNIZED_VAR[];
00076   private:
00077     mutable int _fast_pos;
00078     std::string _var_name;
00079   };
00080 
00081   class INTERPKERNEL_EXPORT ExprParser
00082   {
00083   public:
00084     ExprParser(const char *expr, ExprParser *father=0);
00085     ExprParser(const char *expr, int lgth, ExprParser *father=0);
00086     ~ExprParser();
00087     void parse() throw(INTERP_KERNEL::Exception);
00088     bool isParsingSuccessfull() const { return _is_parsing_ok; }
00089     double evaluate() const throw(INTERP_KERNEL::Exception);
00090     DecompositionInUnitBase evaluateUnit() const throw(INTERP_KERNEL::Exception);
00091     void prepareExprEvaluation(const std::vector<std::string>& vars) const throw(INTERP_KERNEL::Exception);
00092     void evaluateExpr(int szOfOutParam, const double *inParam, double *outParam) const throw(INTERP_KERNEL::Exception);
00093     void prepareExprEvaluationVec() const throw(INTERP_KERNEL::Exception);
00094     void getSetOfVars(std::set<std::string>& vars) const;
00095     void getTrueSetOfVars(std::set<std::string>& vars) const;
00096     //
00097     char *compileX86() const;
00098     char *compileX86_64() const;
00099     void compileX86LowLev(std::vector<std::string>& ass) const;
00100     void compileX86_64LowLev(std::vector<std::string>& ass) const;
00101     int getStackSizeToPlayX86(const ExprParser *asker) const;
00102     //
00103     static std::string buildStringFromFortran(const char *expr, int lgth);
00104     static std::string deleteWhiteSpaces(const std::string& expr);
00105   private:
00106     Value *evaluateLowLev(Value *valGen) const throw(INTERP_KERNEL::Exception);
00107   private:
00108     void prepareExprEvaluationVecLowLev() const throw(INTERP_KERNEL::Exception);
00109     bool tryToInterpALeaf() throw(INTERP_KERNEL::Exception);
00110     void parseUnaryFunc() throw(INTERP_KERNEL::Exception);
00111     void parseForCmp() throw(INTERP_KERNEL::Exception);
00112     void parseForAddMin() throw(INTERP_KERNEL::Exception);
00113     void parseForMulDiv() throw(INTERP_KERNEL::Exception);
00114     void parseForPow() throw(INTERP_KERNEL::Exception);
00115     void parseDeeper() throw(INTERP_KERNEL::Exception);
00116     bool simplify() throw(INTERP_KERNEL::Exception);
00117     void releaseFunctions();
00118     void checkBracketsParity() const throw(INTERP_KERNEL::Exception);
00119     void fillValuesInExpr(std::vector<double>& valuesInExpr) throw(INTERP_KERNEL::Exception);
00120     void replaceValues(const std::vector<double>& valuesInExpr) throw(INTERP_KERNEL::Exception);
00121     static double ReplaceAndTraduce(std::string& expr, int id, std::size_t bg, std::size_t end, int& delta) throw(INTERP_KERNEL::Exception);
00122     static std::size_t FindCorrespondingOpenBracket(const std::string& expr, std::size_t posOfCloseBracket);
00123     static void LocateError(std::ostream& stringToDisp, const std::string& srcOfErr, int posOfErr);
00124   private:
00125     ExprParser *_father;
00126     bool _is_parsed;
00127     LeafExpr *_leaf;
00128     bool _is_parsing_ok;
00129     std::string _expr;
00130     std::list<ExprParser> _sub_expr;
00131     std::list<Function *> _func_btw_sub_expr;
00132   private:
00133     static const int MAX_X86_FP_ST=8;
00134     static const char WHITE_SPACES[];
00135     static const char EXPR_PARSE_ERR_MSG[];
00136   };
00137 }
00138 
00139 #endif