diff options
Diffstat (limited to 'src/zisp/io')
| -rw-r--r-- | src/zisp/io/Parser.zig | 35 | ||||
| -rw-r--r-- | src/zisp/io/print.zig | 30 |
2 files changed, 48 insertions, 17 deletions
diff --git a/src/zisp/io/Parser.zig b/src/zisp/io/Parser.zig index ce61817..f92a308 100644 --- a/src/zisp/io/Parser.zig +++ b/src/zisp/io/Parser.zig @@ -152,7 +152,12 @@ fn read(p: *Parser) !?u8 { } fn readNoEof(p: *Parser, comptime emsg: []const u8) !u8 { - return if (try p.read()) |c| c else p.err(.UnexpectedEof, emsg); + return try p.read() orelse p.err(.UnexpectedEof, emsg); +} + +// Fake optional, for use in: while (readToEof()) |c| { } +fn readNoEof2(p: *Parser, comptime emsg: []const u8) !?u8 { + return try p.read() orelse p.err(.UnexpectedEof, emsg); } fn unread(p: *Parser, c: u8) void { @@ -187,12 +192,13 @@ fn addUnicode(p: *Parser, uc: u21) !void { fn getCharsAsString(p: *Parser) !Value { defer p.chars.clearRetainingCapacity(); const s = p.chars.items; - if (value.sstr.isValidSstr(s)) + if (value.sstr.isValidSstr(s)) { return value.sstr.pack(s); - else if (value.istr.isValidIstr(s)) + } else if (value.istr.isValidIstr(s)) { return value.istr.intern(s); - else - return @panic("not implemented"); // TODO + } else { + @panic("not implemented"); // TODO + } } fn getCharsAsRune(p: *Parser) Value { @@ -445,7 +451,7 @@ fn parseCladDatum(p: *Parser, c: u8, next: Fn) !void { fn getString(p: *Parser, comptime close: u8) !Value { const msg = "string(" ++ .{close} ++ ")"; - while (try p.readNoEof(msg)) |c| sw: switch (c) { + while (try p.readNoEof2(msg)) |c| sw: switch (c) { close => break, '\\' => switch (try p.readNoEof("string backslash escape")) { '\\', '|', '"' => |c2| try p.addChar(c2), @@ -475,33 +481,36 @@ fn getString(p: *Parser, comptime close: u8) !Value { fn getAtString(p: *Parser) !Value { const sentinel = try p.readNoEof("at-string"); - while (try p.readNoEof("at-string")) |c| { + while (try p.readNoEof2("at-string")) |c| { if (c == sentinel) break; try p.addChar(c); } - const s = p.getCharsAsString(); + const s = try p.getCharsAsString(); return p.cons(ATSTR, s); } fn skipStringLfEscape(p: *Parser) !u8 { const msg = "string linefeed escape"; - while (try p.readNoEof(msg)) |c| switch (c) { - '\t', ' ' => {}, + while (try p.readNoEof2(msg)) |c| switch (c) { + '\t', ' ' => continue, '\n' => return p.skipStringIndent(), else => return p.err(.InvalidCharacter, msg), }; + unreachable; } fn skipStringIndent(p: *Parser) !u8 { - while (try p.readNoEof("string newline escape")) |c| switch (c) { - '\t', ' ' => {}, + const msg = "string newline escape"; + while (try p.readNoEof2(msg)) |c| switch (c) { + '\t', ' ' => continue, else => return c, }; + unreachable; } fn parseStringRawHexEsc(p: *Parser) !void { const msg = "string raw hex escape"; - while (try p.readNoEof(msg)) |c1| { + while (try p.readNoEof2(msg)) |c1| { if (c1 == ';') break; const c2 = try p.readNoEof(msg); const hi = try p.parseHexDigit(c1, msg); diff --git a/src/zisp/io/print.zig b/src/zisp/io/print.zig index 65082c4..5e3bd15 100644 --- a/src/zisp/io/print.zig +++ b/src/zisp/io/print.zig @@ -3,14 +3,16 @@ const std = @import("std"); const gc = @import("../gc.zig"); const value = @import("../value.zig"); +const Parser = @import("Parser.zig"); const Value = value.Value; +const Array = value.array.Array; pub fn toWriter(w: *std.Io.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(.seq)) |p| seq(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) @@ -68,6 +70,26 @@ pub fn srat(w: *std.Io.Writer, v: Value) !void { } pub fn pair(w: *std.Io.Writer, p: *[2]Value) !void { + const car = p[0]; + //const cdr = p[1]; + if (car.eq(Parser.PQSTR)) { + //try pipeString(w, cdr); + @panic(""); + } else if (car.eq(Parser.DQSTR)) { + //try quotString(w, cdr); + @panic(""); + } else if (car.eq(Parser.ATSTR)) { + //try atString(w, cdr); + @panic(""); + } else if (car.eq(Parser.LABEL)) { + //try label(w, cdr); + @panic(""); + } else { + try list(w, p); + } +} + +pub fn list(w: *std.Io.Writer, p: *[2]Value) !void { try w.writeByte('('); try toWriter(w, p[0]); var cdr = p[1]; @@ -84,16 +106,16 @@ pub fn pair(w: *std.Io.Writer, p: *[2]Value) !void { try w.writeByte(')'); } -pub fn seq(w: *std.Io.Writer, s: *value.seq.Header) !void { +pub fn array(w: *std.Io.Writer, s: Array) !void { switch (s.type) { .string => try string(w, s), else => @panic("not implemented"), } } -pub fn string(w: *std.Io.Writer, s: *value.seq.Header) !void { +pub fn string(w: *std.Io.Writer, s: Array) !void { // TODO: Check if pipes/escapes necessary. try w.writeByte('|'); - try w.writeAll(s.bytes()); + try w.writeAll(s.str()); try w.writeByte('|'); } |
