implementing line information

This commit is contained in:
Patrick MARIE 2024-08-24 15:33:17 +02:00
parent 9e3ced0f2e
commit 6c8cf697f1
2 changed files with 19 additions and 7 deletions

View File

@ -13,6 +13,7 @@ const Chunk = struct {
count: usize, count: usize,
capacity: usize, capacity: usize,
code: []u8, code: []u8,
lines: []usize,
constants: ValueArray, constants: ValueArray,
pub fn new() Chunk { pub fn new() Chunk {
@ -20,6 +21,7 @@ const Chunk = struct {
.count = 0, .count = 0,
.capacity = 0, .capacity = 0,
.code = &.{}, .code = &.{},
.lines = &.{},
.constants = ValueArray.new(), .constants = ValueArray.new(),
}; };
} }
@ -30,17 +32,20 @@ const Chunk = struct {
self.count = 0; self.count = 0;
self.capacity = 0; self.capacity = 0;
self.code = &.{}; self.code = &.{};
self.lines = &.{};
self.constants = ValueArray.new(); self.constants = ValueArray.new();
} }
pub fn write(self: *Chunk, allocator: Allocator, byte: u8) !void { pub fn write(self: *Chunk, allocator: Allocator, byte: u8, line: usize) !void {
if (self.capacity < self.count + 1) { if (self.capacity < self.count + 1) {
const old_capacity = self.capacity; const old_capacity = self.capacity;
self.capacity = utils.grow_capacity(old_capacity); self.capacity = utils.grow_capacity(old_capacity);
self.code = try allocator.realloc(self.code, self.capacity); self.code = try allocator.realloc(self.code, self.capacity);
self.lines = try allocator.realloc(self.lines, self.capacity);
} }
self.code[self.count] = byte; self.code[self.count] = byte;
self.lines[self.count] = line;
self.count += 1; self.count += 1;
} }
@ -61,6 +66,12 @@ const Chunk = struct {
pub fn dissassemble_instruction(self: Chunk, offset: usize) usize { pub fn dissassemble_instruction(self: Chunk, offset: usize) usize {
debug.print("{x:0>4} ", .{offset}); debug.print("{x:0>4} ", .{offset});
if (offset > 0 and self.lines[offset] == self.lines[offset - 1]) {
debug.print(" | ", .{});
} else {
debug.print("{d:4} ", .{self.lines[offset]});
}
const instruction = self.code[offset]; const instruction = self.code[offset];
switch (instruction) { switch (instruction) {
@ -78,6 +89,7 @@ const Chunk = struct {
if (self.capacity > 0) { if (self.capacity > 0) {
allocator.free(self.code); allocator.free(self.code);
allocator.free(self.lines);
} }
} }
@ -95,9 +107,9 @@ pub fn simple_instruction(opcode_name: []const u8, offset: usize) usize {
pub fn constant_instruction(opcode_name: []const u8, chunk: Chunk, offset: usize) usize { pub fn constant_instruction(opcode_name: []const u8, chunk: Chunk, offset: usize) usize {
const constant = chunk.code[offset + 1]; const constant = chunk.code[offset + 1];
debug.print("{s:16} {d:4} ", .{ opcode_name, constant }); debug.print("{s:16} {d:4} '", .{ opcode_name, constant });
values.print_value(chunk.constants.values[constant]); values.print_value(chunk.constants.values[constant]);
debug.print("\n", .{}); debug.print("'\n", .{});
return offset + 2; return offset + 2;
} }
@ -109,10 +121,10 @@ pub fn main() !void {
var chunk = Chunk.new(); var chunk = Chunk.new();
try chunk.init(allocator); try chunk.init(allocator);
try chunk.write(allocator, @intFromEnum(OpCode.OP_RETURN)); try chunk.write(allocator, @intFromEnum(OpCode.OP_RETURN), 123);
const constant = try chunk.add_constant(allocator, 1.2); const constant = try chunk.add_constant(allocator, 1.2);
try chunk.write(allocator, @intFromEnum(OpCode.OP_CONSTANT)); try chunk.write(allocator, @intFromEnum(OpCode.OP_CONSTANT), 123);
try chunk.write(allocator, @intCast(constant)); try chunk.write(allocator, @intCast(constant), 123);
chunk.dissassemble("test chunk"); chunk.dissassemble("test chunk");

View File

@ -39,5 +39,5 @@ pub const ValueArray = struct {
}; };
pub fn print_value(value: Value) void { pub fn print_value(value: Value) void {
debug.print("{any}", .{value}); debug.print("{d}", .{value});
} }