Compare commits

..

No commits in common. "0f891e4275e3effb0700f4d71ebdec99c48b6873" and "23070e09008d2a05a0df508fd0b739616ac9f8a6" have entirely different histories.

2 changed files with 16 additions and 39 deletions

View File

@ -5,12 +5,10 @@ const ValueArray = value.ValueArray;
pub const OpCode = enum(u8) {
OP_RETURN,
OP_CONSTANT,
};
pub const Chunk = struct {
code: []u8,
lines: []u32,
count: usize,
capacity: usize,
constants: ValueArray,
@ -23,26 +21,30 @@ 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, line: u32) !void {
pub fn write(self: *Chunk, byte: u8) !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});
@ -57,17 +59,9 @@ 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),
@intFromEnum(OpCode.OP_CONSTANT) => return self.constant_instruction("OP_CONSTANT", offset),
else => {
print("unknown opcode {d}\n", .{instruction});
return offset + 1;
@ -82,19 +76,8 @@ 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: <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 {
@ -102,10 +85,6 @@ fn simple_instruction(comptime name: []const u8, offset: usize) usize {
return offset + 1;
}
fn print_value(v: value.Value) void {
print("{d}", .{v});
}
inline fn grow_capacity(old: usize) usize {
return if (old < 8) 8 else old * 2;
}

View File

@ -1,6 +1,5 @@
const std = @import("std");
const chunk = @import("./chunk.zig");
const OpCode = chunk.OpCode;
pub fn main() !void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
@ -12,12 +11,10 @@ pub fn main() !void {
var c = try chunk.Chunk.init(alloc);
defer c.deinit();
const constant_idx = try c.add_constant(1.2);
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.write(@intFromEnum(chunk.OpCode.OP_RETURN));
try c.write(@intFromEnum(chunk.OpCode.OP_RETURN));
_ = try c.add_constant(0.0);
c.dissasemble_chunk("test chunk");
}
@ -25,8 +22,9 @@ test "chunk test" {
var c = try chunk.Chunk.init(std.testing.allocator);
defer c.deinit();
try c.write('a', 0);
try c.write('b', 1);
try c.write('J', 2);
try c.write('H', 3);
try c.write('a');
try c.write('b');
try c.free_chunck();
try c.write('J');
try c.write('H');
}