diff options
| author | Taylan Kammer <taylan.kammer@gmail.com> | 2026-01-06 07:45:07 +0100 |
|---|---|---|
| committer | Taylan Kammer <taylan.kammer@gmail.com> | 2026-01-06 07:45:07 +0100 |
| commit | 75185f9fa26b5fc49036008df16c8bb4f1a51a13 (patch) | |
| tree | cb4f7a260f4e83bfbff302a926e12df2ae7007fa /src/zisp/io | |
| parent | e24978c647188c9b80563b535462a794e6eaa52b (diff) | |
Writergate, other 0.16 changes, and test fixes for new grammar.
Diffstat (limited to 'src/zisp/io')
| -rw-r--r-- | src/zisp/io/Parser.zig | 10 | ||||
| -rw-r--r-- | src/zisp/io/parser.zig | 5 | ||||
| -rw-r--r-- | src/zisp/io/unparser.zig | 30 |
3 files changed, 24 insertions, 21 deletions
diff --git a/src/zisp/io/Parser.zig b/src/zisp/io/Parser.zig index 57cdc14..2c381e1 100644 --- a/src/zisp/io/Parser.zig +++ b/src/zisp/io/Parser.zig @@ -119,7 +119,8 @@ pub const Alloc = struct { }; alloc: Alloc, -input: std.io.AnyReader = undefined, +io: std.Io, +input: *std.Io.Reader = undefined, context: Context = .{}, stack: List(Context) = undefined, chars: List(u8) = undefined, @@ -130,10 +131,11 @@ err_msg: []const u8 = undefined, pub fn init( alloc: Alloc, + io: std.Io, init_stack_capacity: usize, init_chars_capacity: usize, ) !Parser { - var p: Parser = .{ .alloc = alloc }; + var p: Parser = .{ .alloc = alloc, .io = io }; p.stack = try .initCapacity(alloc.stack, init_stack_capacity); p.chars = try .initCapacity(alloc.chars, init_chars_capacity); return p; @@ -155,7 +157,7 @@ fn read(p: *Parser) !?u8 { .{p.unread_char.?}, ); } - const c = p.input.readByte() catch |e| switch (e) { + const c = p.input.takeByte() catch |e| switch (e) { error.EndOfStream => return null, else => return p.err(.ReadError, "???"), }; @@ -242,7 +244,7 @@ fn call(p: *Parser, f: Fn) !void { }; } -pub fn run(p: *Parser, input: std.io.AnyReader) !Value { +pub fn run(p: *Parser, input: *std.Io.Reader) !Value { p.input = input; p.context.next = .parseUnit; while (p.context.next) |next| { diff --git a/src/zisp/io/parser.zig b/src/zisp/io/parser.zig index cfe2bf1..49d260c 100644 --- a/src/zisp/io/parser.zig +++ b/src/zisp/io/parser.zig @@ -44,17 +44,18 @@ pub fn initSfa(alloc: anytype) !Parser { const SfaType = @typeInfo(@TypeOf(alloc)).pointer.child; return Parser.init( alloc.allocator(), + std.Io.Threaded.global_single_threaded.io(), SfaType.init_stack_capacity, SfaType.init_chars_capacity, ); } -pub fn parse(input: std.io.AnyReader) Value { +pub fn parse(input: *std.Io.Reader) Value { return _parse(input, true); } pub fn _parse( - input: std.io.AnyReader, + input: *std.Io.Reader, comptime panic: bool, ) if (panic) Value else error{ ParseError, OutOfMemory }!Value { const alloc = std.heap.smp_allocator; diff --git a/src/zisp/io/unparser.zig b/src/zisp/io/unparser.zig index 64e18d0..04184fa 100644 --- a/src/zisp/io/unparser.zig +++ b/src/zisp/io/unparser.zig @@ -6,10 +6,9 @@ const value = @import("../value.zig"); const istr = value.istr; const seq = value.seq; -const ShortString = value.ShortString; const Value = value.Value; -pub fn unparse(w: anytype, v: Value) anyerror!void { +pub fn unparse(w: *std.Io.Writer, v: Value) anyerror!void { // zig fmt: off try if (v.isDouble()) unparseDouble(w, v) else if (v.isFixnum()) unparseFixnum(w, v) @@ -22,40 +21,41 @@ pub fn unparse(w: anytype, v: Value) anyerror!void { else if (v.isSstr()) unparseSstr(w, v) ; // zig fmt: on + try w.flush(); } -fn unparseDouble(w: anytype, v: Value) !void { +fn unparseDouble(w: *std.Io.Writer, v: Value) !void { _ = w; _ = v; @panic("not implemented"); } -fn unparseFixnum(w: anytype, v: Value) !void { +fn unparseFixnum(w: *std.Io.Writer, v: Value) !void { _ = w; _ = v; @panic("not implemented"); } -fn unparseRune(w: anytype, v: Value) !void { +fn unparseRune(w: *std.Io.Writer, v: Value) !void { const name = value.rune.unpack(v); try w.writeByte('#'); - try w.writeAll(name.constSlice()); + try w.writeAll(name.slice()); } -fn unparseSstr(w: anytype, v: Value) !void { +fn unparseSstr(w: *std.Io.Writer, v: Value) !void { // TODO: Check if pipes/escapes necessary. const str = value.sstr.unpack(v); - try w.writeAll(str.constSlice()); + try w.writeAll(str.slice()); } -fn unparseChar(w: anytype, v: Value) !void { +fn unparseChar(w: *std.Io.Writer, v: Value) !void { const uc: u21 = value.char.unpack(v); var buf: [4]u8 = undefined; const len = try std.unicode.utf8Encode(uc, &buf); try w.writeAll(buf[0..len]); } -fn unparseMisc(w: anytype, v: Value) !void { +fn unparseMisc(w: *std.Io.Writer, v: Value) !void { try switch (v.bits) { value.f.bits => w.writeAll("#f"), value.t.bits => w.writeAll("#t"), @@ -67,13 +67,13 @@ fn unparseMisc(w: anytype, v: Value) !void { }; } -fn unparseSrat(w: anytype, v: Value) !void { +fn unparseSrat(w: *std.Io.Writer, v: Value) !void { _ = w; _ = v; @panic("not implemented"); } -fn unparsePair(w: anytype, p: *[2]Value) !void { +fn unparsePair(w: *std.Io.Writer, p: *[2]Value) !void { try w.writeByte('('); try unparse(w, p[0]); var cdr = p[1]; @@ -83,21 +83,21 @@ fn unparsePair(w: anytype, p: *[2]Value) !void { } if (!value.nil.eq(cdr)) { try w.writeByte(' '); - try w.writeByte('.'); + try w.writeByte('&'); try w.writeByte(' '); try unparse(w, cdr); } try w.writeByte(')'); } -fn unparseSeq(w: anytype, s: *seq.Header) !void { +fn unparseSeq(w: *std.Io.Writer, s: *seq.Header) !void { switch (s.type) { .string => try unparseString(w, s), else => @panic("not implemented"), } } -fn unparseString(w: anytype, s: *seq.Header) !void { +fn unparseString(w: *std.Io.Writer, s: *seq.Header) !void { // TODO: Check if pipes/escapes necessary. try w.writeByte('|'); try w.writeAll(s.bytes()); |
