blob: d040a3418c0897eb7b53307a43e0f33a38023660 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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));
}
|