roza/lib/Program.cpp

78 lines
1.4 KiB
C++

#include "Program.hpp"
namespace roza
{
/*explicit*/ Program::Program()
{
}
/*virtual*/ Program::~Program()
{
}
void Program::push_instr(Opcode opcode, std::optional<param_t> param)
{
m_instrs.push_back({opcode, param});
}
size_t Program::push_value(std::shared_ptr<Value> value)
{
size_t sz = m_values.size();
m_values.push_back(value);
return sz;
}
Opcode Program::opcode(size_t index) const
{
assert(index < size());
return m_instrs[index].opcode;
}
std::optional<param_t> Program::param(size_t index) const
{
assert(index < size());
return m_instrs[index].param;
}
std::shared_ptr<Value> Program::value(size_t index) const
{
assert(has_value(index));
return m_values[index];
}
bool Program::has_value(size_t index) const
{
return index < m_values.size();
}
std::string Program::string() const
{
std::stringstream ss;
size_t addr = 0;
for (auto const& instr: m_instrs)
{
ss << addr++
<< "\t"
<< (OpcodeStr[instr.opcode])
<< "\t";
if (instr.param)
{
ss << std::to_string(*instr.param);
if (has_value(*instr.param))
{
ss << "\t" << "(" << value(*instr.param)->string() << ")";
}
}
ss << "\n";
}
return ss.str();
}
}