summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.zig1
-rw-r--r--src/test/parse.zig2
-rw-r--r--src/test/values.zig2
-rw-r--r--src/zisp.zig3
-rw-r--r--src/zisp/io/print.zig40
-rw-r--r--src/zisp/value.zig16
-rw-r--r--src/zisp/value/array.zig10
-rw-r--r--src/zisp/value/istr.zig7
-rw-r--r--src/zisp/value/pair.zig4
-rw-r--r--src/zisp/value/ptr.zig10
10 files changed, 35 insertions, 60 deletions
diff --git a/src/main.zig b/src/main.zig
index a12ba99..fcf3284 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -1,5 +1,4 @@
const std = @import("std");
-
const zisp = @import("zisp");
pub fn main() !u8 {
diff --git a/src/test/parse.zig b/src/test/parse.zig
index 26d54bd..bc665e9 100644
--- a/src/test/parse.zig
+++ b/src/test/parse.zig
@@ -9,7 +9,7 @@ const io = std.Io.Threaded.global_single_threaded.io();
pub const zisp = @import("../zisp.zig");
pub const value = zisp.value;
-pub const Value = zisp.Value;
+pub const Value = zisp.value.Value;
fn parse(str: []const u8) Value {
var fbs = std.Io.Reader.fixed(str);
diff --git a/src/test/values.zig b/src/test/values.zig
index e628b33..968a5bc 100644
--- a/src/test/values.zig
+++ b/src/test/values.zig
@@ -48,7 +48,7 @@ test "ptr" {
const val: Zptr = @ptrFromInt(256);
const tag = PtrTag.pair;
- const p = ptr.pack(val, tag);
+ const p = ptr.pack(tag, @ptrCast(val));
try testing.expect(ptr.check(p));
const pv, const pt = ptr.unpack(p);
diff --git a/src/zisp.zig b/src/zisp.zig
index 693b7b4..ae4aabc 100644
--- a/src/zisp.zig
+++ b/src/zisp.zig
@@ -6,9 +6,6 @@ pub const io = @import("zisp/io.zig");
pub const lib = @import("zisp/lib.zig");
pub const value = @import("zisp/value.zig");
-pub const Value = value.Value;
-pub const Zptr = value.Zptr;
-
pub const _test = @import("test/all.zig");
test {
diff --git a/src/zisp/io/print.zig b/src/zisp/io/print.zig
index 56d54f7..4312c98 100644
--- a/src/zisp/io/print.zig
+++ b/src/zisp/io/print.zig
@@ -1,33 +1,24 @@
const std = @import("std");
-const gc = @import("../gc.zig");
+const io = @import("../io.zig");
const value = @import("../value.zig");
const Writer = *std.Io.Writer;
-const Parser = @import("Parser.zig");
-
+const Parser = io.Parser;
const Value = value.Value;
-
const PairPtr = value.pair.PairPtr;
const IstrPtr = value.istr.IstrPtr;
const ArrayPtr = value.array.ArrayPtr;
pub fn toWriter(w: Writer, v: Value) anyerror!void {
- // zig fmt: off
- try if (v.isDouble()) double(w, v)
- else if (v.isFixnum()) fixnum(w, v)
- else if (v.getPtr(.pair)) |p| pair(w, @ptrCast(p))
- else if (v.getPtr(.istr)) |p| istr(w, @ptrCast(p))
- else if (v.getPtr(.array)) |p| array(w, @ptrCast(p))
- else if (v.isRune()) rune(w, v)
- else if (v.isChar()) char(w, v)
- else if (v.isMisc()) misc(w, v)
- else if (v.isSrat()) srat(w, v)
- else if (v.isSstr()) sstr(w, v)
- else @panic("unhandled type")
- ;
- // zig fmt: on
+ if (v.isSstr()) return sstr(w, v);
+ if (v.isRune()) return rune(w, v);
+ if (v.isMisc()) return misc(w, v);
+ if (v.getPtr(.istr)) |p| return istr(w, p);
+ if (v.getPtr(.array)) |p| return array(w, p);
+ if (v.getPtr(.pair)) |p| return pair(w, p);
+ @panic("unhandled type");
}
pub fn double(w: Writer, v: Value) !void {
@@ -50,13 +41,6 @@ pub fn sstr(w: Writer, v: Value) !void {
try w.writeAll(value.sstr.unpack(&v));
}
-pub fn char(w: Writer, v: Value) !void {
- const uc: u21 = value.char.unpack(v);
- var buf: [4]u8 = undefined;
- const len = try std.unicode.utf8Encode(uc, &buf);
- try w.writeAll(buf[0..len]);
-}
-
pub fn misc(w: Writer, v: Value) !void {
try switch (v.bits) {
value.f.bits => w.writeAll("#f"),
@@ -69,12 +53,6 @@ pub fn misc(w: Writer, v: Value) !void {
};
}
-pub fn srat(w: Writer, v: Value) !void {
- _ = w;
- _ = v;
- @panic("not implemented");
-}
-
pub fn pair(w: Writer, p: PairPtr) !void {
try switch (p.car.bits) {
Parser.PQSTR.bits => quotString(w, p.cdr, '|'),
diff --git a/src/zisp/value.zig b/src/zisp/value.zig
index 4ecc70f..d39e225 100644
--- a/src/zisp/value.zig
+++ b/src/zisp/value.zig
@@ -153,17 +153,16 @@ const gc = @import("gc.zig");
pub const double = @import("value/double.zig");
pub const fixnum = @import("value/fixnum.zig");
-pub const ptr = @import("value/ptr.zig");
-pub const array = @import("value/array.zig");
-
pub const rune = @import("value/rune.zig");
pub const char = @import("value/char.zig");
pub const misc = @import("value/misc.zig");
pub const sstr = @import("value/sstr.zig");
pub const boole = @import("value/boole.zig");
+pub const ptr = @import("value/ptr.zig");
pub const pair = @import("value/pair.zig");
pub const istr = @import("value/istr.zig");
+pub const array = @import("value/array.zig");
const endian = builtin.target.cpu.arch.endian();
@@ -216,6 +215,15 @@ pub const PtrTag = enum(u3) {
array,
/// Procedure
proc,
+
+ pub fn ptrType(self: PtrTag) type {
+ return switch (self) {
+ .pair => pair.PairPtr,
+ .istr => istr.IstrPtr,
+ .array => array.ArrayPtr,
+ else => @panic("not implemented"),
+ };
+ }
};
// Non-pointer high bits (sign=0,exp=MAX,quiet=0) but as a u13 field.
@@ -395,7 +403,7 @@ pub const Value = packed union {
/// the "high" pointer tags (49-51) that encode properties like weakness
/// don't matter; we only care if it's some kind of valid heap pointer of
/// the given type, so we only check the type tag in the low bits (0-2).
- pub fn getPtr(v: Value, tag: PtrTag) ?Zptr {
+ pub fn getPtr(v: Value, comptime tag: PtrTag) ?tag.ptrType() {
// Readable version:
//
// if (v.isPacked() and !v.ieee.sign and v.ieee.quiet ...)
diff --git a/src/zisp/value/array.zig b/src/zisp/value/array.zig
index 629d31c..f8e87bf 100644
--- a/src/zisp/value/array.zig
+++ b/src/zisp/value/array.zig
@@ -168,8 +168,7 @@ pub fn newString(alloc: Alloc, s: []const u8) !Value {
std.debug.assert(s.len <= std.math.maxInt(u48));
const algn = std.mem.Alignment.of(ArrayPtr);
const size = @sizeOf(ArrayHeader) + s.len;
- const ptr = try alloc.alignedAlloc(u8, algn, size);
- const arr: ArrayPtr = @ptrCast(ptr);
+ const arr: ArrayPtr = @ptrCast(try alloc.alignedAlloc(u8, algn, size));
arr.* = .{
.len_or_ptr = @intCast(s.len),
.type = .str,
@@ -177,12 +176,9 @@ pub fn newString(alloc: Alloc, s: []const u8) !Value {
};
const buf = arr.bufContent();
@memcpy(buf[0..s.len], s);
- return value.ptr.pack(arr, .array);
+ return value.ptr.pack(.array, arr);
}
pub fn check(v: Value) ?ArrayPtr {
- if (v.getPtr(.array)) |p| {
- return @ptrCast(p);
- }
- return null;
+ return v.getPtr(.array) orelse null;
}
diff --git a/src/zisp/value/istr.zig b/src/zisp/value/istr.zig
index 551ea1e..9accd27 100644
--- a/src/zisp/value/istr.zig
+++ b/src/zisp/value/istr.zig
@@ -38,10 +38,7 @@ pub const IstrHead = packed struct(u8) {
};
pub fn check(v: Value) ?IstrPtr {
- if (v.getPtr(.istr)) |p| {
- return @ptrCast(p);
- }
- return null;
+ return v.getPtr(.istr) orelse null;
}
pub fn assert(v: Value) IstrPtr {
@@ -71,7 +68,7 @@ pub fn new(alloc: Alloc, s: []const u8) !IstrPtr {
}
pub fn pack(istr: IstrPtr) Value {
- return value.ptr.pack(@ptrCast(istr), .istr);
+ return value.ptr.pack(.istr, istr);
}
pub fn getOrNew(s: []const u8) !Value {
diff --git a/src/zisp/value/pair.zig b/src/zisp/value/pair.zig
index edc106d..f2a27a0 100644
--- a/src/zisp/value/pair.zig
+++ b/src/zisp/value/pair.zig
@@ -18,7 +18,7 @@ pub const Pair = struct {
// Zig API
pub fn check(v: Value) ?PairPtr {
- return @ptrCast(v.getPtr(.pair));
+ return v.getPtr(.pair);
}
pub fn assert(v: Value) PairPtr {
@@ -34,7 +34,7 @@ pub fn unpack(v: Value) PairPtr {
pub fn consInPool(pool: *PairPool, car: Value, cdr: Value) !Value {
const pair = try pool.cons(car, cdr);
- return ptr.pack(@ptrCast(pair), .pair);
+ return ptr.pack(.pair, pair);
}
// Zisp API
diff --git a/src/zisp/value/ptr.zig b/src/zisp/value/ptr.zig
index 8696056..e6f2639 100644
--- a/src/zisp/value/ptr.zig
+++ b/src/zisp/value/ptr.zig
@@ -31,19 +31,19 @@ pub fn assertWeak(v: Value) void {
}
}
-pub fn _pack(ptr: Zptr, tag: PtrTag, is_weak: bool) Value {
+pub fn _pack(comptime tag: PtrTag, ptr: tag.ptrType(), is_weak: bool) Value {
const ptr_val: usize = @intFromPtr(ptr);
std.debug.assert(ptr_val < std.math.maxInt(u48));
const tagged: u48 = @intCast(ptr_val | @intFromEnum(tag));
return .{ .ptr = .{ .tagged_value = tagged, .is_weak = is_weak } };
}
-pub fn pack(ptr: Zptr, tag: PtrTag) Value {
- return _pack(ptr, tag, false);
+pub fn pack(comptime tag: PtrTag, ptr: tag.ptrType()) Value {
+ return _pack(tag, ptr, false);
}
-pub fn packWeak(ptr: Zptr, tag: PtrTag) Value {
- return _pack(ptr, tag, true);
+pub fn packWeak(comptime tag: PtrTag, ptr: tag.ptrType()) Value {
+ return _pack(tag, ptr, true);
}
pub fn setWeakNull(v: *Value) void {