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

69 lines
1.2 KiB
C++

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