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 - 2022 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: the type is erased in generic_callable_type and the signature of the
63  // function to call is tracked elsewhere in regard with the number of
64  // parameters (args) and the general kind of function (Cmd: cmFUNC,
65  // cmFUNC_STR, or cmFUNC_BULK)
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 API_EXPORT_CXX 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 Type for a vector of strings. */
98  typedef std::vector<string_type> stringbuf_type;
99 
100  /** \brief Position in the Calculation array. */
101  unsigned m_iStackPos;
102 
103  /** \brief String variable storage. */
104  stringbuf_type m_stringBuffer;
105 
106  /** \brief The expression associated with this bytecode. */
107  string_type m_expr;
108 
109  /** \brief Maximum size needed for the stack. */
110  std::size_t m_iMaxStackSize;
111 
112  /** \brief The actual rpn storage. */
113  rpn_type m_vRPN;
114 
115  bool m_bEnableOptimizer;
116 
117  void ConstantFolding(ECmdCode a_Oprt);
118 
119  public:
120 
121  ParserByteCode();
122  ParserByteCode(const ParserByteCode& a_ByteCode);
123  ParserByteCode& operator=(const ParserByteCode& a_ByteCode);
124  void Assign(const ParserByteCode& a_ByteCode);
125 
126  void AddVar(value_type* a_pVar);
127  void AddVal(value_type a_fVal);
128  void AddOp(ECmdCode a_Oprt);
129  void AddIfElse(ECmdCode a_Oprt);
130  void AddAssignOp(value_type* a_pVar);
131  void AddFun(generic_callable_type a_pFun, int a_iArgc, bool isOptimizable);
132  void AddBulkFun(generic_callable_type a_pFun, int a_iArgc);
133  void AddStrFun(generic_callable_type a_pFun, int a_iArgc, int a_iIdx);
134 
135  void EnableOptimizer(bool bStat);
136 
137  void Finalize();
138  void clear();
139  std::size_t GetMaxStackSize() const;
140 
141  std::size_t GetSize() const
142  {
143  return m_vRPN.size();
144  }
145 
146  inline const SToken* GetBase() const
147  {
148  if (m_vRPN.size() == 0)
150  else
151  return &m_vRPN[0];
152  }
153 
154  void StoreEnvironment(string_type expr, stringbuf_type const& strBuf)
155  {
156  m_stringBuffer = strBuf;
157  m_expr = expr;
158  }
159 
160  std::tuple<string_type, stringbuf_type> RestoreEnvironment() const
161  {
162  return std::make_tuple(m_expr, m_stringBuffer);
163  }
164 
165  void AsciiDump() const;
166  };
167 
168 } // namespace mu
169 
170 #endif
171 
172 
Internal error of any kind.
Definition: muParserDef.h:279
Error class of the parser.
Definition: muParserError.h:74
ECmdCode
Bytecode values.
Definition: muParserDef.h:135
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:296
Namespace for mathematical applications.
Definition: muParser.cpp:48
Bytecode implementation of the Math Parser.
MUP_STRING_TYPE string_type
The stringtype used by the parser.
Definition: muParserDef.h:302
This file contains the parser token definition.
This file defines the error class used by the parser.
This file contains standard definitions used by the parser.