-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytecode.cpp
More file actions
37 lines (30 loc) · 832 Bytes
/
bytecode.cpp
File metadata and controls
37 lines (30 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "bytecode.hpp"
#include <format>
#include <stdexcept>
#include <utility>
std::string Chunk::debug() {
std::string message;
size_t byteSize = bytes.size();
for (size_t offset = 0; offset < byteSize;) {
auto pair = _debugInstruction(offset);
message += pair.first;
offset = pair.second;
}
return message;
}
std::pair<std::string, size_t> Chunk::_debugInstruction(size_t offset) {
std::string message = std::format("{: 4} ", offset);
switch ((OpCode)bytes[offset]) {
case OpCode::RETURN:
message += "RETURN\n";
return {message, offset + 1};
}
throw std::runtime_error("Unreachable");
}
Chunk::Chunk(std::vector<uint8_t> bytes) : bytes(bytes) {}
VM::VM(std::vector<Chunk> chunks) : _chunks(chunks) {
for (Chunk &chunk : chunks) {
// TODO: implement
ignore(chunk);
}
}