From b5bfb6f025ce9dbbbd8353c6a019a0a428f4ddc6 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 7 Aug 2022 00:09:32 +0200 Subject: Implemented the } (aka end) funge command --- src/stack.zig | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/stack.zig') 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" { -- cgit v1.2.3