2010-06-28 17:20:45 +00:00
|
|
|
#include "runtime.hpp"
|
2023-01-10 03:10:18 +00:00
|
|
|
#include "program.hpp"
|
2010-06-28 17:20:45 +00:00
|
|
|
|
2010-06-28 19:49:48 +00:00
|
|
|
#include <cassert>
|
2010-06-30 10:04:26 +00:00
|
|
|
#include <cstring>
|
2010-06-28 19:49:48 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2010-06-28 17:20:45 +00:00
|
|
|
namespace Interpreter
|
|
|
|
{
|
2010-06-28 19:49:48 +00:00
|
|
|
int Runtime::getIntegerLiteral(int index) const
|
|
|
|
{
|
2023-01-10 03:10:18 +00:00
|
|
|
if (index < 0 || mProgram->mIntegers.size() <= static_cast<std::size_t>(index))
|
|
|
|
throw std::out_of_range("Invalid integer index");
|
2011-05-18 14:01:19 +00:00
|
|
|
|
2023-01-10 03:10:18 +00:00
|
|
|
return mProgram->mIntegers[static_cast<std::size_t>(index)];
|
2010-06-28 19:49:48 +00:00
|
|
|
}
|
2011-05-18 14:01:19 +00:00
|
|
|
|
2010-06-28 19:49:48 +00:00
|
|
|
float Runtime::getFloatLiteral(int index) const
|
|
|
|
{
|
2023-01-10 03:10:18 +00:00
|
|
|
if (index < 0 || mProgram->mFloats.size() <= static_cast<std::size_t>(index))
|
|
|
|
throw std::out_of_range("Invalid float index");
|
2011-05-18 14:01:19 +00:00
|
|
|
|
2023-01-10 03:10:18 +00:00
|
|
|
return mProgram->mFloats[static_cast<std::size_t>(index)];
|
2010-06-28 19:49:48 +00:00
|
|
|
}
|
2011-05-18 14:01:19 +00:00
|
|
|
|
2022-05-20 23:21:55 +00:00
|
|
|
std::string_view Runtime::getStringLiteral(int index) const
|
2010-06-30 10:04:26 +00:00
|
|
|
{
|
2023-01-10 03:10:18 +00:00
|
|
|
if (index < 0 || mProgram->mStrings.size() <= static_cast<std::size_t>(index))
|
|
|
|
throw std::out_of_range("Invalid string literal index");
|
2011-05-18 14:01:19 +00:00
|
|
|
|
2023-01-10 03:10:18 +00:00
|
|
|
return mProgram->mStrings[static_cast<std::size_t>(index)];
|
2010-06-30 10:04:26 +00:00
|
|
|
}
|
2011-05-18 14:01:19 +00:00
|
|
|
|
2023-01-10 03:10:18 +00:00
|
|
|
void Runtime::configure(const Program& program, Context& context)
|
2011-05-18 14:01:19 +00:00
|
|
|
{
|
2010-06-28 17:20:45 +00:00
|
|
|
clear();
|
2011-05-18 14:01:19 +00:00
|
|
|
|
|
|
|
mContext = &context;
|
2023-01-10 03:10:18 +00:00
|
|
|
mProgram = &program;
|
2010-06-28 18:46:15 +00:00
|
|
|
mPC = 0;
|
2010-06-28 17:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Runtime::clear()
|
|
|
|
{
|
2020-11-13 07:39:47 +00:00
|
|
|
mContext = nullptr;
|
2023-01-10 03:10:18 +00:00
|
|
|
mProgram = nullptr;
|
2010-06-28 19:49:48 +00:00
|
|
|
mStack.clear();
|
2010-06-28 17:20:45 +00:00
|
|
|
}
|
2011-05-18 14:01:19 +00:00
|
|
|
|
2010-07-14 13:28:55 +00:00
|
|
|
void Runtime::push(const Data& data)
|
2010-06-28 19:49:48 +00:00
|
|
|
{
|
|
|
|
mStack.push_back(data);
|
|
|
|
}
|
2011-05-18 14:01:19 +00:00
|
|
|
|
2010-07-14 13:28:55 +00:00
|
|
|
void Runtime::push(Type_Integer value)
|
|
|
|
{
|
|
|
|
Data data;
|
|
|
|
data.mInteger = value;
|
|
|
|
push(data);
|
|
|
|
}
|
2011-05-18 14:01:19 +00:00
|
|
|
|
2010-07-14 13:28:55 +00:00
|
|
|
void Runtime::push(Type_Float value)
|
|
|
|
{
|
|
|
|
Data data;
|
|
|
|
data.mFloat = value;
|
|
|
|
push(data);
|
|
|
|
}
|
2011-05-18 14:01:19 +00:00
|
|
|
|
2010-06-28 19:49:48 +00:00
|
|
|
void Runtime::pop()
|
|
|
|
{
|
|
|
|
if (mStack.empty())
|
|
|
|
throw std::runtime_error("stack underflow");
|
2011-05-18 14:01:19 +00:00
|
|
|
|
2022-01-28 14:26:43 +00:00
|
|
|
mStack.pop_back();
|
2010-06-28 19:49:48 +00:00
|
|
|
}
|
2011-05-18 14:01:19 +00:00
|
|
|
|
2023-01-10 03:42:46 +00:00
|
|
|
Data& Runtime::operator[](int index)
|
2010-06-28 19:49:48 +00:00
|
|
|
{
|
2023-01-10 03:42:46 +00:00
|
|
|
if (index < 0 || index >= static_cast<int>(mStack.size()))
|
2010-06-28 19:49:48 +00:00
|
|
|
throw std::runtime_error("stack index out of range");
|
2011-05-18 14:01:19 +00:00
|
|
|
|
2023-01-10 03:42:46 +00:00
|
|
|
return mStack[mStack.size() - index - 1];
|
2010-06-28 19:49:48 +00:00
|
|
|
}
|
2011-05-18 14:01:19 +00:00
|
|
|
|
2010-06-28 19:49:48 +00:00
|
|
|
Context& Runtime::getContext()
|
|
|
|
{
|
2011-05-18 14:01:19 +00:00
|
|
|
assert(mContext);
|
|
|
|
return *mContext;
|
2010-06-28 19:49:48 +00:00
|
|
|
}
|
2010-06-28 17:20:45 +00:00
|
|
|
}
|