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 | /*!
|
---|
17 | \ingroup SysTools
|
---|
18 | \brief Exception class used by CExpressionEvaluator
|
---|
19 | */
|
---|
20 | class CExprException : public PException {
|
---|
21 | public:
|
---|
22 | explicit CExprException(const char * m) throw() : PException(m) {}
|
---|
23 | explicit CExprException(const string& m) throw() : PException(m) {}
|
---|
24 | };
|
---|
25 |
|
---|
26 | //--------------------------------------------------------------------
|
---|
27 | /*!
|
---|
28 | \ingroup SysTools
|
---|
29 | \brief Base class for arithmetic expressions used by CExpressionEvaluator
|
---|
30 | \sa CExpressionEvaluator
|
---|
31 | */
|
---|
32 | class CExprBase {
|
---|
33 | public:
|
---|
34 | explicit CExprBase();
|
---|
35 | virtual ~CExprBase();
|
---|
36 | virtual double Evaluate() const = 0;
|
---|
37 | virtual bool CheckE(string& errmsg) const { return true; }
|
---|
38 | virtual void CheckThrow(const char * msg) const;
|
---|
39 | inline void CheckThrow(string const & msg) const { CheckThrow(msg.c_str()); }
|
---|
40 | virtual void Print(ostream& os) const = 0;
|
---|
41 | static long NbCreate() { return totnexp_create; }
|
---|
42 | static long NbDelete() { return totnexp_delete; }
|
---|
43 | protected:
|
---|
44 | static long totnexp_create;
|
---|
45 | static long totnexp_delete;
|
---|
46 | };
|
---|
47 |
|
---|
48 | /*!
|
---|
49 | \ingroup SysTools
|
---|
50 | \brief For formatted write (print) of expressions in a stream
|
---|
51 | */
|
---|
52 | inline ostream& operator << (ostream& s, CExprBase const & ex)
|
---|
53 | { ex.Print(s); return(s); }
|
---|
54 |
|
---|
55 | class CE_BinExp;
|
---|
56 | class CE_FuncExp;
|
---|
57 | //---------------------------------------------------------
|
---|
58 | class CExpressionEvaluator : public CExprBase {
|
---|
59 | public:
|
---|
60 | CExpressionEvaluator(string const & sex);
|
---|
61 | virtual ~CExpressionEvaluator();
|
---|
62 | //! Evaluate the expression and returns the corresponding value.
|
---|
63 | virtual double Evaluate() const;
|
---|
64 | //! Alias for Evaluate()
|
---|
65 | inline double Value() const { return Evaluate(); }
|
---|
66 | //! Formatted output on a stream
|
---|
67 | virtual void Print(ostream& os) const;
|
---|
68 |
|
---|
69 | protected:
|
---|
70 | //! Does the parsing and builds an CExprBase object.
|
---|
71 | CExprBase* ParseString(int extype, string fname, string const & sex,
|
---|
72 | size_t off, size_t& stop, string& errmsg);
|
---|
73 |
|
---|
74 | CExprBase * _exp;
|
---|
75 | };
|
---|
76 |
|
---|
77 | } // End of namespace SOPHYA
|
---|
78 |
|
---|
79 | /* end of ifdef CEXPREVAL_SEEN */
|
---|
80 | #endif
|
---|