1 /*
2                  __________
3     _____   __ __\______   \_____  _______  ______  ____ _______
4    /     \ |  |  \|     ___/\__  \ \_  __ \/  ___/_/ __ \\_  __ \
5   |  Y Y  \|  |  /|    |     / __ \_|  | \/\___ \ \  ___/ |  | \/
6   |__|_|  /|____/ |____|    (____  /|__|  /____  > \___  >|__|
7         \/                       \/            \/      \/
8   Copyright (C) 2011 Ingo Berg
9 
10   Permission is hereby granted, free of charge, to any person obtaining a copy of this
11   software and associated documentation files (the "Software"), to deal in the Software
12   without restriction, including without limitation the rights to use, copy, modify,
13   merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
14   permit persons to whom the Software is furnished to do so, subject to the following conditions:
15 
16   The above copyright notice and this permission notice shall be included in all copies or
17   substantial portions of the Software.
18 
19   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
20   NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22   DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #ifndef MUP_DEF_H
26 #define MUP_DEF_H
27 
28 #include <iostream>
29 #include <string>
30 #include <sstream>
31 #include <map>
32 
33 #include "muParserFixes.h"
34 
35 /** \file
36     \brief This file contains standard definitions used by the parser.
37 */
38 
39 #define MUP_VERSION _T("2.0.0")
40 #define MUP_VERSION_DATE _T("20110803; SF-SVN")
41 
42 #define MUP_CHARS _T("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
43 
44 /** \brief If this macro is defined mathematical exceptions (div by zero) will be thrown as exceptions. */
45 //#define MUP_MATH_EXCEPTIONS
46 
47 /** \brief Define the base datatype for values.
48 
49   This datatype must be a built in value type. You can not use custom classes.
50   It has been tested with float, double and long double types, int should
51   work as well.
52 */
53 #define MUP_BASETYPE double
54 
55 /** \brief Activate this option in order to compile with OpenMP support.
56 
57   OpenMP is used only in the bulk mode it may increase the performance a bit.
58 */
59 //#define MUP_USE_OPENMP
60 
61 #if defined(_UNICODE)
62   /** \brief Definition of the basic parser string type. */
63   #define MUP_STRING_TYPE std::wstring
64 
65   #if !defined(_T)
66     #define _T(x) L##x
67   #endif // not defined _T
68 #else
69   #ifndef _T
70   #define _T(x) x
71   #endif
72 
73   /** \brief Definition of the basic parser string type. */
74   #define MUP_STRING_TYPE std::string
75 #endif
76 
77 #if defined(_DEBUG)
78   /** \brief Debug macro to force an abortion of the programm with a certain message.
79   */
80   #define MUP_FAIL(MSG)    \
81           bool MSG=false;  \
82           assert(MSG);
83 
84     /** \brief An assertion that does not kill the program.
85 
86         This macro is neutralised in UNICODE builds. It's
87         too difficult to translate.
88     */
89     #define MUP_ASSERT(COND)                         \
90             if (!(COND))                             \
91             {                                        \
92               stringstream_type ss;                  \
93               ss << _T("Assertion \"") _T(#COND) _T("\" failed: ") \
94                  << __FILE__ << _T(" line ")         \
95                  << __LINE__ << _T(".");             \
96               throw ParserError( ss.str() );         \
97             }
98 #else
99   #define MUP_FAIL(MSG)
100   #define MUP_ASSERT(COND)
101 #endif
102 
103 
104 namespace mu
105 {
106 #if defined(_UNICODE)
107 
108   //------------------------------------------------------------------------------
109   /** \brief Encapsulate wcout. */
console()110   inline std::wostream& console()
111   {
112     return std::wcout;
113   }
114 
115   /** \brief Encapsulate cin. */
console_in()116   inline std::wistream& console_in()
117   {
118     return std::wcin;
119   }
120 
121 #else
122 
123   /** \brief Encapsulate cout.
124 
125     Used for supporting UNICODE more easily.
126   */
127   inline std::ostream& console()
128   {
129     return std::cout;
130   }
131 
132   /** \brief Encapsulate cin.
133 
134     Used for supporting UNICODE more easily.
135   */
136   inline std::istream& console_in()
137   {
138     return std::cin;
139   }
140 
141 #endif
142 
143   //------------------------------------------------------------------------------
144   /** \brief Bytecode values.
145 
146       \attention The order of the operator entries must match the order in ParserBase::c_DefaultOprt!
147   */
148   enum ECmdCode
149   {
150     // The following are codes for built in binary operators
151     // apart from built in operators the user has the opportunity to
152     // add user defined operators.
153     cmLE            = 0,   ///< Operator item:  less or equal
154     cmGE            = 1,   ///< Operator item:  greater or equal
155     cmNEQ           = 2,   ///< Operator item:  not equal
156     cmEQ            = 3,   ///< Operator item:  equals
157     cmLT            = 4,   ///< Operator item:  less than
158     cmGT            = 5,   ///< Operator item:  greater than
159     cmADD           = 6,   ///< Operator item:  add
160     cmSUB           = 7,   ///< Operator item:  subtract
161     cmMUL           = 8,   ///< Operator item:  multiply
162     cmDIV           = 9,   ///< Operator item:  division
163     cmPOW           = 10,  ///< Operator item:  y to the power of ...
164     cmLAND          = 11,
165     cmLOR           = 12,
166     cmASSIGN        = 13,  ///< Operator item:  Assignment operator
167     cmBO            = 14,  ///< Operator item:  opening bracket
168     cmBC            = 15,  ///< Operator item:  closing bracket
169     cmIF            = 16,  ///< For use in the ternary if-then-else operator
170     cmELSE          = 17,  ///< For use in the ternary if-then-else operator
171     cmENDIF         = 18,  ///< For use in the ternary if-then-else operator
172     cmARG_SEP,             ///< function argument separator
173     cmVAR,                 ///< variable item
174     cmVAL,                 ///< value item
175     cmFUNC,                ///< Code for a function item
176     cmFUNC_STR,            ///< Code for a function with a string parameter
177     cmFUNC_BULK,           ///< Special callbacks for Bulk mode with an additional parameter for the bulk index
178     cmSTRING,              ///< Code for a string token
179     cmOPRT_BIN,            ///< user defined binary operator
180     cmOPRT_POSTFIX,        ///< code for postfix operators
181     cmOPRT_INFIX,          ///< code for infix operators
182     cmEND,                 ///< end of formula
183     cmUNKNOWN              ///< uninitialized item
184   };
185 
186   //------------------------------------------------------------------------------
187   /** \brief Types internally used by the parser.
188   */
189   enum ETypeCode
190   {
191     tpSTR  = 0,     ///< String type (Function arguments and constants only, no string variables)
192     tpDBL  = 1,     ///< Floating point variables
193     tpVOID = 2      ///< Undefined type.
194   };
195 
196   //------------------------------------------------------------------------------
197   enum EParserVersionInfo
198   {
199     pviBRIEF,
200     pviFULL
201   };
202 
203   //------------------------------------------------------------------------------
204   /** \brief Parser operator precedence values. */
205   enum EOprtAssociativity
206   {
207     oaLEFT  = 0,
208     oaRIGHT = 1,
209     oaNONE  = 2
210   };
211 
212   //------------------------------------------------------------------------------
213   /** \brief Parser operator precedence values. */
214   enum EOprtPrecedence
215   {
216     // binary operators
217     prLOR     = 1,
218     prLAND    = 2,
219     prLOGIC   = 3,  ///< logic operators
220     prCMP     = 4,  ///< comparsion operators
221     prADD_SUB = 5,  ///< addition
222     prMUL_DIV = 6,  ///< multiplication/division
223     prPOW     = 7,  ///< power operator priority (highest)
224 
225     // infix operators
226     prINFIX   = 6, ///< Signs have a higher priority than ADD_SUB, but lower than power operator
227     prPOSTFIX = 6  ///< Postfix operator priority (currently unused)
228   };
229 
230   //------------------------------------------------------------------------------
231   // basic types
232 
233   /** \brief The numeric datatype used by the parser.
234 
235     Normally this is a floating point type either single or double precision.
236   */
237   typedef MUP_BASETYPE value_type;
238 
239   /** \brief The stringtype used by the parser.
240 
241     Depends on wether UNICODE is used or not.
242   */
243   typedef MUP_STRING_TYPE string_type;
244 
245   /** \brief The character type used by the parser.
246 
247     Depends on wether UNICODE is used or not.
248   */
249   typedef string_type::value_type char_type;
250 
251   /** \brief Typedef for easily using stringstream that respect the parser stringtype. */
252   typedef std::basic_stringstream<char_type,
253                                   std::char_traits<char_type>,
254                                   std::allocator<char_type> > stringstream_type;
255 
256   // Data container types
257 
258   /** \brief Type used for storing variables. */
259   typedef std::map<string_type, value_type*> varmap_type;
260 
261   /** \brief Type used for storing constants. */
262   typedef std::map<string_type, value_type> valmap_type;
263 
264   /** \brief Type for assigning a string name to an index in the internal string table. */
265   typedef std::map<string_type, std::size_t> strmap_type;
266 
267   // Parser callbacks
268 
269   /** \brief Callback type used for functions without arguments. */
270   typedef value_type (*fun_type0)();
271 
272   /** \brief Callback type used for functions with a single arguments. */
273   typedef value_type (*fun_type1)(value_type);
274 
275   /** \brief Callback type used for functions with two arguments. */
276   typedef value_type (*fun_type2)(value_type, value_type);
277 
278   /** \brief Callback type used for functions with three arguments. */
279   typedef value_type (*fun_type3)(value_type, value_type, value_type);
280 
281   /** \brief Callback type used for functions with four arguments. */
282   typedef value_type (*fun_type4)(value_type, value_type, value_type, value_type);
283 
284   /** \brief Callback type used for functions with five arguments. */
285   typedef value_type (*fun_type5)(value_type, value_type, value_type, value_type, value_type);
286 
287   /** \brief Callback type used for functions with five arguments. */
288   typedef value_type (*fun_type6)(value_type, value_type, value_type, value_type, value_type, value_type);
289 
290   /** \brief Callback type used for functions with five arguments. */
291   typedef value_type (*fun_type7)(value_type, value_type, value_type, value_type, value_type, value_type, value_type);
292 
293   /** \brief Callback type used for functions with five arguments. */
294   typedef value_type (*fun_type8)(value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type);
295 
296   /** \brief Callback type used for functions with five arguments. */
297   typedef value_type (*fun_type9)(value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type);
298 
299   /** \brief Callback type used for functions with five arguments. */
300   typedef value_type (*fun_type10)(value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type);
301 
302   /** \brief Callback type used for functions without arguments. */
303   typedef value_type (*bulkfun_type0)(int, int);
304 
305   /** \brief Callback type used for functions with a single arguments. */
306   typedef value_type (*bulkfun_type1)(int, int, value_type);
307 
308   /** \brief Callback type used for functions with two arguments. */
309   typedef value_type (*bulkfun_type2)(int, int, value_type, value_type);
310 
311   /** \brief Callback type used for functions with three arguments. */
312   typedef value_type (*bulkfun_type3)(int, int, value_type, value_type, value_type);
313 
314   /** \brief Callback type used for functions with four arguments. */
315   typedef value_type (*bulkfun_type4)(int, int, value_type, value_type, value_type, value_type);
316 
317   /** \brief Callback type used for functions with five arguments. */
318   typedef value_type (*bulkfun_type5)(int, int, value_type, value_type, value_type, value_type, value_type);
319 
320   /** \brief Callback type used for functions with five arguments. */
321   typedef value_type (*bulkfun_type6)(int, int, value_type, value_type, value_type, value_type, value_type, value_type);
322 
323   /** \brief Callback type used for functions with five arguments. */
324   typedef value_type (*bulkfun_type7)(int, int, value_type, value_type, value_type, value_type, value_type, value_type, value_type);
325 
326   /** \brief Callback type used for functions with five arguments. */
327   typedef value_type (*bulkfun_type8)(int, int, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type);
328 
329   /** \brief Callback type used for functions with five arguments. */
330   typedef value_type (*bulkfun_type9)(int, int, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type);
331 
332   /** \brief Callback type used for functions with five arguments. */
333   typedef value_type (*bulkfun_type10)(int, int, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type);
334 
335   /** \brief Callback type used for functions with a variable argument list. */
336   typedef value_type (*multfun_type)(const value_type*, int);
337 
338   /** \brief Callback type used for functions taking a string as an argument. */
339   typedef value_type (*strfun_type1)(const char_type*);
340 
341   /** \brief Callback type used for functions taking a string and a value as arguments. */
342   typedef value_type (*strfun_type2)(const char_type*, value_type);
343 
344   /** \brief Callback type used for functions taking a string and two values as arguments. */
345   typedef value_type (*strfun_type3)(const char_type*, value_type, value_type);
346 
347   /** \brief Callback used for functions that identify values in a string. */
348   typedef int (*identfun_type)(const char_type *sExpr, int *nPos, value_type *fVal);
349 
350   /** \brief Callback used for variable creation factory functions. */
351   typedef value_type* (*facfun_type)(const char_type*, void*);
352 
353 } // end fo namespace
354 
355 #endif
356 
357