Print debug chunk if tracing is enabled
parent
252190f5e6
commit
bc99cbfc3b
|
@ -54,7 +54,7 @@ pub const Chunk = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prints the value of a single instruction
|
/// 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 the instruction offset
|
||||||
print("{d:0>4} ", .{offset});
|
print("{d:0>4} ", .{offset});
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,8 @@ const Chunk = chunk_mod.Chunk;
|
||||||
const OpCode = chunk_mod.OpCode;
|
const OpCode = chunk_mod.OpCode;
|
||||||
const print = std.debug.print;
|
const print = std.debug.print;
|
||||||
|
|
||||||
|
const tracing = @import("config").tracing;
|
||||||
|
|
||||||
const InterpretResult = enum {
|
const InterpretResult = enum {
|
||||||
Ok,
|
Ok,
|
||||||
CompileError,
|
CompileError,
|
||||||
|
@ -28,6 +30,12 @@ pub const VM = struct {
|
||||||
// Executes the instructions in the bytecode
|
// Executes the instructions in the bytecode
|
||||||
pub fn run(self: *VM) InterpretResult {
|
pub fn run(self: *VM) InterpretResult {
|
||||||
while (true) {
|
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];
|
const next = self.ip[0];
|
||||||
self.ip += 1;
|
self.ip += 1;
|
||||||
switch (next) {
|
switch (next) {
|
||||||
|
|
Loading…
Reference in New Issue