Refactored io functions handling
This commit is contained in:
parent
dc0952685e
commit
f7a5e4f52c
5 changed files with 55 additions and 46 deletions
|
@ -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());
|
||||
}
|
|
@ -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;
|
||||
|
|
44
src/io.zig
Normal file
44
src/io.zig
Normal file
|
@ -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());
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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{});
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue