This repository has been archived on 2023-09-09. You can view files and clone it, but cannot push or open issues/pull-requests.
skemla/lib/Program.cpp

75 lines
1.3 KiB
C++

#include "Program.hpp"
#include "Value.hpp"
namespace sk
{
/*explicit*/ Program::Program()
{
}
/*virtual*/ Program::~Program()
{
}
std::string Program::string() const
{
std::stringstream ss;
size_t i = 0;
for (auto const& instr: m_instrs)
{
ss << i << "\t";
ss << std::string(OpcodeTypeStr[instr.opcode])
.substr(std::string("OPCODE_").size());
if (instr.param)
{
ss << "\t" << *instr.param;
}
ss << "\n";
i++;
}
return ss.str();
}
void Program::push_instr(OpcodeType opcode,
std::optional<param_t> param)
{
m_instrs.push_back({opcode, param});
}
Instr const& Program::instr(size_t index) const
{
assert(index < size());
return m_instrs[index];
}
Instr& Program::instr(size_t index)
{
assert(index < size());
return m_instrs[index];
}
size_t Program::push_value(std::shared_ptr<Value> value)
{
for (size_t i=0; i<m_values.size(); i++)
{
if (value->equals(*m_values[i]))
{
return i;
}
}
m_values.push_back(value);
return m_values.size() - 1;
}
std::shared_ptr<Value> Program::value(size_t index)
{
assert(index < m_values.size());
return m_values[index];
}
}