muParser API -  1.35
muParserBytecode.h
Go to the documentation of this file.
1 /*
2 
3  _____ __ _____________ _______ ______ ___________
4  / \| | \____ \__ \\_ __ \/ ___// __ \_ __ \
5  | Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
6  |__|_| /____/| __(____ /__| /____ >\___ >__|
7  \/ |__| \/ \/ \/
8  Copyright (C) 2004 - 2020 Ingo Berg
9 
10  Redistribution and use in source and binary forms, with or without modification, are permitted
11  provided that the following conditions are met:
12 
13  * Redistributions of source code must retain the above copyright notice, this list of
14  conditions and the following disclaimer.
15  * Redistributions in binary form must reproduce the above copyright notice, this list of
16  conditions and the following disclaimer in the documentation and/or other materials provided
17  with the distribution.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef MU_PARSER_BYTECODE_H
30 #define MU_PARSER_BYTECODE_H
31 
32 #include <string>
33 #include <stack>
34 #include <vector>
35 
36 #include "muParserDef.h"
37 #include "muParserError.h"
38 #include "muParserToken.h"
39 
40 /** \file
41  \brief Definition of the parser bytecode class.
42 */
43 
44 
45 namespace mu
46 {
47  struct SToken
48  {
49  ECmdCode Cmd;
50 
51  union
52  {
53  struct //SValData
54  {
55  value_type* ptr;
56  value_type data;
57  value_type data2;
58  } Val;
59 
60  struct //SFunData
61  {
62  // Note: generic_fun_type is merely a placeholder. The real type could be
63  // anything between gun_type1 and fun_type9. I can't use a void
64  // pointer due to constraints in the ANSI standard which allows
65  // data pointers and function pointers to differ in size.
66  generic_fun_type ptr;
67  int argc;
68  int idx;
69  } Fun;
70 
71  struct //SOprtData
72  {
73  value_type* ptr;
74  int offset;
75  } Oprt;
76  };
77  };
78 
79 
80  /** \brief Bytecode implementation of the Math Parser.
81 
82  The bytecode contains the formula converted to revers polish notation stored in a continious
83  memory area. Associated with this data are operator codes, variable pointers, constant
84  values and function pointers. Those are necessary in order to calculate the result.
85  All those data items will be casted to the underlying datatype of the bytecode.
86  */
87  class ParserByteCode final
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 
132  std::size_t GetSize() const
133  {
134  return m_vRPN.size();
135  }
136 
137  inline const SToken* GetBase() const
138  {
139  if (m_vRPN.size() == 0)
141  else
142  return &m_vRPN[0];
143  }
144 
145  void AsciiDump();
146  };
147 
148 } // namespace mu
149 
150 #endif
151 
152 
void AddStrFun(generic_fun_type a_pFun, int a_iArgc, int a_iIdx)
Add Strung function entry to the parser bytecode.
Internal error of any kind.
Definition: muParserDef.h:279
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.
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.
Error class of the parser.
Definition: muParserError.h:68
ECmdCode
Bytecode values.
Definition: muParserDef.h:135
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:294
Namespace for mathematical applications.
Definition: muParser.cpp:46
value_type(* generic_fun_type)()
Callback type used for functions without arguments.
Definition: muParserDef.h:325
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.