summaryrefslogtreecommitdiff
path: root/src/zisp/value/list.zig
diff options
context:
space:
mode:
authorTaylan Kammer <taylan.kammer@gmail.com>2026-06-20 22:53:50 +0200
committerTaylan Kammer <taylan.kammer@gmail.com>2026-06-20 22:53:50 +0200
commitb84ed4f563b3536365f7d3cc4d068407e98685b3 (patch)
tree9ab7b18d712db1329b6230cb45520e7c85dc46fd /src/zisp/value/list.zig
parentbfaa74b19fc81dbe071d55566a78a8e329237eff (diff)
It's a revolution baby.
Diffstat (limited to 'src/zisp/value/list.zig')
-rw-r--r--src/zisp/value/list.zig64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/zisp/value/list.zig b/src/zisp/value/list.zig
new file mode 100644
index 0000000..d040a34
--- /dev/null
+++ b/src/zisp/value/list.zig
@@ -0,0 +1,64 @@
+//! List array
+
+const std = @import("std");
+
+const Alloc = std.mem.Allocator;
+
+const gc = @import("../gc.zig");
+const value = @import("../value.zig");
+
+const ListPool = gc.ListPool;
+const Value = value.Value;
+
+// Zig API
+
+pub fn check(v: Value) bool {
+ return v.isList();
+}
+
+pub fn assert(v: Value) void {
+ if (!check(v)) {
+ v.dump();
+ @panic("not istr");
+ }
+}
+
+pub fn new(alloc: Alloc, pool: ?*ListPool, vals: []const Value) !Value {
+ var len_tagged_ptr: u48 = undefined;
+ if (vals.len < 8 and pool != null) {
+ const ptr = try pool.?.create(@intCast(vals.len));
+ for (vals, 0..) |v, i| ptr[i] = v;
+ len_tagged_ptr = @intCast(@intFromPtr(ptr) | vals.len);
+ } else {
+ const ptr = try alloc.alloc(Value, vals.len + 1);
+ for (vals, 0..) |v, i| ptr[i] = v;
+ ptr[vals.len] = value.none;
+ len_tagged_ptr = @intCast(@intFromPtr(ptr.ptr));
+ }
+ return .{ .list = .{ .len_tagged_ptr = len_tagged_ptr } };
+}
+
+pub fn getLenTag(v: Value) u3 {
+ return @truncate(v.bits);
+}
+
+pub fn getValPtr(v: Value) [*]Value {
+ return @ptrFromInt(v.bits & 0x0000fffffffffff8);
+}
+
+// Zisp API
+
+pub fn pred(v: Value) Value {
+ return value.boole.pack(check(v));
+}
+
+pub fn getLen(v: Value) Value {
+ assert(v);
+ var len = getLenTag(v);
+ if (len == 0) {
+ const ptr = getValPtr(v);
+ len = 8;
+ while (ptr[len].bits != value.none.bits) len += 1;
+ }
+ return value.fixnum.pack(@intCast(len));
+}