summaryrefslogtreecommitdiff
path: root/src/zisp/io/unparser.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/zisp/io/unparser.zig')
-rw-r--r--src/zisp/io/unparser.zig98
1 files changed, 40 insertions, 58 deletions
diff --git a/src/zisp/io/unparser.zig b/src/zisp/io/unparser.zig
index e72f130..64e18d0 100644
--- a/src/zisp/io/unparser.zig
+++ b/src/zisp/io/unparser.zig
@@ -1,23 +1,27 @@
const std = @import("std");
+const gc = @import("../gc.zig");
const value = @import("../value.zig");
const istr = value.istr;
const seq = value.seq;
const ShortString = value.ShortString;
-const OtherTag = value.OtherTag;
const Value = value.Value;
pub fn unparse(w: anytype, v: Value) anyerror!void {
- try if (value.double.check(v))
- unparseDouble(w, v)
- else if (value.fixnum.check(v))
- unparseFixnum(w, v)
- else if (value.ptr.checkZisp(v))
- unparseHeap(w, v)
- else
- unparseOther(w, v);
+ // zig fmt: off
+ try if (v.isDouble()) unparseDouble(w, v)
+ else if (v.isFixnum()) unparseFixnum(w, v)
+ else if (v.getPtr(.pair)) |p| unparsePair(w, @ptrCast(p))
+ else if (v.getPtr(.seq)) |p| unparseSeq(w, @ptrCast(p))
+ else if (v.isRune()) unparseRune(w, v)
+ else if (v.isChar()) unparseChar(w, v)
+ else if (v.isMisc()) unparseMisc(w, v)
+ else if (v.isSrat()) unparseSrat(w, v)
+ else if (v.isSstr()) unparseSstr(w, v)
+ ;
+ // zig fmt: on
}
fn unparseDouble(w: anytype, v: Value) !void {
@@ -32,25 +36,6 @@ fn unparseFixnum(w: anytype, v: Value) !void {
@panic("not implemented");
}
-fn unparseHeap(w: anytype, v: Value) !void {
- const p, const t = value.ptr.unpack(v);
- try switch (t) {
- .pair => unparsePair(w, @ptrCast(p)),
- .seq => unparseSeq(w, @ptrCast(p)),
- else => @panic("not implemented"),
- };
-}
-
-fn unparseOther(w: anytype, v: Value) !void {
- try switch (v.other.tag) {
- .rune => unparseRune(w, v),
- .sstr => unparseSstr(w, v),
- .qstr => unparseQstr(w, v),
- .char => unparseChar(w, v),
- .misc => unparseMisc(w, v),
- };
-}
-
fn unparseRune(w: anytype, v: Value) !void {
const name = value.rune.unpack(v);
try w.writeByte('#');
@@ -58,41 +43,43 @@ fn unparseRune(w: anytype, v: Value) !void {
}
fn unparseSstr(w: anytype, v: Value) !void {
+ // TODO: Check if pipes/escapes necessary.
const str = value.sstr.unpack(v);
try w.writeAll(str.constSlice());
}
-fn unparseQstr(w: anytype, v: Value) !void {
- const str = value.sstr.unpack(v);
- try w.writeByte('"');
- try w.writeAll(str.constSlice());
- try w.writeByte('"');
-}
-
fn unparseChar(w: anytype, v: Value) !void {
+ const uc: u21 = value.char.unpack(v);
var buf: [4]u8 = undefined;
- const len = try std.unicode.utf8Encode(v.char.value, &buf);
+ const len = try std.unicode.utf8Encode(uc, &buf);
try w.writeAll(buf[0..len]);
}
fn unparseMisc(w: anytype, v: Value) !void {
- try switch (v.misc.value) {
- .f => w.writeAll("#f"),
- .t => w.writeAll("#t"),
- .nil => w.writeAll("()"),
- .eof => w.writeAll("#EOF"),
- .none => w.writeAll("#NONE"),
- .undef => w.writeAll("#UNDEF"),
+ try switch (v.bits) {
+ value.f.bits => w.writeAll("#f"),
+ value.t.bits => w.writeAll("#t"),
+ value.nil.bits => w.writeAll("()"),
+ value.eof.bits => w.writeAll("#EOF"),
+ value.none.bits => w.writeAll("#NONE"),
+ value.undef.bits => w.writeAll("#UNDEF"),
+ else => @panic("not implemented"),
};
}
+fn unparseSrat(w: anytype, v: Value) !void {
+ _ = w;
+ _ = v;
+ @panic("not implemented");
+}
+
fn unparsePair(w: anytype, p: *[2]Value) !void {
try w.writeByte('(');
try unparse(w, p[0]);
var cdr = p[1];
- while (value.pair.check(cdr)) : (cdr = value.pair.cdr(cdr)) {
+ while (value.pair.check(cdr)) |p2| : (cdr = value.pair.cdr(cdr)) {
try w.writeByte(' ');
- try unparse(w, value.pair.car(cdr));
+ try unparse(w, p2[0]);
}
if (!value.nil.eq(cdr)) {
try w.writeByte(' ');
@@ -103,21 +90,16 @@ fn unparsePair(w: anytype, p: *[2]Value) !void {
try w.writeByte(')');
}
-fn unparseSeq(w: anytype, p: *seq.Header) !void {
- const h = istr.getHeaderFromPtr(@ptrCast(p));
- switch (h.type) {
- .string => try unparseString(w, h),
+fn unparseSeq(w: anytype, s: *seq.Header) !void {
+ switch (s.type) {
+ .string => try unparseString(w, s),
else => @panic("not implemented"),
}
}
-fn unparseString(w: anytype, h: *seq.Header) !void {
- const info = h.info.string;
- if (info.quoted) {
- try w.writeByte('"');
- }
- try w.writeAll(h.bytes());
- if (info.quoted) {
- try w.writeByte('"');
- }
+fn unparseString(w: anytype, s: *seq.Header) !void {
+ // TODO: Check if pipes/escapes necessary.
+ try w.writeByte('|');
+ try w.writeAll(s.bytes());
+ try w.writeByte('|');
}