muParser API -  1.35
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
muParserBytecode.h
Go to the documentation of this file.
1 /*
2  __________
3  _____ __ __\______ \_____ _______ ______ ____ _______
4  / \ | | \| ___/\__ \ \_ __ \/ ___/_/ __ \\_ __ \
5  | Y Y \| | /| | / __ \_| | \/\___ \ \ ___/ | | \/
6  |__|_| /|____/ |____| (____ /|__| /____ > \___ >|__|
7  \/ \/ \/ \/
8  Copyright (C) 2004-2013 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 MU_PARSER_BYTECODE_H
26 #define MU_PARSER_BYTECODE_H
27 
28 #include <cassert>
29 #include <string>
30 #include <stack>
31 #include <vector>
32 
33 #include "muParserDef.h"
34 #include "muParserError.h"
35 #include "muParserToken.h"
36 
37 /** \file
38  \brief Definition of the parser bytecode class.
39 */
40 
41 
42 namespace mu
43 {
44  struct SToken
45  {
46  ECmdCode Cmd;
47  int StackPos;
48 
49  union
50  {
51  struct //SValData
52  {
53  value_type *ptr;
54  value_type data;
55  value_type data2;
56  } Val;
57 
58  struct //SFunData
59  {
60  // Note: generic_fun_type is merely a placeholder. The real type could be
61  // anything between gun_type1 and fun_type9. I can't use a void
62  // pointer due to constraints in the ANSI standard which allows
63  // data pointers and function pointers to differ in size.
64  generic_fun_type ptr;
65  int argc;
66  int idx;
67  } Fun;
68 
69  struct //SOprtData
70  {
71  value_type *ptr;
72  int offset;
73  } Oprt;
74  };
75  };
76 
77 
78  /** \brief Bytecode implementation of the Math Parser.
79 
80  The bytecode contains the formula converted to revers polish notation stored in a continious
81  memory area. Associated with this data are operator codes, variable pointers, constant
82  values and function pointers. Those are necessary in order to calculate the result.
83  All those data items will be casted to the underlying datatype of the bytecode.
84 
85  \author (C) 2004-2013 Ingo Berg
86 */
88 {
89 private:
90 
91  /** \brief Token type for internal use only. */
93 
94  /** \brief Token vector for storing the RPN. */
95  typedef std::vector<SToken> rpn_type;
96 
97  /** \brief Position in the Calculation array. */
98  unsigned m_iStackPos;
99 
100  /** \brief Maximum size needed for the stack. */
101  std::size_t m_iMaxStackSize;
102 
103  /** \brief The actual rpn storage. */
104  rpn_type m_vRPN;
105 
106  bool m_bEnableOptimizer;
107 
108  void ConstantFolding(ECmdCode a_Oprt);
109 
110 public:
111 
112  ParserByteCode();
113  ParserByteCode(const ParserByteCode &a_ByteCode);
114  ParserByteCode& operator=(const ParserByteCode &a_ByteCode);
115  void Assign(const ParserByteCode &a_ByteCode);
116 
117  void AddVar(value_type *a_pVar);
118  void AddVal(value_type a_fVal);
119  void AddOp(ECmdCode a_Oprt);
120  void AddIfElse(ECmdCode a_Oprt);
121  void AddAssignOp(value_type *a_pVar);
122  void AddFun(generic_fun_type a_pFun, int a_iArgc);
123  void AddBulkFun(generic_fun_type a_pFun, int a_iArgc);
124  void AddStrFun(generic_fun_type a_pFun, int a_iArgc, int a_iIdx);
125 
126  void EnableOptimizer(bool bStat);
127 
128  void Finalize();
129  void clear();
130  std::size_t GetMaxStackSize() const;
131  std::size_t GetSize() const;
132 
133  const SToken* GetBase() const;
134  void AsciiDump();
135 };
136 
137 } // namespace mu
138 
139 #endif
140 
141 
void AddStrFun(generic_fun_type a_pFun, int a_iArgc, int a_iIdx)
Add Strung function entry to the parser bytecode.
void AddBulkFun(generic_fun_type a_pFun, int a_iArgc)
Add a bulk function to bytecode.
void AddAssignOp(value_type *a_pVar)
Add an assignment operator.
void AddFun(generic_fun_type a_pFun, int a_iArgc)
Add function to bytecode.
void Assign(const ParserByteCode &a_ByteCode)
Copy state of another object to this.
std::size_t GetSize() const
Returns the number of entries in the bytecode.
void AddOp(ECmdCode a_Oprt)
Add an operator identifier to bytecode.
void AsciiDump()
Dump bytecode (for debugging only!).
ParserByteCode()
Bytecode default constructor.
void AddVal(value_type a_fVal)
Add a Variable pointer to bytecode.
ECmdCode
Bytecode values.
Definition: muParserDef.h:149
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:247
Namespace for mathematical applications.
Definition: muParser.cpp:49
value_type(* generic_fun_type)()
Callback type used for functions without arguments.
Definition: muParserDef.h:280
Bytecode implementation of the Math Parser.
This file contains the parser token definition.
void clear()
Delete the bytecode.
void AddVar(value_type *a_pVar)
Add a Variable pointer to bytecode.
ParserByteCode & operator=(const ParserByteCode &a_ByteCode)
Assignment operator.
void Finalize()
Add end marker to bytecode.
This file defines the error class used by the parser.
This file contains standard definitions used by the parser.