summaryrefslogtreecommitdiff
path: root/src/zisp/io/print.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/zisp/io/print.zig')
-rw-r--r--src/zisp/io/print.zig137
1 files changed, 0 insertions, 137 deletions
diff --git a/src/zisp/io/print.zig b/src/zisp/io/print.zig
deleted file mode 100644
index e5caab7..0000000
--- a/src/zisp/io/print.zig
+++ /dev/null
@@ -1,137 +0,0 @@
-const std = @import("std");
-
-const Writer = *std.Io.Writer;
-
-const io = @import("../io.zig");
-const value = @import("../value.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 {
- if (v.isSstr()) return printBareStr(w, value.sstr.unpack(&v));
- if (v.isRune()) return printRune(w, v);
- if (v.isMisc()) return printMisc(w, v);
- if (value.istr.check(v)) |p| return printBareStr(w, p.str());
- if (value.array.check(.str, v)) |p| return printBareStr(w, p.str());
- if (value.pair.check(v)) |p| return printPair(w, p);
- @panic("Unsupported type to print.");
-}
-
-pub fn printBareStr(w: Writer, s: []const u8) !void {
- if (s.len == 0) @panic("Empty string must be quoted.");
- const no_joins = Parser.isSpecialBareChar(s[0]);
- for (s) |c| {
- if (Parser.isBareChar(c)) {
- try w.writeByte(c);
- } else if (no_joins and (c == '.' or c == ':')) {
- try w.writeByte(c);
- } else {
- @panic("String needs quoting.");
- }
- }
-}
-
-pub fn printRune(w: Writer, v: Value) !void {
- try w.writeByte('#');
- try w.writeAll(value.rune.unpack(&v));
-}
-
-pub fn printMisc(w: Writer, v: Value) !void {
- 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("Unsupported misc value to print."),
- };
-}
-
-pub fn printPair(w: Writer, p: PairPtr) !void {
- try switch (p.car.bits) {
- Parser.PQSTR.bits => printQuotString(w, p.cdr, '|'),
- Parser.DQSTR.bits => printQuotString(w, p.cdr, '"'),
- Parser.ATSTR.bits => printAtString(w, p.cdr),
- Parser.LABEL.bits => printLabel(w, p.cdr),
- else => printList(w, p),
- };
-}
-
-fn printQuotString(w: Writer, s: Value, comptime qchar: u8) !void {
- try w.writeByte(qchar);
- const str = try value.string.getString(&s);
- for (str) |c| switch (c) {
- qchar => {
- try w.writeByte('\\');
- try w.writeByte(qchar);
- },
- '\\' => {
- try w.writeByte('\\');
- try w.writeByte('\\');
- },
- else => {
- try w.writeByte(c);
- },
- };
- try w.writeByte(qchar);
-}
-
-pub fn printAtString(w: Writer, at_str_pair: Value) !void {
- const p = value.pair.assert(at_str_pair);
- const b = value.fixnum.unpack(p.car);
- std.debug.assert(b <= 255);
- const str = try value.string.getString(&p.cdr);
- try w.writeByte('@');
- try w.writeByte(@intCast(b));
- try w.writeAll(str);
- try w.writeByte(@intCast(b));
-}
-
-pub fn printLabel(w: Writer, v: Value) !void {
- var num: Value = undefined;
- var dat: ?Value = undefined;
- if (value.pair.check(v)) |p| {
- num = p.car;
- dat = p.cdr;
- } else {
- num = v;
- dat = null;
- }
-
- const x = value.fixnum.unpack(num);
- std.debug.assert(x >= 0);
- std.debug.assert(x <= std.math.maxInt(u48));
- var buf: [12]u8 = undefined;
- const end = std.fmt.printInt(&buf, x, 16, .lower, .{});
-
- try w.writeAll("#%");
- try w.writeAll(buf[0..end]);
- if (dat) |d| {
- try w.writeByte('=');
- try toWriter(w, d);
- } else {
- try w.writeByte('%');
- }
-}
-
-pub fn printList(w: Writer, p: PairPtr) !void {
- try w.writeByte('(');
- try toWriter(w, p.car);
- var cdr = p.cdr;
- while (value.pair.check(cdr)) |p2| : (cdr = p2.cdr) {
- try w.writeByte(' ');
- try toWriter(w, p2.car);
- }
- if (!value.nil.eq(cdr)) {
- try w.writeByte(' ');
- try w.writeByte('&');
- try w.writeByte(' ');
- try toWriter(w, cdr);
- }
- try w.writeByte(')');
-}