aboutsummaryrefslogtreecommitdiff
path: root/src/stackStack.zig
diff options
context:
space:
mode:
authorJulien Dessaux2022-08-07 00:09:32 +0200
committerJulien Dessaux2022-08-07 00:09:32 +0200
commitb5bfb6f025ce9dbbbd8353c6a019a0a428f4ddc6 (patch)
tree9bd9adc5a271e5b4b7c7002354b9457d156d5bb0 /src/stackStack.zig
parentImplemented the { (aka begin) funge command (diff)
downloadzigfunge98-b5bfb6f025ce9dbbbd8353c6a019a0a428f4ddc6.tar.gz
zigfunge98-b5bfb6f025ce9dbbbd8353c6a019a0a428f4ddc6.tar.bz2
zigfunge98-b5bfb6f025ce9dbbbd8353c6a019a0a428f4ddc6.zip
Implemented the } (aka end) funge command
Diffstat (limited to '')
-rw-r--r--src/stackStack.zig43
1 files changed, 42 insertions, 1 deletions
diff --git a/src/stackStack.zig b/src/stackStack.zig
index 11943a7..208d85a 100644
--- a/src/stackStack.zig
+++ b/src/stackStack.zig
@@ -25,7 +25,7 @@ pub const StackStack = struct {
pub fn begin(self: *StackStack, v: [2]i64) !void {
var soss = self.toss;
try self.data.append(soss);
- var n = soss.pop();
+ const n = soss.pop();
self.toss = try stack.Stack.init(self.allocator);
if (n > 0) {
try self.toss.transfert(soss, @intCast(u64, n));
@@ -78,6 +78,47 @@ pub const StackStack = struct {
try std.testing.expectEqualSlices(i64, empty.data.items[2].data.items, sossResult3[0..]);
try std.testing.expectEqualSlices(i64, empty.data.items[3].data.items, sossResult4[0..]);
}
+ pub fn end(self: *StackStack) !?[2]i64 {
+ // Implements the '}' command behaviour which pops a stack from the stack stack
+ // returns null if a reflect should happen, a storage offset vector otherwise
+ if (self.data.popOrNull()) |soss| {
+ const n = self.toss.pop();
+ const v = soss.popVector();
+ if (n > 0) {
+ try soss.transfert(self.toss, @intCast(u64, n));
+ } else {
+ soss.discard(@intCast(u64, -n));
+ }
+ self.toss.deinit();
+ self.toss = soss;
+ return v;
+ } else {
+ return null;
+ }
+ }
+ test "end" {
+ var empty = try StackStack.init(std.testing.allocator);
+ defer empty.deinit();
+ try empty.toss.push(1);
+ try std.testing.expectEqual(empty.end(), null);
+ const tossResult = [_]i64{1};
+ try std.testing.expectEqualSlices(i64, empty.toss.data.items, tossResult[0..]);
+ try empty.toss.push(2);
+ try empty.toss.push(3);
+ try empty.toss.push(4);
+ try empty.toss.push(2);
+ try empty.begin([2]i64{ 5, 6 });
+ try empty.toss.push(7);
+ try empty.toss.push(2);
+ try std.testing.expectEqual(empty.end(), [2]i64{ 5, 6 });
+ const tossResult2 = [_]i64{ 1, 2, 4, 7 };
+ try std.testing.expectEqualSlices(i64, empty.toss.data.items, tossResult2[0..]);
+ try empty.toss.push(1);
+ try empty.begin([2]i64{ 8, 9 });
+ try empty.toss.push(-2);
+ try std.testing.expectEqual(empty.end(), [2]i64{ 8, 9 });
+ try std.testing.expectEqualSlices(i64, empty.toss.data.items, tossResult[0..]);
+ }
pub inline fn toss(self: *StackStack) *stack.Stack {
return self.toss;
}