Skip to content

Commit

Permalink
fix for #131: save string buffer with bytecode
Browse files Browse the repository at this point in the history
  • Loading branch information
beltoforion committed Oct 14, 2023
1 parent f08a54a commit 310b56c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
20 changes: 20 additions & 0 deletions include/muParserBytecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,18 @@ namespace mu
/** \brief Token vector for storing the RPN. */
typedef std::vector<SToken> rpn_type;

/** \brief Type for a vector of strings. */
typedef std::vector<string_type> stringbuf_type;

/** \brief Position in the Calculation array. */
unsigned m_iStackPos;

/** \brief String variable storage. */
stringbuf_type m_stringBuffer;

/** \brief The expression associated with this bytecode. */
string_type m_expr;

/** \brief Maximum size needed for the stack. */
std::size_t m_iMaxStackSize;

Expand Down Expand Up @@ -142,6 +151,17 @@ namespace mu
return &m_vRPN[0];
}

void StoreEnvironment(string_type a_expr, stringbuf_type const& a_strBuf)
{
m_stringBuffer = a_strBuf;
m_expr = a_expr;
}

std::tuple<string_type, stringbuf_type> RestoreEnvironment() const
{
return std::make_tuple(m_expr, m_stringBuffer);
}

void AsciiDump() const;
};

Expand Down
2 changes: 1 addition & 1 deletion samples/example1/example1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ static void Calc()
parser.SetVarFactory(AddVariable, &parser);

// You can extract the bytecode of a parsed functions and save it for later use.
parser.SetExpr(_T("sin(a)"));
parser.SetExpr(_T("sin(a)+strfun2(sVar1, 1 , 2)"));
parser.Eval();
ParserByteCode bytecode1(parser.GetByteCode());

Expand Down
10 changes: 9 additions & 1 deletion src/muParserBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,13 @@ namespace mu
/** \brief Restore a previously saved bytecode. */
void ParserBase::SetByteCode(const ParserByteCode& a_ByteCode)
{
m_vRPN.Assign(a_ByteCode);
m_vRPN = a_ByteCode;

// restore expression environment
string_type expr;
std::tie(expr, m_vStringBuf) = a_ByteCode.RestoreEnvironment();
m_pTokenReader->SetFormula(expr);

m_pParseFormula = &ParserBase::ParseCmdCode;
}

Expand Down Expand Up @@ -1540,12 +1546,14 @@ namespace mu

if (m_vRPN.GetSize() == 2)
{
m_vRPN.StoreEnvironment(m_pTokenReader->GetExpr(), m_vStringBuf);
m_pParseFormula = &ParserBase::ParseCmdCodeShort;
m_vStackBuffer[1] = (this->*m_pParseFormula)();
return m_vStackBuffer[1];
}
else
{
m_vRPN.StoreEnvironment(m_pTokenReader->GetExpr(), m_vStringBuf);
m_pParseFormula = &ParserBase::ParseCmdCode;
return (this->*m_pParseFormula)();
}
Expand Down
9 changes: 7 additions & 2 deletions src/muParserBytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ namespace mu
{
/** \brief Bytecode default constructor. */
ParserByteCode::ParserByteCode()
:m_iStackPos(0)
: m_iStackPos(0)
, m_stringBuffer()
, m_expr()
, m_iMaxStackSize(0)
, m_vRPN()
, m_bEnableOptimizer(true)
Expand Down Expand Up @@ -98,6 +100,9 @@ namespace mu
m_vRPN = a_ByteCode.m_vRPN;
m_iMaxStackSize = a_ByteCode.m_iMaxStackSize;
m_bEnableOptimizer = a_ByteCode.m_bEnableOptimizer;

m_stringBuffer = a_ByteCode.m_stringBuffer;
m_expr = a_ByteCode.m_expr;
}


Expand Down Expand Up @@ -595,7 +600,7 @@ namespace mu
case cmFUNC_STR:
mu::console() << _T("CALL STRFUNC\t");
mu::console() << _T("[ARG:") << std::dec << m_vRPN[i].Fun.argc << _T("]");
mu::console() << _T("[IDX:") << std::dec << m_vRPN[i].Fun.idx << _T("]");
mu::console() << _T("[IDX:") << std::dec << m_vRPN[i].Fun.idx << _T("=\"") << m_stringBuffer[m_vRPN[i].Fun.idx] << ("\"]");
mu::console() << _T("[ADDR: 0x") << std::hex << reinterpret_cast<void*>(m_vRPN[i].Fun.cb._pRawFun) << _T("]");
mu::console() << _T("[USERDATA: 0x") << std::hex << reinterpret_cast<void*>(m_vRPN[i].Fun.cb._pUserData) << _T("]");
mu::console() << _T("\n");
Expand Down

0 comments on commit 310b56c

Please sign in to comment.