1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-01 03:21:41 +00:00
OpenMW/components/compiler/literals.cpp

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

88 lines
2.1 KiB
C++
Raw Normal View History

#include "literals.hpp"
#include <algorithm>
namespace Compiler
{
int Literals::getIntegerSize() const
{
2021-05-02 06:43:44 +00:00
return static_cast<int>(mIntegers.size() * sizeof(Interpreter::Type_Integer));
}
int Literals::getFloatSize() const
{
2021-05-02 06:43:44 +00:00
return static_cast<int>(mFloats.size() * sizeof(Interpreter::Type_Float));
}
int Literals::getStringSize() const
{
int size = 0;
2022-09-22 18:26:05 +00:00
for (std::vector<std::string>::const_iterator iter(mStrings.begin()); iter != mStrings.end(); ++iter)
size += static_cast<int>(iter->size()) + 1;
2022-09-22 18:26:05 +00:00
if (size % 4) // padding
size += 4 - size % 4;
2022-09-22 18:26:05 +00:00
return size;
}
void Literals::append(std::vector<Interpreter::Type_Code>& code) const
{
for (const int& mInteger : mIntegers)
code.push_back(*reinterpret_cast<const Interpreter::Type_Code*>(&mInteger));
2022-09-22 18:26:05 +00:00
for (const float& mFloat : mFloats)
code.push_back(*reinterpret_cast<const Interpreter::Type_Code*>(&mFloat));
2022-09-22 18:26:05 +00:00
int stringBlockSize = getStringSize();
int size = static_cast<int>(code.size());
2022-09-22 18:26:05 +00:00
code.resize(size + stringBlockSize / 4);
2022-09-22 18:26:05 +00:00
2021-05-02 06:43:44 +00:00
size_t offset = 0;
2022-09-22 18:26:05 +00:00
for (const auto& mString : mStrings)
{
2021-05-02 06:43:44 +00:00
size_t stringSize = mString.size() + 1;
2022-09-22 18:26:05 +00:00
std::copy(mString.c_str(), mString.c_str() + stringSize, reinterpret_cast<char*>(&code[size]) + offset);
offset += stringSize;
}
}
int Literals::addInteger(Interpreter::Type_Integer value)
{
int index = static_cast<int>(mIntegers.size());
2022-09-22 18:26:05 +00:00
mIntegers.push_back(value);
2022-09-22 18:26:05 +00:00
return index;
}
int Literals::addFloat(Interpreter::Type_Float value)
{
int index = static_cast<int>(mFloats.size());
2022-09-22 18:26:05 +00:00
mFloats.push_back(value);
2022-09-22 18:26:05 +00:00
return index;
}
int Literals::addString(const std::string& value)
{
int index = static_cast<int>(mStrings.size());
2022-09-22 18:26:05 +00:00
mStrings.push_back(value);
2022-09-22 18:26:05 +00:00
return index;
}
void Literals::clear()
{
mIntegers.clear();
mFloats.clear();
mStrings.clear();
}
}