2017-08-22 22:34:41 +00:00
|
|
|
/*
|
2017-11-18 21:49:52 +00:00
|
|
|
__________
|
|
|
|
_____ __ __\______ \_____ _______ ______ ____ _______
|
2017-08-22 22:34:41 +00:00
|
|
|
/ \ | | \| ___/\__ \ \_ __ \/ ___/_/ __ \\_ __ \
|
|
|
|
| Y Y \| | /| | / __ \_| | \/\___ \ \ ___/ | | \/
|
2017-11-18 21:49:52 +00:00
|
|
|
|__|_| /|____/ |____| (____ /|__| /____ > \___ >|__|
|
|
|
|
\/ \/ \/ \/
|
2017-08-22 22:34:41 +00:00
|
|
|
Copyright (C) 2004-2013 Ingo Berg
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
2017-08-22 22:34:41 +00:00
|
|
|
software and associated documentation files (the "Software"), to deal in the Software
|
2017-11-18 21:49:52 +00:00
|
|
|
without restriction, including without limitation the rights to use, copy, modify,
|
|
|
|
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
2017-08-22 22:34:41 +00:00
|
|
|
permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
The above copyright notice and this permission notice shall be included in all copies or
|
2017-08-22 22:34:41 +00:00
|
|
|
substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
2017-11-18 21:49:52 +00:00
|
|
|
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2017-08-22 22:34:41 +00:00
|
|
|
*/
|
|
|
|
#ifndef MU_PARSER_BYTECODE_H
|
|
|
|
#define MU_PARSER_BYTECODE_H
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <stack>
|
2017-11-18 21:49:52 +00:00
|
|
|
#include <string>
|
2017-08-22 22:34:41 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "muParserDef.h"
|
|
|
|
#include "muParserToken.h"
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
\brief Definition of the parser bytecode class.
|
|
|
|
*/
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
namespace mu {
|
|
|
|
struct SToken {
|
2017-08-22 22:34:41 +00:00
|
|
|
ECmdCode Cmd;
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
union {
|
|
|
|
struct // SValData
|
|
|
|
{
|
|
|
|
value_type *ptr;
|
|
|
|
value_type data;
|
|
|
|
} Val;
|
|
|
|
|
|
|
|
struct // SFunData
|
|
|
|
{
|
|
|
|
// Note: generic_fun_type is merely a placeholder. The real type could be
|
|
|
|
// anything between gun_type1 and fun_type9. I can't use a void
|
|
|
|
// pointer due to constraints in the ANSI standard which allows
|
|
|
|
// data pointers and function pointers to differ in size.
|
|
|
|
generic_fun_type ptr;
|
|
|
|
int argc;
|
|
|
|
int idx;
|
|
|
|
} Fun;
|
|
|
|
|
|
|
|
struct // SOprtData
|
|
|
|
{
|
|
|
|
value_type *ptr;
|
|
|
|
int offset;
|
|
|
|
} Oprt;
|
2017-08-22 22:34:41 +00:00
|
|
|
};
|
2017-11-18 21:49:52 +00:00
|
|
|
};
|
2017-08-22 22:34:41 +00:00
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
/** \brief Bytecode implementation of the Math Parser.
|
2017-08-22 22:34:41 +00:00
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
The bytecode contains the formula converted to revers polish notation stored in a continious
|
|
|
|
memory area. Associated with this data are operator codes, variable pointers, constant
|
|
|
|
values and function pointers. Those are necessary in order to calculate the result.
|
|
|
|
All those data items will be casted to the underlying datatype of the bytecode.
|
2017-08-22 22:34:41 +00:00
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
\author (C) 2004-2013 Ingo Berg
|
|
|
|
*/
|
|
|
|
class ParserByteCode {
|
|
|
|
private:
|
2017-08-22 22:34:41 +00:00
|
|
|
/** \brief Token type for internal use only. */
|
|
|
|
typedef ParserToken<value_type, string_type> token_type;
|
|
|
|
|
|
|
|
/** \brief Position in the Calculation array. */
|
|
|
|
unsigned m_iStackPos;
|
|
|
|
|
|
|
|
/** \brief Maximum size needed for the stack. */
|
|
|
|
std::size_t m_iMaxStackSize;
|
2017-11-18 21:49:52 +00:00
|
|
|
|
2017-08-22 22:34:41 +00:00
|
|
|
/** \brief The actual rpn storage. */
|
2017-12-18 18:33:59 +00:00
|
|
|
std::vector<SToken> m_vRPN;
|
2017-08-22 22:34:41 +00:00
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
public:
|
2017-08-22 22:34:41 +00:00
|
|
|
ParserByteCode();
|
2017-12-18 19:18:20 +00:00
|
|
|
ParserByteCode(const ParserByteCode &a_ByteCode) = delete;
|
|
|
|
ParserByteCode &operator=(const ParserByteCode &a_ByteCode) = delete;
|
2017-08-22 22:34:41 +00:00
|
|
|
|
|
|
|
void AddVar(value_type *a_pVar);
|
|
|
|
void AddVal(value_type a_fVal);
|
|
|
|
void AddOp(ECmdCode a_Oprt);
|
|
|
|
void AddIfElse(ECmdCode a_Oprt);
|
|
|
|
void AddAssignOp(value_type *a_pVar);
|
|
|
|
void AddFun(generic_fun_type a_pFun, int a_iArgc);
|
|
|
|
void AddStrFun(generic_fun_type a_pFun, int a_iArgc, int a_iIdx);
|
|
|
|
|
|
|
|
void Finalize();
|
|
|
|
void clear();
|
|
|
|
std::size_t GetMaxStackSize() const;
|
|
|
|
std::size_t GetSize() const;
|
2017-12-18 17:30:18 +00:00
|
|
|
bool empty() const { return GetSize() == 0; }
|
2017-08-22 22:34:41 +00:00
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
const SToken *GetBase() const;
|
2017-08-22 22:34:41 +00:00
|
|
|
void AsciiDump();
|
|
|
|
};
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
} // namespace mu
|
2017-08-22 22:34:41 +00:00
|
|
|
|
|
|
|
#endif
|