DzLox

:)
git clone https://git.sr.ht/~ashymad/DzLox
Log | Files | Refs | Submodules | LICENSE

commit c3997b2ea200e7bcc8ad818ee841a9cb641a950f
parent aefe9f1ddad710a4d181be6c4dabb37d85cccd7e
Author: Szymon Mikulicz <szymon.mikulicz@aptiv.com>
Date:   Sun,  4 Aug 2024 16:56:50 +0200

Construct map immediately

Diffstat:
Mzlox/src/compiler.zig | 10+++++-----
Mzlox/src/obj.zig | 13++-----------
2 files changed, 7 insertions(+), 16 deletions(-)

diff --git a/zlox/src/compiler.zig b/zlox/src/compiler.zig @@ -305,17 +305,17 @@ pub fn Compiler(size: comptime_int) type { } fn parseLiteralMap(self: *Self) CompilerError!Value { - var array = try ValueArray.init(self.allocator); - defer array.deinit(); + var ret = try self.objects.emplace(.Map, {}); + var retmap = ret.cast(.Map) catch unreachable; while (!self.match(Token.RIGHT_BRACKET)) { - try array.add(try self.parseLiteralValue()); + const key = try self.parseLiteralValue(); self.consume(Token.COLON, "Expect ':' after key in map initalizer"); - try array.add(try self.parseLiteralValue()); + try retmap.set(key, try self.parseLiteralValue()); if (self.match(Token.RIGHT_BRACKET)) break; self.consume(Token.COMMA, "Expect ',' after value in map initalizer"); } - return Value.init(try self.objects.emplace(.Map, array)); + return Value.init(ret); } fn map(self: *Self, _: bool) void { diff --git a/zlox/src/obj.zig b/zlox/src/obj.zig @@ -161,14 +161,14 @@ pub const Obj = packed struct { pub const Map = packed struct { const Self = @This(); - pub const Arg = value.ValueArray; + pub const Arg = void; const Table = table.Table(value.Value, value.Value, hash.hash_t(value.Value), value.Value.eql); obj: Super, map: *Table, hash: u32, - pub fn init(arg: Arg, allocator: std.mem.Allocator) Error!*Self { + pub fn init(_: Arg, allocator: std.mem.Allocator) Error!*Self { const self: *Self = try allocator.create(Self); self.* = Self{ .obj = Super{ @@ -178,15 +178,6 @@ pub const Obj = packed struct { .hash = 0, }; self.map.* = Table.init(allocator); - var i: u8 = 0; - while (i < arg.len) : (i += 2) { - const key = arg.get(i) catch unreachable; - const val = arg.get(i+1) catch unreachable; - self.hash +%= hash.hash(key) +% hash.hash(val); - if (!try self.map.set(key, val)) { - return error.KeyError; - } - } return self; } pub fn cast(self: *Self) *Super {