aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/field.zig26
-rw-r--r--src/interpreter.zig4
-rw-r--r--src/io.zig2
-rw-r--r--src/main.zig7
-rw-r--r--src/pointer.zig27
5 files changed, 32 insertions, 34 deletions
diff --git a/src/field.zig b/src/field.zig
index 6c0e420..0130848 100644
--- a/src/field.zig
+++ b/src/field.zig
@@ -307,10 +307,10 @@ pub const Field = struct {
}
test "load" {
const crData = [_]u8{'v'} ** 4095 ++ "\r\n @";
- const cr = std.io.fixedBufferStream(crData).reader();
+ var cr = std.io.fixedBufferStream(crData);
var f = try Field.init(std.testing.allocator);
defer f.deinit();
- try f.load(cr);
+ try f.load(cr.reader());
try std.testing.expectEqual(f.x, 0);
try std.testing.expectEqual(f.y, 0);
try std.testing.expectEqual(f.lx, 4095);
@@ -318,11 +318,11 @@ pub const Field = struct {
try std.testing.expectEqual(f.lines.items[0].data.items[0], 'v');
try std.testing.expectEqual(f.lines.items[1].x, 1);
try std.testing.expectEqual(f.lines.items[1].data.items[0], '@');
- const cr2 = std.io.fixedBufferStream("v\r@").reader();
- try std.testing.expectEqual(f.load(cr2), error.FIELD_NOT_EMPTY);
+ var cr2 = std.io.fixedBufferStream("v\r@");
+ try std.testing.expectEqual(f.load(cr2.reader()), error.FIELD_NOT_EMPTY);
var f2 = try Field.init(std.testing.allocator);
defer f2.deinit();
- try std.testing.expectEqual(f2.load(cr2), error.GOT_CR_WITHOUT_LF);
+ try std.testing.expectEqual(f2.load(cr2.reader()), error.GOT_CR_WITHOUT_LF);
}
pub fn set(f: *Field, x: i64, y: i64, v: i64) !void {
if (v == ' ') return f.blank(x, y);
@@ -409,16 +409,16 @@ pub const Field = struct {
}
}
test "step" {
- const minimal = std.io.fixedBufferStream("@").reader();
+ var minimal = std.io.fixedBufferStream("@");
var f = try Field.init(std.testing.allocator);
defer f.deinit();
- try f.load(minimal);
+ try f.load(minimal.reader());
try std.testing.expectEqual(f.step(0, 0, 0, 0), .{ .x = 0, .y = 0 });
try std.testing.expectEqual(f.step(0, 0, 1, 0), .{ .x = 0, .y = 0 });
- const hello = std.io.fixedBufferStream("64+\"!dlroW ,olleH\">:#,_@\n").reader();
+ var hello = std.io.fixedBufferStream("64+\"!dlroW ,olleH\">:#,_@\n");
var fHello = try Field.init(std.testing.allocator);
defer fHello.deinit();
- try fHello.load(hello);
+ try fHello.load(hello.reader());
try std.testing.expectEqual(fHello.step(3, 0, 0, 0), .{ .x = 3, .y = 0 });
try std.testing.expectEqual(fHello.step(3, 0, 1, 0), .{ .x = 4, .y = 0 });
try std.testing.expectEqual(fHello.step(0, 0, -1, 0), .{ .x = 23, .y = 0 });
@@ -429,8 +429,8 @@ test "all" {
std.testing.refAllDecls(@This());
}
test "hello" {
- const hello = std.io.fixedBufferStream("64+\"!dlroW ,olleH\">:#,_@\n").reader();
- var f = try Field.init_from_reader(std.testing.allocator, hello);
+ var hello = std.io.fixedBufferStream("64+\"!dlroW ,olleH\">:#,_@\n");
+ var f = try Field.init_from_reader(std.testing.allocator, hello.reader());
defer f.deinit();
try std.testing.expectEqual(f.x, 0);
try std.testing.expectEqual(f.y, 0);
@@ -439,8 +439,8 @@ test "hello" {
try std.testing.expectEqual(f.lines.items[0].data.items[0], '6');
}
test "minimal" {
- const minimal = std.io.fixedBufferStream("@").reader();
- var f = try Field.init_from_reader(std.testing.allocator, minimal);
+ var minimal = std.io.fixedBufferStream("@");
+ var f = try Field.init_from_reader(std.testing.allocator, minimal.reader());
defer f.deinit();
try std.testing.expectEqual(f.x, 0);
try std.testing.expectEqual(f.y, 0);
diff --git a/src/interpreter.zig b/src/interpreter.zig
index 9fb98a1..760e008 100644
--- a/src/interpreter.zig
+++ b/src/interpreter.zig
@@ -13,13 +13,13 @@ pub const Interpreter = struct {
self.field.deinit();
self.allocator.destroy(self);
}
- pub fn init(allocator: std.mem.Allocator, fileReader: anytype, timestamp: fn () i64, args: []const []const u8, env: []const [*:0]const u8) !*Interpreter {
+ pub fn init(allocator: std.mem.Allocator, fileReader: anytype, timestamp: ?i64, args: []const []const u8, env: []const [*:0]const u8) !*Interpreter {
var i = try allocator.create(Interpreter);
errdefer allocator.destroy(i);
i.allocator = allocator;
i.field = try field.Field.init_from_reader(allocator, fileReader);
errdefer i.field.deinit();
- i.pointer = try pointer.Pointer.init(std.testing.allocator, i.field, timestamp, args, env);
+ i.pointer = try pointer.Pointer.init(allocator, i.field, timestamp, args, env);
errdefer i.pointer.deinit();
return i;
}
diff --git a/src/io.zig b/src/io.zig
index f33437f..54abcfc 100644
--- a/src/io.zig
+++ b/src/io.zig
@@ -1,6 +1,6 @@
const std = @import("std");
-pub fn Context(readerType: anytype, writerType: anytype) type {
+pub fn Context(comptime readerType: anytype, comptime writerType: anytype) type {
return struct {
reader: readerType,
writer: writerType,
diff --git a/src/main.zig b/src/main.zig
index 4830f45..7337100 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -16,16 +16,15 @@ pub fn main() anyerror!void {
defer file.close();
const env: []const [*:0]const u8 = std.os.environ;
- var i = try interpreter.Interpreter.init(gpa.allocator(), file.reader(), std.time.timestamp, args, env[0..]);
+ var i = try interpreter.Interpreter.init(gpa.allocator(), file.reader(), null, args, env[0..]);
defer i.deinit();
var ioContext = io.context(std.io.getStdIn().reader(), std.io.getStdOut().writer());
std.os.exit(@intCast(u8, try i.run(&ioContext)));
}
-fn testTimestamp() i64 {
- return 1660681247;
-}
+const testTimestamp: i64 = 1660681247;
+
test "all" {
std.testing.refAllDecls(@This());
}
diff --git a/src/pointer.zig b/src/pointer.zig
index 1f09c6c..0c516be 100644
--- a/src/pointer.zig
+++ b/src/pointer.zig
@@ -25,8 +25,8 @@ pub const Pointer = struct {
ss: *stackStack.StackStack,
env: []const [*:0]const u8,
argv: []const []const u8,
- rand: *std.rand.Random,
- timestamp: fn () i64,
+ rand: std.rand.DefaultPrng,
+ timestamp: ?i64,
pub fn deinit(self: *Pointer) void {
self.ss.deinit();
@@ -245,7 +245,8 @@ pub const Pointer = struct {
// 17
try p.ss.toss.push(@intCast(i64, p.ss.data.items.len) + 1);
// 16
- const now = std.time.epoch.EpochSeconds{ .secs = @intCast(u64, p.timestamp()) };
+ const ts = if (p.timestamp) |v| v else std.time.timestamp();
+ const now = std.time.epoch.EpochSeconds{ .secs = @intCast(u64, ts) };
const epochDay = now.getEpochDay();
const daySeconds = now.getDaySeconds();
try p.ss.toss.push(@intCast(i64, daySeconds.getHoursIntoDay()) * 256 * 256 + @intCast(i64, daySeconds.getMinutesIntoHour()) * 256 + @intCast(i64, daySeconds.getSecondsIntoMinute()));
@@ -349,7 +350,7 @@ pub const Pointer = struct {
self.step();
return result;
}
- pub fn init(allocator: std.mem.Allocator, f: *field.Field, timestamp: fn () i64, argv: []const []const u8, env: []const [*:0]const u8) !*Pointer {
+ pub fn init(allocator: std.mem.Allocator, f: *field.Field, timestamp: ?i64, argv: []const []const u8, env: []const [*:0]const u8) !*Pointer {
var p = try allocator.create(Pointer);
errdefer allocator.destroy(p);
p.allocator = allocator;
@@ -368,8 +369,7 @@ pub const Pointer = struct {
// Initializing the random number generator
var seed: u64 = undefined;
try std.os.getrandom(std.mem.asBytes(&seed));
- var prng = std.rand.DefaultPrng.init(seed);
- p.rand = &prng.random();
+ p.rand = std.rand.DefaultPrng.init(seed);
p.timestamp = timestamp;
return p;
}
@@ -393,7 +393,7 @@ pub const Pointer = struct {
},
'?' => {
const directions = [_]i8{ 0, -1, 1, 0, 0, 1, -1, 0 };
- const r = 2 * p.rand.intRangeAtMost(u8, 0, 3);
+ const r = 2 * p.rand.random().intRangeAtMost(u8, 0, 3);
p.dx = directions[r];
p.dy = directions[r + 1];
},
@@ -432,15 +432,14 @@ pub const Pointer = struct {
}
};
-fn testTimestamp() i64 {
- return 1660681247;
-}
+const testTimestamp: i64 = 1660681247;
+
test "all" {
std.testing.refAllDecls(@This());
}
test "minimal" {
- const minimal = std.io.fixedBufferStream("@").reader();
- var f = try field.Field.init_from_reader(std.testing.allocator, minimal);
+ var minimal = std.io.fixedBufferStream("@");
+ var f = try field.Field.init_from_reader(std.testing.allocator, minimal.reader());
defer f.deinit();
const argv = [_][]const u8{"minimal"};
const env = [_][*:0]const u8{"ENV=TEST"};
@@ -450,8 +449,8 @@ test "minimal" {
try std.testing.expectEqual(p.exec(&ioContext), pointerReturn{});
}
test "almost minimal" {
- const minimal = std.io.fixedBufferStream(" @").reader();
- var f = try field.Field.init_from_reader(std.testing.allocator, minimal);
+ var minimal = std.io.fixedBufferStream(" @");
+ var f = try field.Field.init_from_reader(std.testing.allocator, minimal.reader());
defer f.deinit();
const argv = [_][]const u8{"minimal"};
const env = [_][*:0]const u8{"ENV=TEST"};