Print debug chunk if tracing is enabled

master
Araozu 2024-05-27 21:59:00 -05:00
parent 252190f5e6
commit bc99cbfc3b
2 changed files with 9 additions and 1 deletions

View File

@ -54,7 +54,7 @@ pub const Chunk = struct {
}
/// Prints the value of a single instruction
fn dissasemble_instruction(self: *Chunk, offset: usize) usize {
pub fn dissasemble_instruction(self: *Chunk, offset: usize) usize {
// Print the instruction offset
print("{d:0>4} ", .{offset});

View File

@ -4,6 +4,8 @@ const Chunk = chunk_mod.Chunk;
const OpCode = chunk_mod.OpCode;
const print = std.debug.print;
const tracing = @import("config").tracing;
const InterpretResult = enum {
Ok,
CompileError,
@ -28,6 +30,12 @@ pub const VM = struct {
// Executes the instructions in the bytecode
pub fn run(self: *VM) InterpretResult {
while (true) {
if (tracing) {
// dissasemble & print the current instruction
const offset: usize = @intFromPtr(self.ip) - @intFromPtr(self.chunk.code.ptr);
_ = self.chunk.dissasemble_instruction(offset);
}
const next = self.ip[0];
self.ip += 1;
switch (next) {