From 23070e09008d2a05a0df508fd0b739616ac9f8a6 Mon Sep 17 00:00:00 2001 From: Araozu Date: Mon, 13 May 2024 21:23:28 -0500 Subject: [PATCH] Add constants --- src/chunk.zig | 22 ++++++++++++++++++++-- src/main.zig | 14 ++++++++------ src/value.zig | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 src/value.zig diff --git a/src/chunk.zig b/src/chunk.zig index 5471a95..f99ad5f 100644 --- a/src/chunk.zig +++ b/src/chunk.zig @@ -1,5 +1,7 @@ const std = @import("std"); const print = std.debug.print; +const value = @import("./value.zig"); +const ValueArray = value.ValueArray; pub const OpCode = enum(u8) { OP_RETURN, @@ -9,22 +11,26 @@ pub const Chunk = struct { code: []u8, count: usize, capacity: usize, + constants: ValueArray, allocator: std.mem.Allocator, + /// Initializes a new Chunk pub fn init(allocator: std.mem.Allocator) !Chunk { return .{ .count = 0, .capacity = 0, .allocator = allocator, .code = try allocator.alloc(u8, 0), + .constants = try ValueArray.init(allocator), }; } - pub fn write_chunck(self: *Chunk, byte: u8) !void { + /// Writes a byte to this chunk + 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 = if (old_capacity < 8) 8 else old_capacity * 2; + self.capacity = grow_capacity(old_capacity); self.code = try self.allocator.realloc(self.code, self.capacity); } @@ -39,6 +45,7 @@ pub const Chunk = struct { 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}); @@ -48,6 +55,7 @@ pub const Chunk = struct { } } + /// Prints the value of a single instruction fn dissasemble_instruction(self: *Chunk, offset: usize) usize { print("{d:0>4} ", .{offset}); @@ -61,8 +69,14 @@ pub const Chunk = struct { } } + pub fn add_constant(self: *Chunk, v: value.Value) !usize { + return try self.constants.add_constant(v); + } + + /// Destroys this chunk pub fn deinit(self: Chunk) void { self.allocator.free(self.code); + self.constants.deinit(); } }; @@ -70,3 +84,7 @@ fn simple_instruction(comptime name: []const u8, offset: usize) usize { print("{s}\n", .{name}); return offset + 1; } + +inline fn grow_capacity(old: usize) usize { + return if (old < 8) 8 else old * 2; +} diff --git a/src/main.zig b/src/main.zig index f2f02d0..943910d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -11,8 +11,10 @@ pub fn main() !void { var c = try chunk.Chunk.init(alloc); defer c.deinit(); - try c.write_chunck(@intFromEnum(chunk.OpCode.OP_RETURN)); - try c.write_chunck(@intFromEnum(chunk.OpCode.OP_RETURN)); + 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"); } @@ -20,9 +22,9 @@ test "chunk test" { var c = try chunk.Chunk.init(std.testing.allocator); defer c.deinit(); - try c.write_chunck('a'); - try c.write_chunck('b'); + try c.write('a'); + try c.write('b'); try c.free_chunck(); - try c.write_chunck('J'); - try c.write_chunck('H'); + try c.write('J'); + try c.write('H'); } diff --git a/src/value.zig b/src/value.zig new file mode 100644 index 0000000..edd0e25 --- /dev/null +++ b/src/value.zig @@ -0,0 +1,34 @@ +const std = @import("std"); + +pub const Value = f64; + +pub const ValueArray = struct { + capacity: usize, + count: usize, + values: []Value, + allocator: std.mem.Allocator, + + pub fn init(allocator: std.mem.Allocator) !ValueArray { + return .{ .capacity = 0, .count = 0, .values = try allocator.alloc(Value, 0), .allocator = allocator }; + } + + pub fn write(self: *ValueArray, value: Value) !void { + if (self.count == self.capacity) { + const old = self.capacity; + self.capacity = if (old < 8) 8 else old * 2; + self.values = try self.allocator.realloc(self.values, self.capacity); + } + + self.values[self.count] = value; + self.count += 1; + } + + pub fn add_constant(self: *ValueArray, value: Value) !usize { + try self.write(value); + return self.count - 1; + } + + pub fn deinit(self: ValueArray) void { + self.allocator.free(self.values); + } +};