From bc99cbfc3b656fedf1cd77e5dc5d65dcdeea3918 Mon Sep 17 00:00:00 2001 From: Araozu Date: Mon, 27 May 2024 21:59:00 -0500 Subject: [PATCH] Print debug chunk if tracing is enabled --- src/chunk.zig | 2 +- src/vm.zig | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) 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) {