blob: abd04472418229830332b3e1a9769eae28e70db6 (
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
65
|
const std = @import("std");
const value = @import("../value.zig");
const gc = @import("../gc.zig");
const ptr = @import("ptr.zig");
const seq = @import("seq.zig");
const Hval = gc.Hval;
const Value = value.Value;
// Zig API
pub fn check(v: Value) bool {
return ptr.checkZispTag(v, .seq);
}
pub fn assert(v: Value) void {
if (!check(v)) {
v.dump();
@panic("not istr");
}
}
pub fn intern(str: []const u8, quoted: bool) Value {
if (str.len > value.fixnum.max) {
@panic("String length out of fixnum range.");
}
const header: seq.Header = .{
.type = .string,
.info = .{ .string = .{
.enc = .utf8,
.quoted = quoted,
.interned = true,
} },
.size = @intCast(str.len),
};
const header_ptr = gc.intern(header, str);
return ptr.pack(@ptrCast(header_ptr), .seq);
}
pub fn getHeader(v: Value) *seq.Header {
assert(v);
const header_ptr, _ = ptr.unpack(v);
return gc.istrHeader(header_ptr);
}
pub fn getHeaderFromPtr(p: *Hval) *seq.Header {
return gc.istrHeader(p);
}
// Zisp API
pub fn pred(v: Value) Value {
return value.boole.pack(check(v));
}
pub fn len(v: Value) Value {
const l = getHeader(v).size;
if (l > value.fixnum.max) {
@panic("string length out of range");
}
return value.fixnum.pack(@intCast(l));
}
|