Line information
parent
975280be8f
commit
0f891e4275
|
@ -10,6 +10,7 @@ pub const OpCode = enum(u8) {
|
||||||
|
|
||||||
pub const Chunk = struct {
|
pub const Chunk = struct {
|
||||||
code: []u8,
|
code: []u8,
|
||||||
|
lines: []u32,
|
||||||
count: usize,
|
count: usize,
|
||||||
capacity: usize,
|
capacity: usize,
|
||||||
constants: ValueArray,
|
constants: ValueArray,
|
||||||
|
@ -22,30 +23,26 @@ pub const Chunk = struct {
|
||||||
.capacity = 0,
|
.capacity = 0,
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.code = try allocator.alloc(u8, 0),
|
.code = try allocator.alloc(u8, 0),
|
||||||
|
.lines = try allocator.alloc(u32, 0),
|
||||||
.constants = try ValueArray.init(allocator),
|
.constants = try ValueArray.init(allocator),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes a byte to this chunk
|
/// 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 the code slice is full
|
||||||
if (self.count == self.capacity) {
|
if (self.count == self.capacity) {
|
||||||
const old_capacity = self.capacity;
|
const old_capacity = self.capacity;
|
||||||
self.capacity = grow_capacity(old_capacity);
|
self.capacity = grow_capacity(old_capacity);
|
||||||
self.code = try self.allocator.realloc(self.code, self.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.code[self.count] = byte;
|
||||||
|
self.lines[self.count] = line;
|
||||||
self.count += 1;
|
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
|
/// Prints the current state of the chunk to stderr
|
||||||
pub fn dissasemble_chunk(self: *Chunk, name: []const u8) void {
|
pub fn dissasemble_chunk(self: *Chunk, name: []const u8) void {
|
||||||
print("== {s} ==\n", .{name});
|
print("== {s} ==\n", .{name});
|
||||||
|
@ -60,6 +57,13 @@ pub const Chunk = struct {
|
||||||
fn dissasemble_instruction(self: *Chunk, offset: usize) usize {
|
fn dissasemble_instruction(self: *Chunk, offset: usize) usize {
|
||||||
print("{d:0>4} ", .{offset});
|
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];
|
const instruction = self.code[offset];
|
||||||
switch (instruction) {
|
switch (instruction) {
|
||||||
@intFromEnum(OpCode.OP_RETURN) => return simple_instruction("OP_RETURN", offset),
|
@intFromEnum(OpCode.OP_RETURN) => return simple_instruction("OP_RETURN", offset),
|
||||||
|
@ -78,15 +82,17 @@ pub const Chunk = struct {
|
||||||
/// Destroys this chunk
|
/// Destroys this chunk
|
||||||
pub fn deinit(self: Chunk) void {
|
pub fn deinit(self: Chunk) void {
|
||||||
self.allocator.free(self.code);
|
self.allocator.free(self.code);
|
||||||
|
self.allocator.free(self.lines);
|
||||||
self.constants.deinit();
|
self.constants.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn constant_instruction(self: *Chunk, comptime name: []const u8, offset: usize) usize {
|
fn constant_instruction(self: *Chunk, comptime name: []const u8, offset: usize) usize {
|
||||||
const constant_addr = self.code[offset + 1];
|
const constant_addr = self.code[offset + 1];
|
||||||
const constant_value = self.constants.values[constant_addr];
|
const constant_value = self.constants.values[constant_addr];
|
||||||
|
|
||||||
print("{s} ", .{name});
|
print("{s: <16} {d: >4} '", .{ name, constant_addr });
|
||||||
print_value(constant_value);
|
print_value(constant_value);
|
||||||
print("\n", .{});
|
print("'\n", .{});
|
||||||
return offset + 2;
|
return offset + 2;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
17
src/main.zig
17
src/main.zig
|
@ -12,11 +12,11 @@ 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(@intFromEnum(chunk.OpCode.OP_RETURN));
|
|
||||||
|
|
||||||
const constant_idx = try c.add_constant(1.2);
|
const constant_idx = try c.add_constant(1.2);
|
||||||
try c.write(@intFromEnum(OpCode.OP_CONSTANT));
|
try c.write(@intFromEnum(OpCode.OP_CONSTANT), 1);
|
||||||
try c.write(@truncate(constant_idx));
|
try c.write(@truncate(constant_idx), 1);
|
||||||
|
|
||||||
|
try c.write(@intFromEnum(chunk.OpCode.OP_RETURN), 1);
|
||||||
|
|
||||||
c.dissasemble_chunk("test chunk");
|
c.dissasemble_chunk("test chunk");
|
||||||
}
|
}
|
||||||
|
@ -25,9 +25,8 @@ test "chunk test" {
|
||||||
var c = try chunk.Chunk.init(std.testing.allocator);
|
var c = try chunk.Chunk.init(std.testing.allocator);
|
||||||
defer c.deinit();
|
defer c.deinit();
|
||||||
|
|
||||||
try c.write('a');
|
try c.write('a', 0);
|
||||||
try c.write('b');
|
try c.write('b', 1);
|
||||||
try c.free_chunck();
|
try c.write('J', 2);
|
||||||
try c.write('J');
|
try c.write('H', 3);
|
||||||
try c.write('H');
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue