chunk decoding

master
Araozu 2024-05-13 20:51:52 -05:00
parent d28a6730ab
commit 79a574f053
2 changed files with 32 additions and 3 deletions

View File

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const print = std.debug.print;
const OpCode = enum { pub const OpCode = enum(u8) {
OP_RETURN, OP_RETURN,
}; };
@ -38,7 +39,34 @@ pub const Chunk = struct {
self.code = try self.allocator.realloc(self.code, 0); self.code = try self.allocator.realloc(self.code, 0);
} }
pub fn dissasemble_chunk(self: *Chunk, name: []const u8) void {
print("== {s} ==\n", .{name});
var offset: usize = 0;
while (offset < self.count) {
offset = dissasemble_instruction(self, offset);
}
}
fn dissasemble_instruction(self: *Chunk, offset: usize) usize {
print("{d:0>4} ", .{offset});
const instruction = self.code[offset];
switch (instruction) {
@intFromEnum(OpCode.OP_RETURN) => return simple_instruction("OP_RETURN", offset),
else => {
print("unknown opcode {d}\n", .{instruction});
return offset + 1;
},
}
}
pub fn deinit(self: Chunk) void { pub fn deinit(self: Chunk) void {
self.allocator.free(self.code); self.allocator.free(self.code);
} }
}; };
fn simple_instruction(comptime name: []const u8, offset: usize) usize {
print("{s}\n", .{name});
return offset + 1;
}

View File

@ -11,8 +11,9 @@ pub fn main() !void {
var c = try chunk.Chunk.init(alloc); var c = try chunk.Chunk.init(alloc);
defer c.deinit(); defer c.deinit();
try c.write_chunck('a'); try c.write_chunck(@intFromEnum(chunk.OpCode.OP_RETURN));
try c.free_chunck(); try c.write_chunck(@intFromEnum(chunk.OpCode.OP_RETURN));
c.dissasemble_chunk("test chunk");
} }
test "chunk test" { test "chunk test" {