.
This commit is contained in:
@@ -1,23 +1,44 @@
|
||||
#include "bytecode.hh"
|
||||
#include "../common/overloaded.hh"
|
||||
|
||||
namespace tyche {
|
||||
|
||||
Bytecode::Bytecode(BytecodePrototype const& bp)
|
||||
{
|
||||
// header
|
||||
byte_array_.add_uint32(0, MAGIC);
|
||||
byte_array_.add_byte(4, VERSION);
|
||||
|
||||
// constants
|
||||
std::vector<uint32_t> constant_indexes;
|
||||
std::vector<uint8_t> constant_array;
|
||||
std::vector<uint32_t> constant_table;
|
||||
ByteArray constant_array;
|
||||
uint32_t idx = 0;
|
||||
for (auto const& constant: bp.constants) {
|
||||
|
||||
constant_table.emplace_back(idx);
|
||||
std::visit(overloaded {
|
||||
[&](int32_t i) { constant_array.append_int(i); },
|
||||
[&](float f) { constant_array.append_float(f); },
|
||||
[&](std::string const& s) { constant_array.append_string(s); },
|
||||
}, constant);
|
||||
idx = constant_array.size();
|
||||
}
|
||||
|
||||
// constants table
|
||||
|
||||
// function table
|
||||
|
||||
//
|
||||
// build binary
|
||||
//
|
||||
|
||||
// header
|
||||
byte_array_.set_uint32(0, MAGIC);
|
||||
byte_array_.set_byte(4, VERSION);
|
||||
|
||||
// index - other entries created later
|
||||
byte_array_.set_uint32(HEADER_SZ, HEADER_SZ + INDEX_SZ);
|
||||
|
||||
// constants
|
||||
idx = HEADER_SZ + INDEX_SZ;
|
||||
for (auto const& const_idx: constant_table) {
|
||||
byte_array_.set_uint32(idx, const_idx);
|
||||
idx += 4;
|
||||
}
|
||||
byte_array_.append_bytearray(constant_array);
|
||||
}
|
||||
|
||||
uint32_t Bytecode::n_constants() const
|
||||
|
||||
Reference in New Issue
Block a user