.
This commit is contained in:
@@ -72,6 +72,9 @@ add_library(lib${PROJECT_NAME} SHARED
|
|||||||
src/vm/instruction.cc
|
src/vm/instruction.cc
|
||||||
src/vm/value.cc
|
src/vm/value.cc
|
||||||
src/vm/value.hh
|
src/vm/value.hh
|
||||||
|
src/vm/stack.cc
|
||||||
|
src/vm/stack.hh
|
||||||
|
src/vm/vm_exceptions.hh
|
||||||
)
|
)
|
||||||
|
|
||||||
target_compile_options(lib${PROJECT_NAME} PRIVATE ${warnings})
|
target_compile_options(lib${PROJECT_NAME} PRIVATE ${warnings})
|
||||||
|
|||||||
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/bytearray.hh"
|
||||||
#include "../bytecode/bytecode.hh"
|
#include "../bytecode/bytecode.hh"
|
||||||
#include "code.hh"
|
#include "code.hh"
|
||||||
|
#include "stack.hh"
|
||||||
|
|
||||||
using namespace tyche;
|
using namespace tyche;
|
||||||
|
|
||||||
@@ -28,6 +29,20 @@ TEST(Code, ImportSingleAndDebug)
|
|||||||
printf("%s\n", code.disassemble().c_str());
|
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)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
testing::InitGoogleTest(&argc, 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