wxArt2D
eval.h
1 /*! \file wx/canextobj/eval.h
2  \author Klaas Holwerda
3 
4  Copyright: 2000-2004 (c) Klaas Holwerda
5 
6  Licence: wxWidgets Licence
7 
8  RCS-ID: $Id: eval.h,v 1.8 2008/09/05 19:01:10 titato Exp $
9 */
10 #ifndef __WXEVAL_H__
11 #define __WXEVAL_H__
12 
13 #ifndef WX_PRECOMP
14 #include "wx/wx.h"
15 #endif
16 
17 #if !wxUSE_EXCEPTIONS
18 #include <setjmp.h>
19 #endif
20 
21 #ifdef A2DEDITORMAKINGDLL
22 #define A2DCANVASDLLEXP WXEXPORT
23 #define A2DCANVASDLLEXP_DATA(type) WXEXPORT type
24 #define A2DCANVASDLLEXP_CTORFN
25 #elif defined(WXART2D_USINGDLL)
26 #define A2DCANVASDLLEXP WXIMPORT
27 #define A2DCANVASDLLEXP_DATA(type) WXIMPORT type
28 #define A2DCANVASDLLEXP_CTORFN
29 #else // not making nor using DLL
30 #define A2DCANVASDLLEXP
31 #define A2DCANVASDLLEXP_DATA(type) type
32 #define A2DCANVASDLLEXP_CTORFN
33 #endif
34 
35 /* Some of you may choose to define TYPENUMBER as a "float" instead... */
36 #define TYPENUMBER double /*!< Type of numbers to work with */
37 
38 #define VARLEN 15 /*!< Max length of variable names */
39 #define TOKLEN 30 /*!< Max token length */
40 
41 #define VAR 1
42 #define DEL 2
43 #define NUM 3
44 
45 //! holds a variable in an evaluation string for a2dEval
46 /*!
47  \ingroup global
48 */
49 class A2DCANVASDLLEXP a2dEvalVar
50 {
51 public:
52 
53  a2dEvalVar( const wxString& name, TYPENUMBER value )
54  {
55  m_name = name;
56  m_value = value;
57  }
58 
59  ~a2dEvalVar() {}
60 
61  wxString m_name; /* Variable name */
62  TYPENUMBER m_value; /* Variable value */
63 };
64 
65 
66 //! holds a function in an evaluation string for a2dEval
67 /*!
68  \ingroup global
69 */
70 class A2DCANVASDLLEXP a2dEvalFunction
71 {
72  typedef TYPENUMBER ( *fArgX )( ... ); // Sloppy function type used internally
73 
74 public:
75 
76  // Function types I can accept in my ctors
77  typedef TYPENUMBER ( *fArg1 )( TYPENUMBER );
78  typedef TYPENUMBER ( *fArg2 )( TYPENUMBER, TYPENUMBER );
79 
80  //! name, nr args, function to call
81  a2dEvalFunction( const wxString& name, fArg1 func )
82  {
83  m_name = name;
84  m_args = 1;
85  m_func = ( fArgX )func;
86  }
87 
88  a2dEvalFunction( const wxString& name, fArg2 func )
89  {
90  m_name = name;
91  m_args = 2;
92  m_func = ( fArgX )func;
93  }
94 
95  wxString m_name; /* Function name */
96  int m_args; /* Number of arguments to expect */
97  fArgX m_func;
98 };
99 
100 #include <wx/listimpl.cpp>
101 WX_DECLARE_LIST_WITH_DECL( a2dEvalFunction, a2dFunctionList, class A2DCANVASDLLEXP );
102 WX_DECLARE_LIST_WITH_DECL( a2dEvalVar, a2dVariableList, class A2DCANVASDLLEXP );
103 
104 
105 /* The following macros are ASCII dependant, no EBCDIC here! */
106 #define iswhite(c) (c == _T(' ') || c == _T('\t'))
107 #define isnumer(c) ((c >= _T('0') && c <= _T('9')) || c == _T('.'))
108 #define isalphaeval(c) ((c >= _T('a') && c <= _T('z')) || (c >= _T('0') && c <= _T('9')) \
109  || c == _T('_'))
110 #define isdelim(c) (c == _T('+') || c == _T('-') || c == _T('*') || c == _T('/') || c == _T('%') \
111  || c == _T('^') || c == _T('(') || c == _T(')') || c == _T(',') || c == _T('='))
112 
113 /* Codes returned from the evaluator */
114 #define E_OK 0 /*!< Successful evaluation */
115 #define E_SYNTAX 1 /*!< Syntax error */
116 #define E_UNBALAN 2 /*!< Unbalanced parenthesis */
117 #define E_DIVZERO 3 /*!< Attempted division by zero */
118 #define E_UNKNOWN 4 /*!< Reference to unknown variable */
119 #define E_MAXVARS 5 /*!< Maximum variables exceeded */
120 #define E_BADFUNC 6 /*!< Unrecognised function */
121 #define E_NUMARGS 7 /*!< Wrong number of arguments to funtion */
122 #define E_NOARG 8 /*!< Missing an argument to a funtion */
123 #define E_EMPTY 9 /*!< Empty expression */
124 
125 //! expression(s) evaluation based on a string.
126 /*!
127 The a2dEval module provides a class that allows you to incorporate
128 mathematical expression evaluation into your programs, that can be changed
129 at run time. For example, using the class you can evaluate such expressions as:
130  \verbatim
131  1+1
132  10 * (x=5) <== Assigns 5 to X first!
133  ((1/3) * sin(45))^2
134  X=50
135  Y=100
136  z=hypot(x,y)
137  \endverbatim
138 
139 You can extend Constants and Functions that can be used.
140 
141  \ingroup global
142 */
143 class A2DCANVASDLLEXP a2dEval
144 {
145 
146 public:
147 
148  a2dEval();
149 
150  ~a2dEval();
151 
152  void SetEvalString( const wxString& toEval ) { m_eval = toEval; }
153 
154  bool GetSymbol( const wxString& envname, TYPENUMBER* envValue );
155 
156  void ClearAllVars();
157 
158  bool ClearVar( const wxString& name );
159 
160  int Evaluate( TYPENUMBER* result, int* a );
161 
162  void Parse();
163 
164  bool GetValue( const wxString& name, TYPENUMBER* value );
165 
166  bool SetValue( const wxString& name, TYPENUMBER value );
167 
168  a2dFunctionList m_functions;
169  a2dVariableList m_vars;
170  a2dVariableList m_const;
171 
172 protected:
173 
174  int Level1( TYPENUMBER* r );
175  void Level2( TYPENUMBER* r );
176  void Level3( TYPENUMBER* r );
177  void Level4( TYPENUMBER* r );
178  void Level5( TYPENUMBER* r );
179  void Level6( TYPENUMBER* r );
180 
181 private:
182 
183  wxString m_eval;
184 
185  //! Pointer to the user's expression
186 #if wxUSE_UNICODE
187  const wxStringCharType* m_expression;
188  //! Holds the current token
189  const wxStringCharType* m_token;
190 #else
191  wxChar* m_expression;
192  //! Holds the current token
193  wxChar* m_token;
194 #endif // wxUSE_UNICODE
195 
196  wxString m_tokenstr;
197 
198  //! Type of the current token
199  int m_type;
200 
201 #if !wxUSE_EXCEPTIONS
202  //! jmp_buf for errors
203  jmp_buf m_jb;
204 #endif
205 };
206 
207 #endif
208 
holds a function in an evaluation string for a2dEval
Definition: eval.h:70
holds a variable in an evaluation string for a2dEval
Definition: eval.h:49
expression(s) evaluation based on a string.
Definition: eval.h:143
a2dEvalFunction(const wxString &name, fArg1 func)
name, nr args, function to call
Definition: eval.h:81
eval.h Source File -- Sun Oct 12 2014 17:04:19 -- Sun Oct 12 2014 -- 1.8.5 -- wxArt2D -- . -- Main Page Reference Documentation