From f7a5e4f52c529a74ca0e4ec5a269c4d594990802 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 12 Jun 2022 21:47:52 +0200 Subject: Refactored io functions handling --- src/defaultIO.zig | 30 ------------------------------ src/interpreter.zig | 3 ++- src/io.zig | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/main.zig | 5 +++-- src/pointer.zig | 19 ++++++------------- 5 files changed, 55 insertions(+), 46 deletions(-) delete mode 100644 src/defaultIO.zig create mode 100644 src/io.zig diff --git a/src/defaultIO.zig b/src/defaultIO.zig deleted file mode 100644 index 9da4ad2..0000000 --- a/src/defaultIO.zig +++ /dev/null @@ -1,30 +0,0 @@ -const std = @import("std"); - -pub const IOErrors = error{ - IOError, - NotImplemented, -}; - -pub fn characterInput() IOErrors!i64 { - // TODO - return error.NotImplemented; -} - -pub fn decimalInput() IOErrors!i64 { - // TODO - return error.NotImplemented; -} - -pub fn characterOutput(v: i64) IOErrors!void { - std.debug.print("{c}", .{@intCast(u8, v)}); - return; -} - -pub fn decimalOutput(v: i64) IOErrors!void { - std.debug.print("{d}", .{v}); - return; -} - -test "all" { - std.testing.refAllDecls(@This()); -} diff --git a/src/interpreter.zig b/src/interpreter.zig index 1ab07c9..a947d6f 100644 --- a/src/interpreter.zig +++ b/src/interpreter.zig @@ -1,5 +1,6 @@ const std = @import("std"); const field = @import("field.zig"); +const io = @import("io.zig"); const pointer = @import("pointer.zig"); pub const Interpreter = struct { @@ -12,7 +13,7 @@ pub const Interpreter = struct { self.field.deinit(); self.allocator.destroy(self); } - pub fn init(allocator: std.mem.Allocator, reader: anytype, ioFunctions: ?pointer.IOFunctions, args: []const []const u8) !*Interpreter { + pub fn init(allocator: std.mem.Allocator, reader: anytype, ioFunctions: io.Functions, args: []const []const u8) !*Interpreter { var i = try allocator.create(Interpreter); errdefer allocator.destroy(i); i.allocator = allocator; diff --git a/src/io.zig b/src/io.zig new file mode 100644 index 0000000..549711d --- /dev/null +++ b/src/io.zig @@ -0,0 +1,44 @@ +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; +} + +fn decimalOutput(v: i64) IOErrors!void { + std.debug.print("{d}", .{v}); + return; +} + +test "all" { + std.testing.refAllDecls(@This()); +} diff --git a/src/main.zig b/src/main.zig index 3bdf68d..6962c37 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,6 +1,7 @@ const std = @import("std"); const field = @import("field.zig"); const interpreter = @import("interpreter.zig"); +const io = @import("io.zig"); const pointer = @import("pointer.zig"); const stackStack = @import("stackStack.zig"); @@ -17,7 +18,7 @@ pub fn main() anyerror!void { var file = try std.fs.cwd().openFile("mycology/sanity.bf", .{}); defer file.close(); - var i = try interpreter.Interpreter.init(gpa.allocator(), file.reader(), null, args); + var i = try interpreter.Interpreter.init(gpa.allocator(), file.reader(), io.defaultFunctions, args); defer i.deinit(); std.os.exit(@intCast(u8, try i.run())); @@ -30,7 +31,7 @@ test "sanity" { var file = try std.fs.cwd().openFile("mycology/sanity.bf", .{}); defer file.close(); const args = [_][]const u8{"sanity"}; - var i = try interpreter.Interpreter.init(std.testing.allocator, file.reader(), null, args[0..]); + var i = try interpreter.Interpreter.init(std.testing.allocator, file.reader(), io.defaultFunctions, args[0..]); defer i.deinit(); try std.testing.expectEqual(try i.run(), 0); } diff --git a/src/pointer.zig b/src/pointer.zig index 62f8dee..9aae54e 100644 --- a/src/pointer.zig +++ b/src/pointer.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const defaultIO = @import("defaultIO.zig"); +const io = @import("io.zig"); const field = @import("field.zig"); const stackStack = @import("stackStack.zig"); @@ -11,13 +11,6 @@ const pointerReturn = struct { code: ?i64 = null, }; -pub const IOFunctions = struct { - characterInput: fn () defaultIO.IOErrors!i64 = defaultIO.characterInput, - decimalInput: fn () defaultIO.IOErrors!i64 = defaultIO.decimalInput, - characterOutput: fn (i64) defaultIO.IOErrors!void = defaultIO.characterOutput, - decimalOutput: fn (i64) defaultIO.IOErrors!void = defaultIO.decimalOutput, -}; - pub const Pointer = struct { allocator: std.mem.Allocator, field: *field.Field, @@ -30,7 +23,7 @@ pub const Pointer = struct { stringMode: bool = false, // string mode flags lastCharWasSpace: bool = false, ss: *stackStack.StackStack, - ioFunctions: IOFunctions, + ioFunctions: io.Functions, argv: []const []const u8, rand: *std.rand.Random, @@ -245,13 +238,13 @@ pub const Pointer = struct { self.step(); return result; } - pub fn init(allocator: std.mem.Allocator, f: *field.Field, ioFunctions: ?IOFunctions, argv: []const []const u8) !*Pointer { + pub fn init(allocator: std.mem.Allocator, f: *field.Field, ioFunctions: io.Functions, argv: []const []const u8) !*Pointer { var p = try allocator.create(Pointer); errdefer allocator.destroy(p); p.allocator = allocator; p.field = f; p.ss = try stackStack.StackStack.init(allocator); - p.ioFunctions = if (ioFunctions) |i| i else IOFunctions{}; + p.ioFunctions = ioFunctions; p.argv = argv; p.x = 0; p.y = 0; @@ -335,7 +328,7 @@ test "minimal" { var f = try field.Field.init_from_reader(std.testing.allocator, minimal); defer f.deinit(); const argv = [_][]const u8{"minimal"}; - var p = try Pointer.init(std.testing.allocator, f, null, argv[0..]); + var p = try Pointer.init(std.testing.allocator, f, io.defaultFunctions, argv[0..]); defer p.deinit(); try std.testing.expectEqual(p.exec(), pointerReturn{}); } @@ -344,7 +337,7 @@ test "almost minimal" { var f = try field.Field.init_from_reader(std.testing.allocator, minimal); defer f.deinit(); const argv = [_][]const u8{"minimal"}; - var p = try Pointer.init(std.testing.allocator, f, null, argv[0..]); + var p = try Pointer.init(std.testing.allocator, f, io.defaultFunctions, argv[0..]); defer p.deinit(); try std.testing.expectEqual(p.exec(), pointerReturn{}); } -- cgit v1.2.3