From 362fdc1118ebe4699df05a4cbb0bbbe8f2a30a59 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 8 Aug 2022 21:41:55 +0200 Subject: Implemented the under funge command --- src/pointer.zig | 7 +++++-- src/stackStack.zig | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/pointer.zig b/src/pointer.zig index 7dbf566..7578a65 100644 --- a/src/pointer.zig +++ b/src/pointer.zig @@ -169,8 +169,11 @@ pub const Pointer = struct { p.reverse(); } }, - // TODO - 'u' => return error.NotImplemented, + 'u' => { + if (p.ss.under() catch true) { + p.reverse(); + } + }, 'g' => { const v = p.ss.toss.popVector(); try p.ss.toss.push(p.field.get(v[0] + p.sox, v[1] + p.soy)); diff --git a/src/stackStack.zig b/src/stackStack.zig index 208d85a..a76d304 100644 --- a/src/stackStack.zig +++ b/src/stackStack.zig @@ -119,6 +119,55 @@ pub const StackStack = struct { try std.testing.expectEqual(empty.end(), [2]i64{ 8, 9 }); try std.testing.expectEqualSlices(i64, empty.toss.data.items, tossResult[0..]); } + pub fn under(self: *StackStack) !bool { + if (self.data.items.len == 0) { + return true; + } + const n = self.toss.pop(); + var soss = self.data.items[self.data.items.len - 1]; + if (n > 0) { + var i: usize = 0; + while (i < n) : (i += 1) { + try self.toss.push(soss.pop()); + } + } else { + var i: usize = 0; + while (i < -n) : (i += 1) { + try soss.push(self.toss.pop()); + } + } + return false; + } + test "under" { + var empty = try StackStack.init(std.testing.allocator); + defer empty.deinit(); + try empty.toss.push(1); + try std.testing.expectEqual(empty.under(), true); + 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(5); + try empty.toss.push(6); + try empty.toss.push(0); + try empty.begin([2]i64{ 7, 8 }); + try empty.toss.push(9); + try empty.toss.push(0); + try std.testing.expectEqual(empty.under(), false); + const tossResult2 = [_]i64{9}; + try std.testing.expectEqualSlices(i64, empty.toss.data.items, tossResult2[0..]); + try empty.toss.push(2); + try std.testing.expectEqual(empty.under(), false); + const tossResult3 = [_]i64{ 9, 8, 7 }; + try std.testing.expectEqualSlices(i64, empty.toss.data.items, tossResult3[0..]); + try empty.toss.push(-1); + try std.testing.expectEqual(empty.under(), false); + const tossResult4 = [_]i64{ 9, 8 }; + const sossResult = [_]i64{ 1, 2, 3, 4, 5, 6, 7 }; + try std.testing.expectEqualSlices(i64, empty.toss.data.items, tossResult4[0..]); + try std.testing.expectEqualSlices(i64, empty.data.items[0].data.items, sossResult[0..]); + } pub inline fn toss(self: *StackStack) *stack.Stack { return self.toss; } -- cgit v1.2.3