[2510] | 1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
| 2 | // Evaluateur d'expression C - R. Ansari 03/2004
|
---|
| 3 | // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
|
---|
| 4 |
|
---|
| 5 | #ifndef CEXPREVAL_SEEN
|
---|
| 6 | #define CEXPREVAL_SEEN
|
---|
| 7 |
|
---|
| 8 | #include "machdefs.h"
|
---|
| 9 | #include <iostream>
|
---|
| 10 | #include <string>
|
---|
| 11 | #include "pexceptions.h"
|
---|
| 12 |
|
---|
| 13 | using namespace std;
|
---|
| 14 | namespace SOPHYA {
|
---|
| 15 |
|
---|
| 16 | //! Exception class used by CExpressionEvaluator
|
---|
[2512] | 17 | class CExprException : public PException {
|
---|
[2510] | 18 | public:
|
---|
| 19 | explicit CExprException(const char * m) : PException(m) {}
|
---|
| 20 | explicit CExprException(const string& m) : PException(m) {}
|
---|
| 21 | };
|
---|
| 22 |
|
---|
| 23 | //--------------------------------------------------------------------
|
---|
| 24 | //! Base class for arithmetic expressions used by CExpressionEvaluator
|
---|
| 25 | class CExprBase {
|
---|
| 26 | public:
|
---|
| 27 | explicit CExprBase();
|
---|
| 28 | virtual ~CExprBase();
|
---|
| 29 | virtual double Evaluate() const = 0;
|
---|
| 30 | virtual bool CheckE(string& errmsg) const { return true; }
|
---|
| 31 | virtual void CheckThrow(const char * msg) const;
|
---|
| 32 | inline void CheckThrow(string const & msg) const { CheckThrow(msg.c_str()); }
|
---|
| 33 | virtual void Print(ostream& os) const = 0;
|
---|
| 34 | static long NbCreate() { return totnexp_create; }
|
---|
| 35 | static long NbDelete() { return totnexp_delete; }
|
---|
| 36 | protected:
|
---|
| 37 | static long totnexp_create;
|
---|
| 38 | static long totnexp_delete;
|
---|
| 39 | };
|
---|
| 40 |
|
---|
| 41 | inline ostream& operator << (ostream& s, CExprBase const & ex)
|
---|
| 42 | { ex.Print(s); return(s); }
|
---|
| 43 |
|
---|
| 44 | class CE_BinExp;
|
---|
| 45 | class CE_FuncExp;
|
---|
| 46 | //---------------------------------------------------------
|
---|
| 47 | //! Class for evaluation of arithmetic expressions with C-like syntax
|
---|
| 48 | class CExpressionEvaluator : public CExprBase {
|
---|
| 49 | public:
|
---|
| 50 | CExpressionEvaluator(string const & sex);
|
---|
| 51 | virtual ~CExpressionEvaluator();
|
---|
| 52 | virtual double Evaluate() const;
|
---|
| 53 | inline double Value() const { return Evaluate(); }
|
---|
| 54 | virtual void Print(ostream& os) const;
|
---|
| 55 |
|
---|
| 56 | protected:
|
---|
| 57 | CExprBase* ParseString(int extype, string fname, string const & sex,
|
---|
| 58 | size_t off, size_t& stop, string& errmsg);
|
---|
| 59 |
|
---|
| 60 | CExprBase * _exp;
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 | } // End of namespace SOPHYA
|
---|
| 64 |
|
---|
| 65 | /* end of ifdef CEXPREVAL_SEEN */
|
---|
| 66 | #endif
|
---|