Skip to content

Commit

Permalink
fix for #93
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingo Berg committed Jun 7, 2021
1 parent 6d8c4b8 commit 39ddf1f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
2 changes: 1 addition & 1 deletion include/muParserBytecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ namespace mu
void AddOp(ECmdCode a_Oprt);
void AddIfElse(ECmdCode a_Oprt);
void AddAssignOp(value_type* a_pVar);
void AddFun(generic_callable_type a_pFun, int a_iArgc);
void AddFun(generic_callable_type a_pFun, int a_iArgc, bool isOptimizable);
void AddBulkFun(generic_callable_type a_pFun, int a_iArgc);
void AddStrFun(generic_callable_type a_pFun, int a_iArgc, int a_iIdx);

Expand Down
8 changes: 8 additions & 0 deletions include/muParserToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,14 @@ namespace mu
return m_pCallback->GetArgc();
}

//------------------------------------------------------------------------------
/** \brief Return true if the token is a function token that can be optimized.
*/
bool IsOptimizable() const
{
return m_pCallback->IsValid() && m_pCallback->IsOptimizable();
}

//------------------------------------------------------------------------------
/** \brief Return the token identifier.
Expand Down
2 changes: 1 addition & 1 deletion src/muParserBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ namespace mu
if (funTok.GetArgCount() == -1 && iArgCount == 0)
Error(ecTOO_FEW_PARAMS, m_pTokenReader->GetPos(), funTok.GetAsString());

m_vRPN.AddFun(funTok.GetFuncAddr(), (funTok.GetArgCount() == -1) ? -iArgNumerical : iArgNumerical);
m_vRPN.AddFun(funTok.GetFuncAddr(), (funTok.GetArgCount() == -1) ? -iArgNumerical : iArgNumerical, funTok.IsOptimizable());
break;
default:
break;
Expand Down
4 changes: 2 additions & 2 deletions src/muParserBytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,13 @@ namespace mu
\param a_iArgc Number of arguments, negative numbers indicate multiarg functions.
\param a_pFun Pointer to function callback.
*/
void ParserByteCode::AddFun(generic_callable_type a_pFun, int a_iArgc)
void ParserByteCode::AddFun(generic_callable_type a_pFun, int a_iArgc, bool isFunctionOptimizable)
{
std::size_t sz = m_vRPN.size();
bool optimize = false;

// only optimize functions with fixed number of more than a single arguments
if (m_bEnableOptimizer && a_iArgc > 0)
if (isFunctionOptimizable && m_bEnableOptimizer && a_iArgc > 0)
{
// <ibg 2020-06-10/> Unary Plus is a no-op
if (a_pFun == generic_callable_type{(erased_fun_type)&MathImpl<value_type>::UnaryPlus, nullptr})
Expand Down
37 changes: 27 additions & 10 deletions src/muParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,36 @@ namespace mu
Parser p;
try
{
p.DefineFun(_T("unoptimizable"), f1of1, false);
p.SetExpr(_T("unoptimizable(1)"));
p.Eval();

// test for #93 (https://github.com/beltoforion/muparser/issues/93)
// expected bytecode is:
// FUN, VAL, END
auto& bc = p.GetByteCode();
const SToken* tok = bc.GetBase();
if (tok->Cmd != cmFUNC)
// VAL, FUN
{
p.DefineFun(_T("unoptimizable"), f1of1, false);
p.SetExpr(_T("unoptimizable(1)"));
p.Eval();

auto& bc = p.GetByteCode();
const SToken* tok = bc.GetBase();
if (bc.GetSize() != 2 && tok[1].Cmd != cmFUNC)
{
mu::console() << _T("#93 an unoptimizable expression was optimized!") << endl;
iStat += 1;
}
}

{
mu::console() << _T("#93 an unoptimizable expression was optimized!") << endl;
iStat += 1;
p.ClearFun();
p.DefineFun(_T("unoptimizable"), f1of1, true);
p.SetExpr(_T("unoptimizable(1)"));
p.Eval();

auto& bc = p.GetByteCode();
const SToken* tok = bc.GetBase();
if (bc.GetSize() != 1 && tok[0].Cmd != cmVAL)
{
mu::console() << _T("#93 optimizer error") << endl;
iStat += 1;
}
}
}
catch (...)
Expand Down

0 comments on commit 39ddf1f

Please sign in to comment.