fastdo  0.6.16
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 const * e, ExprOperand * arOperands[], short n, winux::SimplePointer<ExprOperand> * outRetValue, void * data );
93 
95  static bool Possibility( ExprPackage const * package, winux::String const & str );
96 
97  ExprOperator( winux::String const & oprStr = $T(""), bool isUnary = false, bool isRight = false, short level = 0, OperatorFunction oprFn = NULL );
98  virtual ~ExprOperator();
99 
100  virtual ExprAtom * clone() const override;
101  virtual winux::String toString() const override;
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 ) const = 0;
158  winux::Mixed val() const;
159 
161  bool evaluateMixedPtr( winux::Mixed ** ppv ) const;
162 
164  ExprOperandType getOperandType() const;
165 
166 protected:
167  ExprOperandType _operandType; // 操作数类型
168 
169  friend class ExprParser;
170 };
171 
174 {
175 public:
176  ExprLiteral();
177  ExprLiteral( winux::Mixed const & val );
178  virtual ~ExprLiteral();
179 
180  virtual ExprAtom * clone() const override;
181  virtual winux::String toString() const override;
182  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result ) const override;
183 
184  winux::Mixed::MixedType getValueType() const { return this->_val._type; }
185  winux::Mixed const & getValue() const { return this->_val; }
186  winux::Mixed & getValue() { return this->_val; }
187  void setValue( winux::Mixed const & val ) { this->_val = val; }
188 
190  static bool NumberPossibility( winux::String const & str, bool * isFloat = NULL, bool * isExp = NULL );
191 
192 protected:
194 
195  friend class ExprParser;
196 };
197 
200 {
201 public:
202  ExprIdentifier( Expression * exprObj, winux::String const & name );
203  virtual ~ExprIdentifier();
204 
205  virtual ExprAtom * clone() const override;
206  virtual winux::String toString() const override;
207  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result ) const override;
208 
209  winux::String const & getName() const { return _name; }
210 
212  void setVar( winux::Mixed const & val );
213 
215  winux::Mixed const & getVar() const;
216 
218  VarContext * getVarContext() const;
219 
221  Expression * getExprObj() const { return _exprObj; }
222 
223  static bool Possibility( winux::String const & str );
224 
225 protected:
226  Expression * _exprObj; // 所属表达式对象
227  winux::String _name; // 名称
228 
229  friend class ExprParser;
230 };
231 
234 {
235 public:
236  ExprReference( winux::Mixed & ref, winux::String const & syntax );
237  virtual ~ExprReference();
238 
239  virtual ExprAtom * clone() const override;
240  virtual winux::String toString() const override;
241  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result ) const override;
242 
243  winux::Mixed & getRef() const { return *this->_ref; }
244 protected:
245 
248 };
249 
252 {
253 public:
254  // 函数类型
255  typedef bool (* FuncFunction)( Expression * e, std::vector<Expression *> const & params, winux::SimplePointer<ExprOperand> * outRetValue, void * data );
256  // 字符串=>函数映射
257  typedef std::map< winux::String, FuncFunction > StringFuncMap;
258 
259  ExprFunc( Expression * exprObj, winux::String const & funcName );
260  virtual ~ExprFunc();
261  ExprFunc( ExprFunc const & other );
262  ExprFunc & operator = ( ExprFunc const & other );
263 
264  virtual ExprAtom * clone() const override;
265  virtual winux::String toString() const override;
266  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result ) const override;
267 
268 //protected:
271  std::vector<Expression *> _params;
272 
274  void _clear();
276  void _addParam( Expression * param );
277 
278 
279  friend class ExprParser;
280 };
281 
284 {
285 public:
286  Expression( ExprPackage * package, VarContext * ctx, Expression * parent, void * data );
287  virtual ~Expression();
288  Expression( Expression const & other );
289  Expression & operator = ( Expression const & other );
290 
291  virtual ExprAtom * clone() const override;
292  virtual winux::String toString() const override;
293  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result ) const override;
294 
295  winux::String toSuffixString() const;
296 
297  bool isEmpty() const { return this->_suffixAtoms.empty(); }
298 
300  ExprPackage * getPackage() const { return this->_package; }
301 
303  VarContext * getVarContext() const;
304 
306  void * getDataPtr() const;
307 
312  bool getVar( winux::String const & name, winux::Mixed * * outVarPtr, VarContext * * outVarCtx = nullptr ) const;
314  winux::Mixed const & getVar( winux::String const & name, VarContext * * outVarCtx = nullptr ) const;
315 
317  void setVar( winux::String const & name, winux::Mixed const & val );
318 
320  void clear();
321 
322 //protected:
323  std::vector<ExprAtom *> _suffixAtoms;
327  void * _data;
328 
330  void _addAtom( ExprAtom * atom );
331 
332  friend class ExprIdentifier;
333  friend class ExprParser;
334 };
335 
338 {
339 public:
341  {
343  bool isNewAlloc;
344 
345  VariableStruct() : p(NULL), isNewAlloc(false)
346  {
347  }
348  };
349 
350  VarContext( winux::Mixed * collection = nullptr );
351 
352  virtual ~VarContext();
353 
354  VarContext( VarContext const & other );
355 
356  VarContext & operator = ( VarContext const & other );
357 
359  void setMixedCollection( winux::Mixed * collection );
360 
365  void set( winux::String const & name, winux::Mixed const & v );
366 
371  winux::Mixed & set( winux::String const & name );
372 
377  void setPtr( winux::String const & name, winux::Mixed * v );
378 
383  winux::Mixed * & setPtr( winux::String const & name );
384 
385  bool unset( winux::String const & name );
386 
387  bool has( winux::String const & name ) const;
388 
389  bool get( winux::String const & name, winux::Mixed * * outVarPtr ) const;
390 
391  winux::Mixed const & get( winux::String const & name ) const;
392 
394  void clear();
395 
397  winux::Mixed dump() const;
398 
399 protected:
401  std::map< winux::String, VariableStruct > _vars;
402 
403  friend class ExprParser;
404 };
405 
408 {
409 public:
411  ExprPackage();
412 
414  bool oprPossibility( winux::String const & str ) const;
415 
417  void addOpr( winux::String const & oprStr, bool isUnary, bool isRight, short level, ExprOperator::OperatorFunction oprFn );
419  bool delOpr( winux::String const & oprStr, bool isUnary, bool isRight );
421  bool modifyOpr( int i, winux::String const & oprStr, bool isUnary, bool isRight, short level, ExprOperator::OperatorFunction oprFn );
423  int findOpr( ExprOperator * opr, winux::String const & oprStr, bool isUnary = false, bool isRight = false ) const;
424 
426  int getOpr( winux::String const & oprStr, ExprOperator * oprArr, int n ) const;
428  int getAllOprs( ExprOperator * oprArr, int n ) const;
429 
430 
432  void setFunc( winux::String const & funcName, ExprFunc::FuncFunction fn );
434  bool delFunc( winux::String const & funcName );
436  bool modifyFunc( winux::String const & funcName, winux::String const & newFuncName, ExprFunc::FuncFunction newFn );
438  bool findFunc( winux::String const & funcName, ExprFunc::FuncFunction * fn ) const;
440  int getAllFuncs( std::vector< std::pair< winux::String, ExprFunc::FuncFunction > > * funcs ) const;
441 
442 private:
443  std::vector<ExprOperator> _operators; // 支持的运算符
444  ExprFunc::StringFuncMap _funcsMap; // 支持的函数
445 
446  friend class ExprFunc;
447  friend class Expression;
448  friend class ExprParser;
449 };
450 
453 {
454 public:
455  ExprParser();
456  virtual ~ExprParser();
457 
458  void parse( Expression * e, winux::String const & str );
459 
460 private:
461  enum ExprParseContext
462  {
463  epcExpr, epcFuncParams, epcString, epcStrAntiSlashes
464  };
465  void _parseExpr( Expression * e, winux::String const & str, int & i, std::vector<ExprParseContext> & epc );
466  void _parseString( winux::String * v, winux::String const & str, int & i, std::vector<ExprParseContext> & epc );
467  void _parseStrAntiSlashes( winux::String * v, winux::String const & str, int & i, std::vector<ExprParseContext> & epc );
468  void _parseFuncParams( Expression * exprOwner, ExprFunc * funcAtom, winux::String const & str, int & i, std::vector<ExprParseContext> & epc );
469 
470 };
471 
472 } // namespace eienexpr
473 
474 #endif //__EIENEXPR_HPP__
XString< char > AnsiString
Definition: utilities.hpp:257
MixedType
混合体类型识别常量
Definition: utilities.hpp:1484
ExprError(int errNo, winux::AnsiString const &err)
Definition: eienexpr.hpp:51
bool(* FuncFunction)(Expression *e, std::vector< Expression * > const &params, winux::SimplePointer< ExprOperand > *outRetValue, void *data)
Definition: eienexpr.hpp:255
winux::String _funcName
函数名
Definition: eienexpr.hpp:270
winux::Mixed _val
Definition: eienexpr.hpp:193
winux::String _syntax
Definition: eienexpr.hpp:247
winux::Mixed const & getValue() const
Definition: eienexpr.hpp:185
ExprOperandType _operandType
Definition: eienexpr.hpp:167
OperatorFunction _oprFn
Definition: eienexpr.hpp:133
std::map< winux::String, FuncFunction > StringFuncMap
Definition: eienexpr.hpp:257
标识符(变量)操作数
Definition: eienexpr.hpp:199
XString< tchar > String
Definition: utilities.hpp:261
winux::Mixed * _ref
Definition: eienexpr.hpp:246
ExprAtomType getAtomType() const
原子类型
Definition: eienexpr.hpp:68
winux::String _oprStr
Definition: eienexpr.hpp:129
bool isEmpty() const
Definition: eienexpr.hpp:297
表达式操作符
Definition: eienexpr.hpp:88
ExprPackage * getPackage() const
获取表达式包
Definition: eienexpr.hpp:300
表达式库错误
Definition: eienexpr.hpp:32
bool(* OperatorFunction)(Expression const *e, ExprOperand *arOperands[], short n, winux::SimplePointer< ExprOperand > *outRetValue, void *data)
Definition: eienexpr.hpp:92
std::vector< Expression * > _params
参数,也是表达式
Definition: eienexpr.hpp:271
winux::Mixed * _collection
Definition: eienexpr.hpp:400
ExprPackage * _package
表达式包
Definition: eienexpr.hpp:324
void * _data
外部数据
Definition: eienexpr.hpp:327
std::map< winux::String, VariableStruct > _vars
Definition: eienexpr.hpp:401
表达式引擎,提供一种简单的动态解释执行的功能
Definition: eienexpr.hpp:7
#define EIENEXPR_DLL
Definition: eienexpr.hpp:24
Expression * getExprObj() const
获取关联的表达式对象
Definition: eienexpr.hpp:221
bool isRight() const
Definition: eienexpr.hpp:110
void setValue(winux::Mixed const &val)
Definition: eienexpr.hpp:187
winux::Mixed & getValue()
Definition: eienexpr.hpp:186
winux::String const & getName() const
Definition: eienexpr.hpp:209
bool isSameLevel(ExprOperator const &opr) const
判断两个算符是否结合性和优先级都相同
Definition: eienexpr.hpp:126
Expression * _parent
父表达式
Definition: eienexpr.hpp:326
bool isNewAlloc
是否为新分配的Mixed变量
Definition: eienexpr.hpp:343
变量场景类
Definition: eienexpr.hpp:337
winux::Mixed::MixedType getValueType() const
Definition: eienexpr.hpp:184
winux::Mixed & getRef() const
Definition: eienexpr.hpp:243
混合体,能表示多种类型的值
Definition: utilities.hpp:1440
ExprAtomType _atomType
Definition: eienexpr.hpp:77
简单指针
Definition: smartptr.hpp:302
字面值操作数,无需计算
Definition: eienexpr.hpp:173
winux::String const & getStr() const
Definition: eienexpr.hpp:111
错误类
Definition: utilities.hpp:838
表达式原子
Definition: eienexpr.hpp:55
函数操作数
Definition: eienexpr.hpp:251
VarContext * _varCtx
变量场景
Definition: eienexpr.hpp:325
临时引用操作数
Definition: eienexpr.hpp:233
std::vector< ExprAtom * > _suffixAtoms
后缀式原子
Definition: eienexpr.hpp:323
bool isUnary() const
Definition: eienexpr.hpp:109
Expression * _exprObj
所属表达式对象
Definition: eienexpr.hpp:269
表达式操作数
Definition: eienexpr.hpp:141
表达式包(包含支持的算符和函数)
Definition: eienexpr.hpp:407