From 90af1ed17d317435fb56ea041fa0d937f5043726 Mon Sep 17 00:00:00 2001 From: Taylan Kammer Date: Sat, 6 Jun 2026 21:02:55 +0200 Subject: More cleanup. --- src/zisp/value/array.zig | 13 ++++++++----- src/zisp/value/istr.zig | 4 ++-- src/zisp/value/pair.zig | 2 -- src/zisp/value/string.zig | 13 +++++++++++++ 4 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 src/zisp/value/string.zig (limited to 'src/zisp/value') diff --git a/src/zisp/value/array.zig b/src/zisp/value/array.zig index f8e87bf..6be3c9c 100644 --- a/src/zisp/value/array.zig +++ b/src/zisp/value/array.zig @@ -1,11 +1,11 @@ const builtin = @import("builtin"); const std = @import("std"); +const Alloc = std.mem.Allocator; + const gc = @import("../gc.zig"); const value = @import("../value.zig"); -const Alloc = std.mem.Allocator; - const Value = value.Value; /// Pointer to header for an array of various element types and sizes. @@ -54,7 +54,7 @@ pub const ArrayHeader = packed struct(u64) { len_or_ptr: u48, is_slice: bool = false, is_ptr: bool = false, - type: enum(u2) { int, flt, val, str }, + type: ArrayType, info: packed union { int: packed struct(u12) { size: u10, @@ -154,6 +154,8 @@ pub const ArrayHeader = packed struct(u64) { } }; +const ArrayType = enum(u2) { int, flt, val, str }; + const Endian = enum(u1) { little, big, @@ -179,6 +181,7 @@ pub fn newString(alloc: Alloc, s: []const u8) !Value { return value.ptr.pack(.array, arr); } -pub fn check(v: Value) ?ArrayPtr { - return v.getPtr(.array) orelse null; +pub fn check(comptime t: ArrayType, v: Value) ?ArrayPtr { + if (v.getPtr(.array)) |p| if (p.type == t) return p; + return null; } diff --git a/src/zisp/value/istr.zig b/src/zisp/value/istr.zig index 9accd27..35093ed 100644 --- a/src/zisp/value/istr.zig +++ b/src/zisp/value/istr.zig @@ -4,11 +4,11 @@ const std = @import("std"); +const Alloc = std.mem.Allocator; + const gc = @import("../gc.zig"); const value = @import("../value.zig"); -const Alloc = std.mem.Allocator; - const IstrSet = gc.IstrSet; const Value = value.Value; diff --git a/src/zisp/value/pair.zig b/src/zisp/value/pair.zig index f2a27a0..4ae38a3 100644 --- a/src/zisp/value/pair.zig +++ b/src/zisp/value/pair.zig @@ -1,5 +1,3 @@ -const std = @import("std"); - const gc = @import("../gc.zig"); const value = @import("../value.zig"); diff --git a/src/zisp/value/string.zig b/src/zisp/value/string.zig new file mode 100644 index 0000000..129e35b --- /dev/null +++ b/src/zisp/value/string.zig @@ -0,0 +1,13 @@ +//! Abstract Data Type over Sstr, Istr, Array + +const value = @import("../value.zig"); + +const Value = value.Value; + +pub fn getString(s: *const Value) ![]const u8 { + if (value.sstr.check(s.*)) return value.sstr.unpack(s); + if (value.istr.check(s.*)) |istr_ptr| return istr_ptr.str(); + if (value.array.check(.str, s.*)) |arr_ptr| return arr_ptr.str(); + s.dump(); + @panic("This is not a string."); +} -- cgit v1.2.3