summaryrefslogtreecommitdiff
path: root/src/zisp/io/print.zig
diff options
context:
space:
mode:
authorTaylan Kammer <taylan.kammer@gmail.com>2026-06-06 19:28:59 +0200
committerTaylan Kammer <taylan.kammer@gmail.com>2026-06-06 19:28:59 +0200
commit416feebbf083ccdcab41fa2e0d4fc1bb0242562c (patch)
treef521f4601a0b6cf6663184c4cd781f453ecfea51 /src/zisp/io/print.zig
parenta3f25c0232283a6144646ca758ae2e166681bbb8 (diff)
Label printing.
Diffstat (limited to 'src/zisp/io/print.zig')
-rw-r--r--src/zisp/io/print.zig49
1 files changed, 38 insertions, 11 deletions
diff --git a/src/zisp/io/print.zig b/src/zisp/io/print.zig
index 890befd..56d54f7 100644
--- a/src/zisp/io/print.zig
+++ b/src/zisp/io/print.zig
@@ -131,28 +131,55 @@ fn getStr(s: *const Value) ![]const u8 {
}
pub fn label(w: Writer, v: Value) !void {
- _ = w;
- _ = v;
- @panic("not implemented");
+ 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 istr(w: Writer, p: IstrPtr) !void {
- // TODO: This assumes it needs no quoting
- try w.writeAll(p.str());
+ try string(w, p.str());
}
pub fn array(w: Writer, s: ArrayPtr) !void {
switch (s.type) {
- .string => try string(w, s),
+ .str => try string(w, s.str()),
else => @panic("not implemented"),
}
}
-pub fn string(w: Writer, s: ArrayPtr) !void {
- // TODO: Check if pipes/escapes necessary.
- try w.writeByte('|');
- try w.writeAll(s.str());
- try w.writeByte('|');
+pub fn string(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 list(w: Writer, p: PairPtr) !void {