improve disassemble

This commit is contained in:
Patrick MARIE 2024-09-01 13:39:15 +02:00
parent 980312bd62
commit 3ecbe5ca9d
3 changed files with 11 additions and 5 deletions

View File

@ -73,18 +73,18 @@ pub const Chunk = struct {
debug.print("== end of chunk dump \n\n", .{});
}
pub fn dissassemble(self: Chunk, name: []const u8) void {
pub fn disassemble(self: Chunk, name: []const u8) void {
debug.print("== {s} ==\n", .{name});
var offset: usize = 0;
while (offset < self.count) {
offset = self.dissassemble_instruction(offset);
offset = self.disassemble_instruction(offset);
}
debug.print("== end of {s} ==\n\n", .{name});
}
pub fn dissassemble_instruction(self: Chunk, offset: usize) usize {
pub fn disassemble_instruction(self: Chunk, offset: usize) usize {
var current_offset = offset;
debug.print("{d:0>4} ", .{offset});

View File

@ -158,8 +158,14 @@ pub const Parser = struct {
fn end_parser(self: *Parser) !*Obj.Function {
try self.emit_return();
const compiler_function = self.compiler.function;
if (!self.had_error and constants.DEBUG_PRINT_CODE) {
self.current_chunk().dissassemble("code");
var label: []const u8 = "<script>";
if (compiler_function.name != null) {
label = compiler_function.name.?.chars;
}
self.current_chunk().disassemble(label);
}
const function_obj = self.compiler.function;

View File

@ -156,7 +156,7 @@ pub const VM = struct {
}
debug.print("\n", .{});
}
_ = self.current_chunk().dissassemble_instruction(self.current_frame().ip);
_ = self.current_chunk().disassemble_instruction(self.current_frame().ip);
}
const instruction = self.read_byte();