aboutsummaryrefslogtreecommitdiff
path: root/src/io.zig
diff options
context:
space:
mode:
authorJulien Dessaux2022-07-30 00:31:14 +0200
committerJulien Dessaux2022-07-30 00:31:14 +0200
commitaa56929ff7e03cd563d951782ba72e06be2a6935 (patch)
tree54501fa0e6af07581d998ebc5e2fe7fa373828a7 /src/io.zig
parentRemoved useless imports (diff)
downloadzigfunge98-aa56929ff7e03cd563d951782ba72e06be2a6935.tar.gz
zigfunge98-aa56929ff7e03cd563d951782ba72e06be2a6935.tar.bz2
zigfunge98-aa56929ff7e03cd563d951782ba72e06be2a6935.zip
Reworked io functions to take a configurable reader or writer
Diffstat (limited to '')
-rw-r--r--src/io.zig65
1 files changed, 30 insertions, 35 deletions
diff --git a/src/io.zig b/src/io.zig
index 549711d..5ec3b48 100644
--- a/src/io.zig
+++ b/src/io.zig
@@ -1,42 +1,37 @@
const std = @import("std");
-pub const IOErrors = error{
- IOError,
- NotImplemented,
-};
-
-pub const Functions = struct {
- characterInput: fn () IOErrors!i64,
- decimalInput: fn () IOErrors!i64,
- characterOutput: fn (i64) IOErrors!void,
- decimalOutput: fn (i64) IOErrors!void,
-};
-
-pub const defaultFunctions = Functions{
- .characterInput = characterInput,
- .decimalInput = decimalInput,
- .characterOutput = characterOutput,
- .decimalOutput = decimalOutput,
-};
-
-fn characterInput() IOErrors!i64 {
- // TODO
- return error.NotImplemented;
-}
-
-fn decimalInput() IOErrors!i64 {
- // TODO
- return error.NotImplemented;
-}
-
-fn characterOutput(v: i64) IOErrors!void {
- std.debug.print("{c}", .{@intCast(u8, v)});
- return;
+pub fn Context(readerType: anytype, writerType: anytype) type {
+ return struct {
+ reader: readerType,
+ writer: writerType,
+ pub fn characterInput(self: @This()) !i64 {
+ var buffer = [_]u8{0};
+ var n = try self.reader.read(buffer[0..]);
+ if (n == 1) {
+ return buffer[0];
+ }
+ return error.IOError;
+ }
+ pub fn decimalInput(self: @This()) !i64 {
+ _ = self;
+ return error.NotImplemented;
+ }
+ pub fn characterOutput(self: @This(), v: i64) !void {
+ try self.writer.print("{c}", .{@intCast(u8, v)});
+ return;
+ }
+ pub fn decimalOutput(self: @This(), v: i64) !void {
+ try self.writer.print("{d}", .{v});
+ return;
+ }
+ };
}
-fn decimalOutput(v: i64) IOErrors!void {
- std.debug.print("{d}", .{v});
- return;
+pub fn context(reader: anytype, writer: anytype) Context(@TypeOf(reader), @TypeOf(writer)) {
+ return .{
+ .reader = reader,
+ .writer = writer,
+ };
}
test "all" {