DzLox

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

obj::table.zig (2713B)


      1 const std = @import("std");
      2 
      3 const table = @import("lib::table.zig");
      4 const utils = @import("lib::utils.zig");
      5 const hash = @import("hash.zig");
      6 
      7 const Packed = @import("lib::packed.zig").Packed;
      8 const Value = @import("value.zig").Value;
      9 const Obj = @import("obj.zig").Obj;
     10 
     11 pub fn Table(fields: anytype) type {
     12     const Super = Obj(fields);
     13 
     14     return packed struct {
     15         const Self = @This();
     16         pub const Arg = void;
     17         pub const Table = table.Table(Value, Value, hash.hash_t(Value), Value.eql);
     18         pub const Error = error{OutOfMemory} || Self.Table.Error;
     19 
     20         obj: Super,
     21         table: Packed(*Self.Table),
     22 
     23         pub fn init(_: Arg, allocator: std.mem.Allocator) Error!*Self {
     24             const self: *Self = try allocator.create(Self);
     25             self.* = Self{
     26                 .obj = Super.make(Self),
     27                 .table = try Packed(*Self.Table).create(allocator),
     28             };
     29             return self;
     30         }
     31 
     32         pub fn cast(self: anytype) utils.copy_const(@TypeOf(self), *Super) {
     33             return @ptrCast(self);
     34         }
     35 
     36         pub fn set(self: *Self, key: Value, val: Value) Error!bool {
     37             return self.table.ptr().set(key, val);
     38         }
     39 
     40         pub fn get(self: *Self, key: Value) Error!Value {
     41             return self.table.ptr().get(key);
     42         }
     43 
     44         pub fn delete(self: *Self, key: Value) void {
     45             _ = self.table.ptr().delete(key);
     46         }
     47 
     48         pub fn format(self: *const Self, writer: *std.Io.Writer) !void {
     49             const Printer = struct {
     50                 writer: @TypeOf(writer),
     51                 count: usize,
     52 
     53                 pub fn print(this: *@This(), key: Value, val: Value) std.Io.Writer.Error!void {
     54                     this.count -= 1;
     55 
     56                     try key.format(this.writer);
     57                     _ = try this.writer.write(":");
     58                     try val.format(this.writer);
     59                     if (this.count > 0) _ = try this.writer.write(", ");
     60                 }
     61             };
     62 
     63             var printer = Printer{ .writer = writer, .count = self.table.ptr().count };
     64             _ = try writer.write("[");
     65             if (self.table.ptr().count > 0) {
     66                 try self.table.ptr().for_each_try(&printer, Printer.print);
     67             } else {
     68                 _ = try writer.write(":");
     69             }
     70             _ = try writer.writeAll("]");
     71         }
     72 
     73         pub fn eql(self: *const Self, other: *const Self) bool {
     74             return self.table.ptr().eql(other.table.ptr(), Value.eql);
     75         }
     76 
     77         pub fn free(self: *const Self, allocator: std.mem.Allocator) void {
     78             self.table.destroy(allocator);
     79             allocator.destroy(self);
     80         }
     81     };
     82 }