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

62 lines
1.1 KiB
C++
Raw Normal View History

2023-09-09 22:03:28 +00:00
#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 });
}
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)
{
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];
}
}