DzLox

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

obj::chunk.zig (2385B)


      1 const std = @import("std");
      2 
      3 const utils = @import("lib::utils.zig");
      4 const array = @import("lib::array.zig");
      5 
      6 const Value = @import("value.zig").Value;
      7 const Obj = @import("obj.zig").Obj;
      8 const OP = @import("op.zig").OP;
      9 const Packed = @import("lib::packed.zig").Packed;
     10 
     11 pub fn Chunk(fields: anytype) type {
     12     const Super = Obj(fields);
     13 
     14     return packed struct {
     15         const Self = @This();
     16 
     17         pub const Arg = void;
     18         pub const Error = error{OutOfMemory};
     19 
     20         pub const Code = array.Array(u8);
     21         pub const Constants = array.Array(Value);
     22         pub const Lines = array.Array(i32);
     23 
     24         obj: Super,
     25         code: Packed(*Code),
     26         constants: Packed(*Constants),
     27         lines: Packed(*Lines),
     28 
     29         pub fn init(_: Arg, allocator: std.mem.Allocator) Error!*Self {
     30             const self: *Self = try allocator.create(Self);
     31             self.* = Self{
     32                 .obj = Super.make(Self),
     33                 .code = try Packed(*Code).create(allocator),
     34                 .constants = try Packed(*Constants).create(allocator),
     35                 .lines = try Packed(*Lines).create(allocator),
     36             };
     37             return self;
     38         }
     39 
     40         pub fn cast(self: anytype) utils.copy_const(@TypeOf(self), *Super) {
     41             return @ptrCast(self);
     42         }
     43 
     44         pub fn format(self: *const Self, writer: *std.Io.Writer) !void {
     45             _ = try writer.print("{d}", .{self.lines.ptr().get(0) orelse 0});
     46         }
     47 
     48         pub fn free(self: *const Self, allocator: std.mem.Allocator) void {
     49             self.constants.destroy(allocator);
     50             self.lines.destroy(allocator);
     51             self.code.destroy(allocator);
     52             allocator.destroy(self);
     53         }
     54 
     55         pub fn write(self: *@This(), byte: u8, line: i32) Error!void {
     56             try self.code.ptr().add(byte);
     57             try self.lines.ptr().add(line);
     58         }
     59 
     60         pub fn writeOP(self: *@This(), op: OP, line: i32) Error!void {
     61             try self.write(@intFromEnum(op), line);
     62         }
     63 
     64         pub fn addConstant(self: *@This(), val: Value) Error!u8 {
     65             for (self.constants.ptr().slice(), 0..) |el, i| {
     66                 if (el.eql(val)) {
     67                     return @intCast(i);
     68                 }
     69             }
     70             try self.constants.ptr().add(val);
     71             return @intCast(self.constants.ptr().len - 1);
     72         }
     73     };
     74 }