diff --git a/src/chunk.zig b/src/chunk.zig index 1c50b84..3e3bc2c 100644 --- a/src/chunk.zig +++ b/src/chunk.zig @@ -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}); diff --git a/src/vm.zig b/src/vm.zig index 452fb8d..f3d45c1 100644 --- a/src/vm.zig +++ b/src/vm.zig @@ -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) {