summaryrefslogtreecommitdiff
path: root/src/libzisp/value/istr.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/libzisp/value/istr.zig')
-rw-r--r--src/libzisp/value/istr.zig55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/libzisp/value/istr.zig b/src/libzisp/value/istr.zig
index 5937531..8056d98 100644
--- a/src/libzisp/value/istr.zig
+++ b/src/libzisp/value/istr.zig
@@ -1,3 +1,58 @@
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 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 bytes_ptr = gc.intern(header, str);
+ return ptr.pack(bytes_ptr, .seq);
+}
+
+pub fn getHeader(v: Value) *seq.Header {
+ assert(v);
+ return gc.istrHeader(ptr.unpack(v).@"0");
+}
+
+// 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));
+}