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; const Printer = @This(); writer: *Writer, pub fn init(writer: *Writer) Printer { return .{ .writer = writer }; } fn write(p: *Printer, str: []const u8) !void { _ = try p.writer.write(str); } pub fn print(p: *Printer, v: Value) anyerror!void { if (v.isSstr()) return p.printBareStr(value.sstr.unpack(&v)); if (v.isRune()) return p.printRune(v); if (v.isMisc()) return p.printMisc(v); if (value.istr.check(v)) |str| return p.printBareStr(str.str()); if (value.array.check(.str, v)) |str| return p.printBareStr(str.str()); if (value.pair.check(v)) |pair| return p.printPair(pair); @panic("Unsupported type to print."); } pub fn printBareStr(p: *Printer, 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 p.writer.writeByte(c); } else if (no_joins and (c == '.' or c == ':')) { try p.writer.writeByte(c); } else { @panic("String needs quoting."); } } } pub fn printRune(p: *Printer, v: Value) !void { try p.write("#"); try p.write(value.rune.unpack(&v)); } pub fn printMisc(p: *Printer, v: Value) !void { try switch (v.bits) { value.f.bits => p.write("#f"), value.t.bits => p.write("#t"), value.nil.bits => p.write("()"), value.eof.bits => p.write("#EOF"), value.none.bits => p.write("#NONE"), value.undef.bits => p.write("#UNDEF"), else => @panic("Unsupported misc value to print."), }; } pub fn printPair(p: *Printer, pair: PairPtr) !void { try switch (pair.car.bits) { Parser.PQSTR.bits => p.printQuotString(pair.cdr, '|'), Parser.DQSTR.bits => p.printQuotString(pair.cdr, '"'), Parser.ATSTR.bits => p.printAtString(pair.cdr), Parser.LABEL.bits => p.printLabel(pair.cdr), else => p.printList(pair), }; } fn printQuotString(p: *Printer, s: Value, comptime qchar: u8) !void { try p.writer.writeByte(qchar); const str = try value.string.getString(&s); for (str) |c| switch (c) { qchar => { try p.writer.writeByte('\\'); try p.writer.writeByte(qchar); }, '\\' => { try p.writer.writeByte('\\'); try p.writer.writeByte('\\'); }, else => { try p.writer.writeByte(c); }, }; try p.writer.writeByte(qchar); } pub fn printAtString(p: *Printer, at_str_pair: Value) !void { const pair = value.pair.assert(at_str_pair); const byte = value.fixnum.unpack(pair.car); std.debug.assert(byte <= 255); const str = try value.string.getString(&pair.cdr); try p.write("@"); try p.writer.writeByte(@intCast(byte)); try p.write(str); try p.writer.writeByte(@intCast(byte)); } pub fn printLabel(p: *Printer, v: Value) !void { var num: Value = undefined; var dat: ?Value = undefined; if (value.pair.check(v)) |pair| { num = pair.car; dat = pair.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 p.write("#%"); try p.write(buf[0..end]); if (dat) |d| { try p.write("="); try p.print(d); } else { try p.write("%"); } } pub fn printList(p: *Printer, pair: PairPtr) !void { try p.write("("); try p.print(pair.car); var cdr = pair.cdr; while (value.pair.check(cdr)) |next| : (cdr = next.cdr) { try p.write(" "); try p.print(next.car); } if (!value.nil.eq(cdr)) { try p.write(" & "); try p.print(cdr); } try p.write(")"); }