1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include "runtime.hpp"
#include "program.hpp"
#include <cassert>
#include <cstring>
#include <stdexcept>
namespace Interpreter
{
int Runtime::getIntegerLiteral(int index) const
{
if (index < 0 || mProgram->mIntegers.size() <= static_cast<std::size_t>(index))
throw std::out_of_range("Invalid integer index");
return mProgram->mIntegers[static_cast<std::size_t>(index)];
}
float Runtime::getFloatLiteral(int index) const
{
if (index < 0 || mProgram->mFloats.size() <= static_cast<std::size_t>(index))
throw std::out_of_range("Invalid float index");
return mProgram->mFloats[static_cast<std::size_t>(index)];
}
std::string_view Runtime::getStringLiteral(int index) const
{
if (index < 0 || mProgram->mStrings.size() <= static_cast<std::size_t>(index))
throw std::out_of_range("Invalid string literal index");
return mProgram->mStrings[static_cast<std::size_t>(index)];
}
void Runtime::configure(const Program& program, Context& context)
{
clear();
mContext = &context;
mProgram = &program;
mPC = 0;
}
void Runtime::clear()
{
mContext = nullptr;
mProgram = nullptr;
mStack.clear();
}
void Runtime::push(const Data& data)
{
mStack.push_back(data);
}
void Runtime::push(Type_Integer value)
{
Data data;
data.mInteger = value;
push(data);
}
void Runtime::push(Type_Float value)
{
Data data;
data.mFloat = value;
push(data);
}
void Runtime::pop()
{
if (mStack.empty())
throw std::runtime_error("stack underflow");
mStack.pop_back();
}
Data& Runtime::operator[](int index)
{
if (index < 0 || index >= static_cast<int>(mStack.size()))
throw std::runtime_error("stack index out of range");
return mStack[mStack.size() - index - 1];
}
Context& Runtime::getContext()
{
assert(mContext);
return *mContext;
}
}
|