diff --git a/src/chunk.zig b/src/chunk.zig index aaee6e1..fcdc648 100644 --- a/src/chunk.zig +++ b/src/chunk.zig @@ -23,19 +23,18 @@ pub const Chunk = struct { .capacity = 0, .code = &.{}, .lines = &.{}, - .constants = ValueArray.new(), + .constants = ValueArray.new(allocator), .allocator = allocator, }; } - pub fn init(self: *Chunk) !void { - self.deinit(self.allocator); + pub fn destroy(self: *Chunk) void { + self.constants.destroy(); - self.count = 0; - self.capacity = 0; - self.code = &.{}; - self.lines = &.{}; - self.constants = ValueArray.new(); + if (self.capacity > 0) { + self.allocator.free(self.code); + self.allocator.free(self.lines); + } } pub fn write(self: *Chunk, byte: u8, line: usize) !void { @@ -109,17 +108,8 @@ pub const Chunk = struct { } } - pub fn deinit(self: *Chunk) void { - self.constants.free(self.allocator); - - if (self.capacity > 0) { - self.allocator.free(self.code); - self.allocator.free(self.lines); - } - } - pub fn add_constant(self: *Chunk, value: Value) !usize { - try self.constants.write(self.allocator, value); + try self.constants.write(value); return self.constants.count - 1; } }; diff --git a/src/compile.zig b/src/compile.zig index 5f8a26c..3f838c5 100644 --- a/src/compile.zig +++ b/src/compile.zig @@ -746,8 +746,8 @@ const Local = struct { depth: ?usize, }; -pub fn compile(allocator: Allocator, vm: *VM, contents: []const u8) !?*Obj.Function { - var compiler = Compiler.new(allocator, FunctionType.Script); +pub fn compile(vm: *VM, contents: []const u8) !?*Obj.Function { + var compiler = Compiler.new(vm.allocator, FunctionType.Script); var scanner = Scanner.init(contents); var parser = Parser.new(vm, &compiler, &scanner); diff --git a/src/main.zig b/src/main.zig index e919faa..b64a07b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -63,7 +63,7 @@ pub fn main() !void { defer std.process.argsFree(allocator, args); var vm = VM.new(allocator); - defer vm.free(); + defer vm.destroy(); if (args.len == 1) { try repl(allocator, &vm); diff --git a/src/object.zig b/src/object.zig index 38e8297..21766fb 100644 --- a/src/object.zig +++ b/src/object.zig @@ -36,9 +36,8 @@ pub const Obj = struct { } pub fn destroy(self: *String) void { - const allocator = self.obj.allocator; - allocator.free(self.chars); - allocator.destroy(self); + self.obj.allocator.free(self.chars); + self.obj.allocator.destroy(self); } }; @@ -64,9 +63,8 @@ pub const Obj = struct { } pub fn destroy(self: *Function) void { - const allocator = self.obj.allocator; - self.chunk.deinit(); - allocator.destroy(self); + self.chunk.destroy(); + self.obj.allocator.destroy(self); } }; diff --git a/src/table.zig b/src/table.zig index 74349f7..fc72980 100644 --- a/src/table.zig +++ b/src/table.zig @@ -30,7 +30,7 @@ pub const Table = struct { }; } - pub fn deinit(self: *Table) void { + pub fn destroy(self: *Table) void { if (self.capacity == 0) { return; } @@ -202,7 +202,7 @@ test "initialize an hash table" { const allocator = std.testing.allocator; var table = Table.new(allocator); - defer table.deinit(); + defer table.destroy(); try std.testing.expectEqual(0, table.count); try std.testing.expectEqual(0, table.capacity); } @@ -214,7 +214,7 @@ test "adding values" { defer key.destroy(); var table = Table.new(allocator); - defer table.deinit(); + defer table.destroy(); var res = table.set(key, Value.nil_val()); try std.testing.expectEqual(true, res); @@ -234,7 +234,7 @@ test "adding tables" { defer key.destroy(); var table = Table.new(allocator); - defer table.deinit(); + defer table.destroy(); const res = table.set(key, Value.nil_val()); try std.testing.expectEqual(true, res); @@ -242,7 +242,7 @@ test "adding tables" { try std.testing.expectEqual(1, table.count); var table2 = Table.new(allocator); - defer table2.deinit(); + defer table2.destroy(); try std.testing.expectEqual(0, table2.capacity); try std.testing.expectEqual(0, table2.count); @@ -259,7 +259,7 @@ test "deleting from table" { defer key.destroy(); var table = Table.new(allocator); - defer table.deinit(); + defer table.destroy(); var res = table.set(key, Value.nil_val()); try std.testing.expectEqual(true, res); @@ -279,7 +279,7 @@ test "find" { defer key.destroy(); var table = Table.new(allocator); - defer table.deinit(); + defer table.destroy(); const value = Value.number_val(42.0); diff --git a/src/values.zig b/src/values.zig index 5f76929..4deae4f 100644 --- a/src/values.zig +++ b/src/values.zig @@ -141,32 +141,34 @@ pub const Value = struct { }; pub const ValueArray = struct { + allocator: Allocator, capacity: usize, count: usize, values: []Value, - pub fn new() ValueArray { + pub fn new(allocator: Allocator) ValueArray { return ValueArray{ + .allocator = allocator, .capacity = 0, .count = 0, .values = &.{}, }; } - pub fn write(self: *ValueArray, allocator: Allocator, value: Value) !void { + pub fn write(self: *ValueArray, value: Value) !void { if (self.capacity < self.count + 1) { const old_capacity = self.capacity; self.capacity = utils.grow_capacity(old_capacity); - self.values = try allocator.realloc(self.values, self.capacity); + self.values = try self.allocator.realloc(self.values, self.capacity); } self.values[self.count] = value; self.count += 1; } - pub fn free(self: *ValueArray, allocator: Allocator) void { + pub fn destroy(self: *ValueArray) void { if (self.capacity > 0) { - allocator.free(self.values); + self.allocator.free(self.values); } } }; diff --git a/src/vm.zig b/src/vm.zig index 26544a9..babbf03 100644 --- a/src/vm.zig +++ b/src/vm.zig @@ -53,13 +53,13 @@ pub const VM = struct { }; } - pub fn free(self: *VM) void { + pub fn destroy(self: *VM) void { if (constants.DEBUG_PRINT_INTERNAL_STRINGS) { self.strings.dump(); } - self.strings.deinit(); - self.globals.deinit(); + self.strings.destroy(); + self.globals.destroy(); self.clean_references(); self.references.deinit(); } @@ -74,9 +74,9 @@ pub const VM = struct { pub fn interpret(self: *VM, allocator: Allocator, content: []const u8) !InterpretResult { var chunk = Chunk.new(allocator); - defer chunk.deinit(); + defer chunk.destroy(); - const function = try compile(allocator, self, content); + const function = try compile(self, content); if (function == null) { return InterpretResult.COMPILE_ERROR; }