diff --git a/src/context.cpp b/src/context.cpp index 78c3c75..9b12fac 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -303,24 +303,36 @@ namespace plorth bool Context::Pop(Ref& 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& slot) { - if (!PeekArray(slot)) + Ref value; + + if (Pop(value, Value::TYPE_ARRAY)) { - return false; + slot = value.As(); + + return true; } - m_stack.pop_back(); - return true; + return false; } bool Context::PopBool(bool& slot) @@ -339,56 +351,71 @@ namespace plorth bool Context::PopError(Ref& slot) { - if (!PeekError(slot)) + Ref value; + + if (Pop(value, Value::TYPE_ERROR)) { - return false; + slot = value.As(); + + return true; } - m_stack.pop_back(); - return true; + return false; } bool Context::PopNumber(Ref& slot) { - if (!PeekNumber(slot)) + Ref value; + + if (Pop(value, Value::TYPE_NUMBER)) { - return false; + slot = value.As(); + + return true; } - m_stack.pop_back(); - return true; + return false; } bool Context::PopObject(Ref& slot) { - if (!PeekObject(slot)) + Ref value; + + if (Pop(value, Value::TYPE_OBJECT)) { - return false; + slot = value.As(); + + return true; } - m_stack.pop_back(); - return true; + return false; } bool Context::PopQuote(Ref& slot) { - if (!PeekQuote(slot)) + Ref value; + + if (Pop(value, Value::TYPE_QUOTE)) { - return false; + slot = value.As(); + + return true; } - m_stack.pop_back(); - return true; + return false; } bool Context::PopString(Ref& slot) { - if (!PeekString(slot)) + Ref value; + + if (Pop(value, Value::TYPE_STRING)) { - return false; + slot = value.As(); + + return true; } - m_stack.pop_back(); - return true; + return false; } }