1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-03 17:37:18 +00:00
OpenMW/components/interpreter/runtime.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

91 lines
2.1 KiB
C++
Raw Normal View History

2010-06-28 17:20:45 +00:00
#include "runtime.hpp"
#include "program.hpp"
2010-06-28 17:20:45 +00:00
2010-06-28 19:49:48 +00:00
#include <cassert>
#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
{
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)];
2010-06-28 19:49:48 +00:00
}
2010-06-28 19:49:48 +00:00
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)];
2010-06-28 19:49:48 +00:00
}
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)
{
2010-06-28 17:20:45 +00:00
clear();
mContext = &context;
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;
mProgram = nullptr;
2010-06-28 19:49:48 +00:00
mStack.clear();
2010-06-28 17:20:45 +00:00
}
void Runtime::push(const Data& data)
2010-06-28 19:49:48 +00:00
{
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);
}
2010-06-28 19:49:48 +00:00
void Runtime::pop()
{
if (mStack.empty())
throw std::runtime_error("stack underflow");
2022-01-28 14:26:43 +00:00
mStack.pop_back();
2010-06-28 19:49:48 +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");
2023-01-10 03:42:46 +00:00
return mStack[mStack.size() - index - 1];
2010-06-28 19:49:48 +00:00
}
2010-06-28 19:49:48 +00:00
Context& Runtime::getContext()
{
assert(mContext);
return *mContext;
2010-06-28 19:49:48 +00:00
}
2010-06-28 17:20:45 +00:00
}