Line information

master
Araozu 2024-05-14 09:22:58 -05:00
parent 975280be8f
commit 0f891e4275
2 changed files with 24 additions and 19 deletions

View File

@ -10,6 +10,7 @@ pub const OpCode = enum(u8) {
pub const Chunk = struct {
code: []u8,
lines: []u32,
count: usize,
capacity: usize,
constants: ValueArray,
@ -22,30 +23,26 @@ pub const Chunk = struct {
.capacity = 0,
.allocator = allocator,
.code = try allocator.alloc(u8, 0),
.lines = try allocator.alloc(u32, 0),
.constants = try ValueArray.init(allocator),
};
}
/// Writes a byte to this chunk
pub fn write(self: *Chunk, byte: u8) !void {
pub fn write(self: *Chunk, byte: u8, line: u32) !void {
// If the code slice is full
if (self.count == self.capacity) {
const old_capacity = self.capacity;
self.capacity = grow_capacity(old_capacity);
self.code = try self.allocator.realloc(self.code, self.capacity);
self.lines = try self.allocator.realloc(self.lines, self.capacity);
}
self.code[self.count] = byte;
self.lines[self.count] = line;
self.count += 1;
}
/// Reinitializes the state of the chunk.
pub fn free_chunck(self: *Chunk) !void {
self.count = 0;
self.capacity = 0;
self.code = try self.allocator.realloc(self.code, 0);
}
/// Prints the current state of the chunk to stderr
pub fn dissasemble_chunk(self: *Chunk, name: []const u8) void {
print("== {s} ==\n", .{name});
@ -60,6 +57,13 @@ pub const Chunk = struct {
fn dissasemble_instruction(self: *Chunk, offset: usize) usize {
print("{d:0>4} ", .{offset});
// Print the line number
if (offset > 0 and self.lines[offset] == self.lines[offset - 1]) {
print(" | ", .{});
} else {
print("{d: >4} ", .{self.lines[offset]});
}
const instruction = self.code[offset];
switch (instruction) {
@intFromEnum(OpCode.OP_RETURN) => return simple_instruction("OP_RETURN", offset),
@ -78,15 +82,17 @@ pub const Chunk = struct {
/// Destroys this chunk
pub fn deinit(self: Chunk) void {
self.allocator.free(self.code);
self.allocator.free(self.lines);
self.constants.deinit();
}
fn constant_instruction(self: *Chunk, comptime name: []const u8, offset: usize) usize {
const constant_addr = self.code[offset + 1];
const constant_value = self.constants.values[constant_addr];
print("{s} ", .{name});
print("{s: <16} {d: >4} '", .{ name, constant_addr });
print_value(constant_value);
print("\n", .{});
print("'\n", .{});
return offset + 2;
}
};

View File

@ -12,11 +12,11 @@ pub fn main() !void {
var c = try chunk.Chunk.init(alloc);
defer c.deinit();
try c.write(@intFromEnum(chunk.OpCode.OP_RETURN));
const constant_idx = try c.add_constant(1.2);
try c.write(@intFromEnum(OpCode.OP_CONSTANT));
try c.write(@truncate(constant_idx));
try c.write(@intFromEnum(OpCode.OP_CONSTANT), 1);
try c.write(@truncate(constant_idx), 1);
try c.write(@intFromEnum(chunk.OpCode.OP_RETURN), 1);
c.dissasemble_chunk("test chunk");
}
@ -25,9 +25,8 @@ test "chunk test" {
var c = try chunk.Chunk.init(std.testing.allocator);
defer c.deinit();
try c.write('a');
try c.write('b');
try c.free_chunck();
try c.write('J');
try c.write('H');
try c.write('a', 0);
try c.write('b', 1);
try c.write('J', 2);
try c.write('H', 3);
}