Compare commits

..

2 Commits

Author SHA1 Message Date
Araozu 0f891e4275 Line information 2024-05-14 09:22:58 -05:00
Araozu 975280be8f constant dissasemble 2024-05-14 09:00:34 -05:00
2 changed files with 39 additions and 16 deletions

View File

@ -5,10 +5,12 @@ const ValueArray = value.ValueArray;
pub const OpCode = enum(u8) { pub const OpCode = enum(u8) {
OP_RETURN, OP_RETURN,
OP_CONSTANT,
}; };
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,
@ -21,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});
@ -59,9 +57,17 @@ 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),
@intFromEnum(OpCode.OP_CONSTANT) => return self.constant_instruction("OP_CONSTANT", offset),
else => { else => {
print("unknown opcode {d}\n", .{instruction}); print("unknown opcode {d}\n", .{instruction});
return offset + 1; return offset + 1;
@ -76,8 +82,19 @@ 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 {
const constant_addr = self.code[offset + 1];
const constant_value = self.constants.values[constant_addr];
print("{s: <16} {d: >4} '", .{ name, constant_addr });
print_value(constant_value);
print("'\n", .{});
return offset + 2;
}
}; };
fn simple_instruction(comptime name: []const u8, offset: usize) usize { fn simple_instruction(comptime name: []const u8, offset: usize) usize {
@ -85,6 +102,10 @@ fn simple_instruction(comptime name: []const u8, offset: usize) usize {
return offset + 1; return offset + 1;
} }
fn print_value(v: value.Value) void {
print("{d}", .{v});
}
inline fn grow_capacity(old: usize) usize { inline fn grow_capacity(old: usize) usize {
return if (old < 8) 8 else old * 2; return if (old < 8) 8 else old * 2;
} }

View File

@ -1,5 +1,6 @@
const std = @import("std"); const std = @import("std");
const chunk = @import("./chunk.zig"); const chunk = @import("./chunk.zig");
const OpCode = chunk.OpCode;
pub fn main() !void { pub fn main() !void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
@ -11,10 +12,12 @@ 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);
try c.write(@intFromEnum(chunk.OpCode.OP_RETURN)); try c.write(@intFromEnum(OpCode.OP_CONSTANT), 1);
try c.write(@truncate(constant_idx), 1);
try c.write(@intFromEnum(chunk.OpCode.OP_RETURN), 1);
_ = try c.add_constant(0.0);
c.dissasemble_chunk("test chunk"); c.dissasemble_chunk("test chunk");
} }
@ -22,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');
} }