diff options
Diffstat (limited to 'src/zisp/io.zig')
| -rw-r--r-- | src/zisp/io.zig | 58 |
1 files changed, 36 insertions, 22 deletions
diff --git a/src/zisp/io.zig b/src/zisp/io.zig index 0a7e5dd..f0d0a8f 100644 --- a/src/zisp/io.zig +++ b/src/zisp/io.zig @@ -1,5 +1,6 @@ const std = @import("std"); +const gc = @import("gc.zig"); const value = @import("value.zig"); const Alloc = std.mem.Allocator; @@ -14,6 +15,34 @@ pub const Printer = @import("io/Printer.zig"); pub const Decoder = @import("io/Decoder.zig"); pub const Encoder = @import("io/Encoder.zig"); +var main_io: std.Io = undefined; +var main_decoder: Decoder = undefined; +var main_encoder: Encoder = undefined; + +var init_done = false; + +pub fn init() !void { + if (init_done) return; + defer init_done = true; + main_io = std.Io.Threaded.global_single_threaded.io(); + main_decoder = try .init(gc.mainAlloc()); + main_encoder = try .init(gc.mainAlloc()); +} + +pub fn mainIo() std.Io { + return main_io; +} + +pub fn mainDecoder() *Decoder { + init() catch @panic("OOM"); // TODO this is only here for the test suite + return &main_decoder; +} + +pub fn mainEncoder() *Encoder { + init() catch @panic("OOM"); // TODO this is only here for the test suite + return &main_encoder; +} + // Parse const ParserErrors = error{ @@ -51,26 +80,11 @@ pub fn print(writer: *Writer, v: Value) !void { try p.print(v); } -// Decode - -pub fn decode(v: Value) !Value { - _ = v; - @panic("not implemented"); -} - -// Encode - -pub fn encode(v: Value) !Value { - _ = v; - @panic("not implemented"); -} - -// Read & Write - -pub fn read(alloc: Alloc, reader: *Reader) !Value { - return decode(try parse(alloc, reader)); -} - -pub fn write(v: Value) !void { - return print(try encode(v)); +pub fn printStdout(v: Value) !void { + var stdout_buffer: [4096]u8 = undefined; + var stdout_writer = std.Io.File.stdout().writer(main_io, &stdout_buffer); + const writer = &stdout_writer.interface; + try print(writer, v); + try writer.writeByte('\n'); + try writer.flush(); } |
