3 _____ __ _____________ _______ ______ ___________
4 / \| | \____ \__ \\_ __ \/ ___// __ \_ __ \
5 | Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
6 |__|_| /____/| __(____ /__| /____ >\___ >__|
8 Copyright (C) 2004 - 2020 Ingo Berg
10 Redistribution and use in source and binary forms, with or without modification, are permitted
11 provided that the following conditions are met:
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.
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.
29 #ifndef MU_PARSER_BYTECODE_H
30 #define MU_PARSER_BYTECODE_H
36 #include "muParserDef.h"
37 #include "muParserError.h"
38 #include "muParserToken.h"
41 \brief Definition of the parser bytecode class.
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.
80 /** \brief Bytecode implementation of the Math Parser.
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.
87 class ParserByteCode final
91 /** \brief Token type for internal use only. */
92 typedef ParserToken<value_type, string_type> token_type;
94 /** \brief Token vector for storing the RPN. */
95 typedef std::vector<SToken> rpn_type;
97 /** \brief Position in the Calculation array. */
100 /** \brief Maximum size needed for the stack. */
101 std::size_t m_iMaxStackSize;
103 /** \brief The actual rpn storage. */
106 bool m_bEnableOptimizer;
108 void ConstantFolding(ECmdCode a_Oprt);
113 ParserByteCode(const ParserByteCode& a_ByteCode);
114 ParserByteCode& operator=(const ParserByteCode& a_ByteCode);
115 void Assign(const ParserByteCode& a_ByteCode);
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);
126 void EnableOptimizer(bool bStat);
130 std::size_t GetMaxStackSize() const;
132 std::size_t GetSize() const
134 return m_vRPN.size();
137 inline const SToken* GetBase() const
139 if (m_vRPN.size() == 0)
140 throw ParserError(ecINTERNAL_ERROR);