Skip to content

Commit

Permalink
Fix pop methods
Browse files Browse the repository at this point in the history
Pop methods in execution context don't pop values from the stack if they
are not expected type.
  • Loading branch information
RauliL committed Apr 30, 2017
1 parent 8ec123e commit a7ba405
Showing 1 changed file with 52 additions and 25 deletions.
77 changes: 52 additions & 25 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,24 +303,36 @@ namespace plorth

bool Context::Pop(Ref<Value>& slot, Value::Type type)
{
if (!Peek(slot, type))
if (!Peek(slot))
{
return false;
}
m_stack.pop_back();
if (slot->GetType() != type)
{
std::stringstream ss;

ss << "Expected " << type << ", got " << slot->GetType() << " instead.";
SetError(Error::ERROR_CODE_TYPE, ss.str());

return false;
}

return true;
}

bool Context::PopArray(Ref<Array>& slot)
{
if (!PeekArray(slot))
Ref<Value> value;

if (Pop(value, Value::TYPE_ARRAY))
{
return false;
slot = value.As<Array>();

return true;
}
m_stack.pop_back();

return true;
return false;
}

bool Context::PopBool(bool& slot)
Expand All @@ -339,56 +351,71 @@ namespace plorth

bool Context::PopError(Ref<Error>& slot)
{
if (!PeekError(slot))
Ref<Value> value;

if (Pop(value, Value::TYPE_ERROR))
{
return false;
slot = value.As<Error>();

return true;
}
m_stack.pop_back();

return true;
return false;
}

bool Context::PopNumber(Ref<Number>& slot)
{
if (!PeekNumber(slot))
Ref<Value> value;

if (Pop(value, Value::TYPE_NUMBER))
{
return false;
slot = value.As<Number>();

return true;
}
m_stack.pop_back();

return true;
return false;
}

bool Context::PopObject(Ref<Object>& slot)
{
if (!PeekObject(slot))
Ref<Value> value;

if (Pop(value, Value::TYPE_OBJECT))
{
return false;
slot = value.As<Object>();

return true;
}
m_stack.pop_back();

return true;
return false;
}

bool Context::PopQuote(Ref<Quote>& slot)
{
if (!PeekQuote(slot))
Ref<Value> value;

if (Pop(value, Value::TYPE_QUOTE))
{
return false;
slot = value.As<Quote>();

return true;
}
m_stack.pop_back();

return true;
return false;
}

bool Context::PopString(Ref<String>& slot)
{
if (!PeekString(slot))
Ref<Value> value;

if (Pop(value, Value::TYPE_STRING))
{
return false;
slot = value.As<String>();

return true;
}
m_stack.pop_back();

return true;
return false;
}
}

0 comments on commit a7ba405

Please sign in to comment.