fastdo  0.6.8
eienexpr.hpp
浏览该文件的文档.
1 #ifndef __EIENEXPR_HPP__
2 #define __EIENEXPR_HPP__
3 
4 #include "winux.hpp"
5 
7 namespace eienexpr
8 {
9 #ifdef EIENEXPR_DLL_USE
10  #if defined(_MSC_VER) || defined(WIN32)
11  #pragma warning( disable: 4251 )
12  #ifdef EIENEXPR_DLL_EXPORTS
13  #define EIENEXPR_DLL __declspec(dllexport)
14  #else
15  #define EIENEXPR_DLL __declspec(dllimport)
16  #endif
17 
18  #define EIENEXPR_API __stdcall
19  #else
20  #define EIENEXPR_DLL
21  #define EIENEXPR_API
22  #endif
23 #else
24  #define EIENEXPR_DLL
25  #define EIENEXPR_API
26 #endif
27 
28 #define EIENEXPR_FUNC_DECL(ret) EIENEXPR_DLL ret EIENEXPR_API
29 #define EIENEXPR_FUNC_IMPL(ret) ret EIENEXPR_API
30 
33 {
34 public:
35  enum
36  {
48  eeOutOfArrayBound
49  };
50 
51  ExprError( int errNo, winux::AnsiString const & err ) throw() : winux::Error( errNo, err ) { }
52 };
53 
56 {
57 public:
59  {
61  eatOperand
62  };
63 
64  ExprAtom();
65  virtual ~ExprAtom();
66 
68  ExprAtomType getAtomType() const { return this->_atomType; }
69 
71  virtual ExprAtom * clone() const = 0;
72 
74  virtual winux::String toString() const = 0;
75 
76 protected:
77  ExprAtomType _atomType; // 表达式原子类型
78 
79  friend class ExprParser;
80 };
81 
82 class ExprOperand;
83 class VarContext;
84 class ExprPackage;
85 class Expression;
86 
89 {
90 public:
91  // 操作符函数类型
92  typedef bool (* OperatorFunction)( Expression * e, ExprOperand * arOperands[], short n, winux::SimplePointer<ExprOperand> * outRetValue, void * data );
93 
95  static bool Possibility( ExprPackage * package, winux::String const & str );
96 
97  ExprOperator( winux::String const & oprStr = "", bool isUnary = false, bool isRight = false, short level = 0, OperatorFunction oprFn = NULL );
98  virtual ~ExprOperator();
99 
100  virtual ExprAtom * clone() const;
101  virtual winux::String toString() const;
102 
107  int nexus( ExprOperator const & opr ) const;
108 
109  bool isUnary() const { return _isUnary; }
110  bool isRight() const { return _isRight; }
111  winux::String const & getStr() const { return _oprStr; }
112 
114  bool operator == ( ExprOperator const & opr ) const
115  {
116  return (
117  this->_oprStr == opr._oprStr &&
118  this->_isUnary == opr._isUnary &&
119  this->_level == opr._level &&
120  this->_isRight == opr._isRight &&
121  this->_oprFn == opr._oprFn
122  );
123  }
124 
126  bool isSameLevel( ExprOperator const & opr ) const { return _level == opr._level && _isRight == opr._isRight; }
127 
128 protected:
129  winux::String _oprStr; // 算符文本
130  bool _isUnary; // 是否一元
131  short _level; // 优先级别
132  bool _isRight; // 是否右结合
133  OperatorFunction _oprFn; // 对应函数
134 
135  friend class Expression;
136  friend class ExprPackage;
137  friend class ExprParser;
138 };
139 
142 {
143 public:
145  {
150  eotExpression
151  };
152 
153  ExprOperand();
154  virtual ~ExprOperand();
156  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result ) = 0;
158  winux::Mixed val();
159 
166  //bool evaluateUntil( ExprOperandType const * types, int n, winux::SimplePointer<ExprOperand> * result );
167 
169  bool evaluateMixedPtr( winux::Mixed ** ppv );
170 
172  ExprOperandType getOperandType() const;
173 
174 protected:
175  ExprOperandType _operandType; // 操作数类型
176 
177  friend class ExprParser;
178 };
179 
182 {
183 public:
184  ExprLiteral();
185  ExprLiteral( winux::Mixed const & val );
186  virtual ~ExprLiteral();
187 
188  virtual ExprAtom * clone() const;
189  virtual winux::String toString() const;
190  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result );
191 
192  winux::Mixed::MixedType getValueType() const;
193  winux::Mixed const & getValue() const;
194  winux::Mixed & getValue();
195  void setValue( winux::Mixed const & val );
196 
198  static bool NumberPossibility( winux::String const & str, bool * isFloat = NULL, bool * isExp = NULL/*, bool * isMinus = NULL*/ );
199 protected:
201 
202  friend class ExprParser;
203 };
204 
207 {
208 public:
209  ExprIdentifier( Expression * exprObj, winux::String const & name );
210  virtual ~ExprIdentifier();
211 
212  virtual ExprAtom * clone() const;
213  virtual winux::String toString() const;
214  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result );
215 
216  winux::String const & getName() const { return _name; }
217 
219  void setVar( winux::Mixed const & val );
220 
222  winux::Mixed const & getVar() const;
223 
225  VarContext * getVarContext() const;
226 
228  Expression * getExprObj() const { return _exprObj; }
229 
230  static bool Possibility( winux::String const & str );
231 
232 protected:
233  Expression * _exprObj; // 所属表达式对象
234  winux::String _name; // 名称
235 
236  friend class ExprParser;
237 };
238 
241 {
242 public:
243  ExprReference( winux::Mixed & ref, winux::String const & syntax );
244  virtual ~ExprReference();
245 
246  virtual ExprAtom * clone() const;
247  virtual winux::String toString() const;
248  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result );
249 
250  winux::Mixed & getRef() const { return *this->_ref; }
251 protected:
252 
255 };
256 
259 {
260 public:
261  // 函数类型
262  typedef bool (* FuncFunction)( Expression * e, std::vector<Expression *> & params, winux::SimplePointer<ExprOperand> * outRetValue, void * data );
263  // 字符串=>函数映射
264  typedef std::map< winux::String, FuncFunction > StringFuncMap;
265 
266  ExprFunc( Expression * exprObj, winux::String const & funcName );
267  virtual ~ExprFunc();
268  ExprFunc( ExprFunc const & other );
269  ExprFunc & operator = ( ExprFunc const & other );
270 
271  virtual ExprAtom * clone() const;
272  virtual winux::String toString() const;
273  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result );
274 
275 //protected:
278  std::vector<Expression *> _params;
279 
281  void _clear();
283  void _addParam( Expression * param );
284 
285 
286  friend class ExprParser;
287 };
288 
291 {
292 public:
293  Expression( ExprPackage * package, VarContext * ctx, Expression * parent, void * data );
294  virtual ~Expression();
295  Expression( Expression const & other );
296  Expression & operator = ( Expression const & other );
297 
298  virtual ExprAtom * clone() const;
299  virtual winux::String toString() const;
300  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result );
301 
302  winux::String toSuffixString() const;
303 
304  bool isEmpty() const { return this->_suffixAtoms.empty(); }
305 
307  ExprPackage * getPackage() const { return this->_package; }
308 
310  VarContext * getVarContext() const;
311 
313  void * getDataPtr() const;
314 
319  bool getVar( winux::String const & name, winux::Mixed * * outVarPtr, VarContext * * outVarCtx = nullptr ) const;
321  winux::Mixed const & getVar( winux::String const & name, VarContext * * outVarCtx = nullptr ) const;
322 
324  void setVar( winux::String const & name, winux::Mixed const & val );
325 
327  void clear();
328 
329 //protected:
330  std::vector<ExprAtom *> _suffixAtoms;
334  void * _data;
335 
337  void _addAtom( ExprAtom * atom );
338 
339  friend class ExprIdentifier;
340  friend class ExprParser;
341 };
342 
345 {
346 public:
348  {
350  bool isNewAlloc;
351 
352  VariableStruct() : p(NULL), isNewAlloc(false)
353  {
354  }
355  };
356 
357  VarContext( winux::Mixed * collection = nullptr );
358 
359  virtual ~VarContext();
360 
361  VarContext( VarContext const & other );
362 
363  VarContext & operator = ( VarContext const & other );
364 
366  void setMixedCollection( winux::Mixed * collection );
367 
372  void set( winux::String const & name, winux::Mixed const & v );
373 
378  winux::Mixed & set( winux::String const & name );
379 
384  void setPtr( winux::String const & name, winux::Mixed * v );
385 
390  winux::Mixed * & setPtr( winux::String const & name );
391 
392  bool unset( winux::String const & name );
393 
394  bool has( winux::String const & name ) const;
395 
396  bool get( winux::String const & name, winux::Mixed * * outVarPtr ) const;
397 
398  winux::Mixed const & get( winux::String const & name ) const;
399 
401  void clear();
402 
404  winux::Mixed dump() const;
405 
406 protected:
408  std::map< winux::String, VariableStruct > _vars;
409 
410  friend class ExprParser;
411 };
412 
415 {
416 public:
417  ExprPackage();
418 
420  bool oprPossibility( winux::String const & str ) const;
421 
423  void addOpr( winux::String const & oprStr, bool isUnary, bool isRight, short level, ExprOperator::OperatorFunction oprFn );
425  bool delOpr( winux::String const & oprStr, bool isUnary, bool isRight );
427  bool modifyOpr( int i, winux::String const & oprStr, bool isUnary, bool isRight, short level, ExprOperator::OperatorFunction oprFn );
429  int findOpr( ExprOperator * opr, winux::String const & oprStr, bool isUnary = false, bool isRight = false ) const;
430 
432  int getOpr( winux::String const & oprStr, ExprOperator * oprArr, int n ) const;
434  int getAllOprs( ExprOperator * oprArr, int n ) const;
435 
436 
438  void setFunc( winux::String const & funcName, ExprFunc::FuncFunction fn );
440  bool delFunc( winux::String const & funcName );
442  bool modifyFunc( winux::String const & funcName, winux::String const & newFuncName, ExprFunc::FuncFunction newFn );
444  bool findFunc( winux::String const & funcName, ExprFunc::FuncFunction * fn ) const;
446  int getAllFuncs( std::vector< std::pair< winux::String, ExprFunc::FuncFunction > > * funcs ) const;
447 
448 private:
449  std::vector<ExprOperator> _operators; // 支持的运算符
450  ExprFunc::StringFuncMap _funcsMap; // 支持的函数
451 
452  friend class ExprFunc;
453  friend class Expression;
454  friend class ExprParser;
455 };
456 
459 {
460 public:
461  ExprParser();
462  virtual ~ExprParser();
463 
464  void parse( Expression * e, winux::String const & str );
465 
466  //winux::String const & error() const { return _errStr; }
467  //int errNo() const { return _errNo; }
468 private:
469  //int _errNo;
470  //winux::String _errStr;
471  enum ExprParseContext
472  {
473  epcExpr, epcFuncParams, epcString, epcStrAntiSlashes
474  };
475  void _parseExpr( Expression * e, winux::String const & str, int & i, std::vector<ExprParseContext> & epc );
476  void _parseString( winux::String * v, winux::String const & str, int & i, std::vector<ExprParseContext> & epc );
477  void _parseStrAntiSlashes( winux::String * v, winux::String const & str, int & i, std::vector<ExprParseContext> & epc );
478  void _parseFuncParams( Expression * exprOwner, ExprFunc * funcAtom, winux::String const & str, int & i, std::vector<ExprParseContext> & epc );
479 
480 };
481 
482 } // namespace eienexpr
483 
484 #endif //__EIENEXPR_HPP__
XString< char > AnsiString
Definition: utilities.hpp:212
ExprError(int errNo, winux::AnsiString const &err)
Definition: eienexpr.hpp:51
winux::String _funcName
函数名
Definition: eienexpr.hpp:277
winux::Mixed _val
Definition: eienexpr.hpp:200
bool(* OperatorFunction)(Expression *e, ExprOperand *arOperands[], short n, winux::SimplePointer< ExprOperand > *outRetValue, void *data)
Definition: eienexpr.hpp:92
winux::String _syntax
Definition: eienexpr.hpp:254
ExprOperandType _operandType
Definition: eienexpr.hpp:175
OperatorFunction _oprFn
Definition: eienexpr.hpp:133
std::map< winux::String, FuncFunction > StringFuncMap
Definition: eienexpr.hpp:264
标识符(变量)操作数
Definition: eienexpr.hpp:206
XString< tchar > String
Definition: utilities.hpp:216
winux::Mixed * _ref
Definition: eienexpr.hpp:253
ExprAtomType getAtomType() const
原子类型
Definition: eienexpr.hpp:68
winux::String _oprStr
Definition: eienexpr.hpp:129
bool isEmpty() const
Definition: eienexpr.hpp:304
表达式操作符
Definition: eienexpr.hpp:88
ExprPackage * getPackage() const
获取表达式包
Definition: eienexpr.hpp:307
bool(* FuncFunction)(Expression *e, std::vector< Expression * > &params, winux::SimplePointer< ExprOperand > *outRetValue, void *data)
Definition: eienexpr.hpp:262
表达式库错误
Definition: eienexpr.hpp:32
std::vector< Expression * > _params
参数,也是表达式
Definition: eienexpr.hpp:278
winux::Mixed * _collection
Definition: eienexpr.hpp:407
ExprPackage * _package
表达式包
Definition: eienexpr.hpp:331
void * _data
外部数据
Definition: eienexpr.hpp:334
std::map< winux::String, VariableStruct > _vars
Definition: eienexpr.hpp:408
表达式引擎,提供一种简单的动态解释执行的功能
Definition: eienexpr.hpp:7
#define EIENEXPR_DLL
Definition: eienexpr.hpp:24
Expression * getExprObj() const
获取关联的表达式对象
Definition: eienexpr.hpp:228
bool isRight() const
Definition: eienexpr.hpp:110
winux::String const & getName() const
Definition: eienexpr.hpp:216
bool isSameLevel(ExprOperator const &opr) const
判断两个算符是否结合性和优先级都相同
Definition: eienexpr.hpp:126
Expression * _parent
父表达式
Definition: eienexpr.hpp:333
bool isNewAlloc
是否为新分配的Mixed变量
Definition: eienexpr.hpp:350
变量场景类
Definition: eienexpr.hpp:344
winux::Mixed & getRef() const
Definition: eienexpr.hpp:250
混合体,能表示多种类型的值
Definition: utilities.hpp:750
ExprAtomType _atomType
Definition: eienexpr.hpp:77
简单指针
Definition: smartptr.hpp:235
字面值操作数,无需计算
Definition: eienexpr.hpp:181
winux::String const & getStr() const
Definition: eienexpr.hpp:111
错误类
Definition: utilities.hpp:505
表达式原子
Definition: eienexpr.hpp:55
函数操作数
Definition: eienexpr.hpp:258
VarContext * _varCtx
变量场景
Definition: eienexpr.hpp:332
临时引用操作数
Definition: eienexpr.hpp:240
std::vector< ExprAtom * > _suffixAtoms
后缀式原子
Definition: eienexpr.hpp:330
bool isUnary() const
Definition: eienexpr.hpp:109
Expression * _exprObj
所属表达式对象
Definition: eienexpr.hpp:276
表达式操作数
Definition: eienexpr.hpp:141
表达式包(包含支持的算符和函数)
Definition: eienexpr.hpp:414