commit 50245c3c19db72a2761149ac6422e7ab2adf6831
parent 18b75d9d52446ca806468ff019c52fde9b1dd756
Author: Szymon Mikulicz <szymon.mikulicz@aptiv.com>
Date: Sat, 3 Aug 2024 21:47:10 +0200
Update map hash when adding elements
Diffstat:
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/zlox/src/obj.zig b/zlox/src/obj.zig
@@ -186,6 +186,12 @@ pub const Obj = packed struct {
pub fn cast(self: *Self) *Super {
return @ptrCast(self);
}
+
+ pub fn set(self: *Self, key: value.Value, val: value.Value) !void {
+ self.hash +%= hash.hash(key) +% hash.hash(val);
+ _ = try self.map.set(key, val);
+ }
+
fn print_element(key: value.Value, val: value.Value) void {
key.print();
std.debug.print(":", .{});
diff --git a/zlox/src/vm.zig b/zlox/src/vm.zig
@@ -223,7 +223,7 @@ pub const VM = struct {
self.runtimeError("Cannot index a non-map value", .{});
return InterpreterError.RuntimeError;
}
- _ = try (map.obj.cast(.Map) catch unreachable).map.set(idx, val);
+ _ = try (map.obj.cast(.Map) catch unreachable).set(idx, val);
self.push(val);
},
@intFromEnum(OP.DEFINE_GLOBAL) => _ = try self.vm.globals.set(self.read_string(), Global.make_var(self.pop())),