.
This commit is contained in:
39
src/vm/stack.cc
Normal file
39
src/vm/stack.cc
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "stack.hh"
|
||||
|
||||
#include "vm_exceptions.hh"
|
||||
|
||||
namespace tyche {
|
||||
|
||||
void Stack::push(Value const& value)
|
||||
{
|
||||
stack_.push_back(value);
|
||||
}
|
||||
|
||||
Value Stack::pop()
|
||||
{
|
||||
if (stack_.empty())
|
||||
throw VMStackUnderflow();
|
||||
|
||||
Value v = stack_.back();
|
||||
stack_.pop_back();
|
||||
return v;
|
||||
}
|
||||
|
||||
Value Stack::at(int pos) const
|
||||
{
|
||||
try {
|
||||
if (pos >= 0)
|
||||
return stack_.at(pos);
|
||||
else
|
||||
return stack_.at(stack_.size() + pos);
|
||||
} catch (std::out_of_range&) {
|
||||
throw VMStackOutOfRange();
|
||||
}
|
||||
}
|
||||
|
||||
size_t Stack::size() const
|
||||
{
|
||||
return stack_.size();
|
||||
}
|
||||
|
||||
} // tyche
|
||||
24
src/vm/stack.hh
Normal file
24
src/vm/stack.hh
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef TYCHE_STACK_HH
|
||||
#define TYCHE_STACK_HH
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "value.hh"
|
||||
|
||||
namespace tyche {
|
||||
|
||||
class Stack {
|
||||
public:
|
||||
void push(Value const& value);
|
||||
Value pop();
|
||||
|
||||
[[nodiscard]] Value at(int pos) const;
|
||||
[[nodiscard]] size_t size() const;
|
||||
|
||||
private:
|
||||
std::vector<Value> stack_;
|
||||
};
|
||||
|
||||
} // tyche
|
||||
|
||||
#endif //TYCHE_STACK_HH
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "../bytecode/bytearray.hh"
|
||||
#include "../bytecode/bytecode.hh"
|
||||
#include "code.hh"
|
||||
#include "stack.hh"
|
||||
|
||||
using namespace tyche;
|
||||
|
||||
@@ -28,6 +29,20 @@ TEST(Code, ImportSingleAndDebug)
|
||||
printf("%s\n", code.disassemble().c_str());
|
||||
}
|
||||
|
||||
TEST(Stack, PushPullGet)
|
||||
{
|
||||
Stack stack;
|
||||
stack.push(Value::CreateInteger(10));
|
||||
stack.push(Value::CreateInteger(20));
|
||||
stack.push(Value::CreateInteger(30));
|
||||
|
||||
ASSERT_EQ(stack.size(), 3);
|
||||
ASSERT_EQ(stack.at(0).as_integer(), 10);
|
||||
ASSERT_EQ(stack.at(1).as_integer(), 20);
|
||||
ASSERT_EQ(stack.at(-1).as_integer(), 30);
|
||||
ASSERT_EQ(stack.at(-2).as_integer(), 20);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
|
||||
29
src/vm/vm_exceptions.hh
Normal file
29
src/vm/vm_exceptions.hh
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef TYCHE_VM_EXCEPTIONS_HH
|
||||
#define TYCHE_VM_EXCEPTIONS_HH
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace tyche {
|
||||
|
||||
class VMRuntimeError : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
explicit VMRuntimeError(std::string const& str) : std::runtime_error(str.c_str()) {}
|
||||
};
|
||||
|
||||
class VMStackUnderflow : public VMRuntimeError
|
||||
{
|
||||
public:
|
||||
explicit VMStackUnderflow() : VMRuntimeError("Stack underflow") {}
|
||||
};
|
||||
|
||||
class VMStackOutOfRange : public VMRuntimeError
|
||||
{
|
||||
public:
|
||||
explicit VMStackOutOfRange() : VMRuntimeError("Item does not exist in stack") {}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //TYCHE_VM_EXCEPTIONS_HH
|
||||
Reference in New Issue
Block a user