const std = @import("std"); const value = @import("../value.zig"); const gc = @import("../gc.zig"); const Value = value.Value; // Zig API /// Pointer to beginning of an interned string with a 1-byte length prefix. /// Aligned to 8 bytes like all Zisp heap pointers. pub const Istr = *align(8) struct { pub fn buf(self: *@This()) [*]u8 { return @ptrCast(self); } pub fn len(self: *@This()) u8 { return buf()[0]; } pub fn str(self: *@This()) []u8 { return buf()[1 .. 1 + len()]; } }; pub fn check(v: Value) ?Istr { if (v.getPtr(.istr)) |p| { return @ptrCast(p); } return null; } pub fn assert(v: Value) Istr { return check(v) orelse { v.dump(); @panic("not istr"); }; } pub fn isValidIstr(s: []const u8) bool { return s.len <= 255; } fn assertValidIstr(s: []const u8) void { if (!isValidIstr(s)) { std.debug.print("invalid istr: {s}\n", .{s}); @panic("invalid istr"); } } pub fn intern(s: []const u8) !Value { assertValidIstr(s); return gc.internString(s); } // Zisp API pub fn pred(v: Value) Value { return value.boole.pack(check(v) != null); } pub fn len(v: Value) Value { const istr = assert(v); return value.fixnum.pack(@intCast(istr.len())); }