aboutsummaryrefslogtreecommitdiff
path: root/src/stack.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/stack.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/stack.zig24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/stack.zig b/src/stack.zig
index 1a70be1..e02cb20 100644
--- a/src/stack.zig
+++ b/src/stack.zig
@@ -123,6 +123,30 @@ pub const Stack = struct {
const fullResult = [_]i64{1};
try std.testing.expectEqualSlices(i64, full.data.items, fullResult[0..]);
}
+ pub fn discard(self: *Stack, n: u64) void {
+ // Implements a discard mechanism intended for use with the '}'(aka end) stackstack command
+ if (self.data.items.len > n) {
+ self.data.items.len -= n;
+ } else {
+ self.data.items.len = 0;
+ }
+ }
+ test "discard" {
+ var empty = try Stack.init(std.testing.allocator);
+ defer empty.deinit();
+ empty.discard(1);
+ const emptyResult = [_]i64{};
+ try std.testing.expectEqualSlices(i64, empty.data.items, emptyResult[0..]);
+ try empty.push(2);
+ empty.discard(3);
+ try std.testing.expectEqualSlices(i64, empty.data.items, emptyResult[0..]);
+ try empty.push(4);
+ try empty.push(5);
+ try empty.push(6);
+ empty.discard(1);
+ const emptyResult2 = [_]i64{ 4, 5 };
+ try std.testing.expectEqualSlices(i64, empty.data.items, emptyResult2[0..]);
+ }
};
test "all" {