A cross-platform simulator for the GreenArrays GA144 multi-computer chip.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Use separate Return and Data stack structs

Comptime generics made for fairly DRY code, but still has some runtime
checks. I think it's OK to repeat yourself here for now.

Kent Smith a0243c79 bffc25e8

+53 -42
+53 -42
src/stack.zig
··· 1 - pub fn Stack(T: type, use_s: bool) type { 2 - return struct { 3 - t: T, 4 - s: T, 5 - stack: [8]T, 6 - index: u3 = 0, 1 + pub const DataStack = struct { 2 + t: Word, 3 + s: Word, 4 + stack: [8]Word, 5 + index: u3 = 0, 7 6 8 - pub fn push(self: *@This(), value: T) void { 9 - if (use_s) { 10 - self.stack[self.index] = self.s; 11 - self.s = self.t; 12 - } else { 13 - self.stack[self.index] = self.t; 14 - } 7 + pub fn push(self: *@This(), value: Word) void { 8 + self.stack[self.index] = self.s; 9 + self.s = self.t; 10 + 11 + self.t = value; 12 + self.index +%= 1; 13 + } 14 + 15 + pub fn pop(self: *@This()) Word { 16 + self.index -%= 1; 17 + const old_t = self.t; 18 + self.t = self.s; 19 + self.s = self.stack[self.index]; 20 + 21 + return old_t; 22 + } 23 + 24 + pub const empty: @This() = .{ 25 + .stack = @splat(0), 26 + .index = 0, 27 + .t = 0, 28 + .s = 0, 29 + }; 30 + }; 31 + 32 + pub const ReturnStack = struct { 33 + t: Word, 34 + stack: [8]Word, 35 + index: u3 = 0, 36 + 37 + pub fn push(self: *@This(), value: Word) void { 38 + self.stack[self.index] = self.t; 15 39 16 - self.t = value; 17 - self.index +%= 1; 18 - } 40 + self.t = value; 41 + self.index +%= 1; 42 + } 19 43 20 - pub fn pop(self: *@This()) T { 21 - self.index -%= 1; 22 - const old_t = self.t; 23 - if (use_s) { 24 - self.t = self.s; 25 - self.s = self.stack[self.index]; 26 - } else { 27 - self.t = self.stack[self.index]; 28 - } 44 + pub fn pop(self: *@This()) Word { 45 + self.index -%= 1; 46 + const old_t = self.t; 47 + self.t = self.stack[self.index]; 29 48 30 - return old_t; 31 - } 49 + return old_t; 50 + } 32 51 33 - pub const empty: @This() = .{ 34 - .stack = @splat(0), 35 - .index = 0, 36 - .t = 0, 37 - .s = 0, 38 - }; 52 + pub const empty: @This() = .{ 53 + .stack = @splat(0), 54 + .index = 0, 55 + .t = 0, 39 56 }; 40 - } 41 - pub const DataStack = Stack(Word, true); 42 - pub const ReturnStack = Stack(Word, false); 57 + }; 43 58 44 59 test "DataStack works" { 45 60 var stack: DataStack = .empty; ··· 68 83 try expectEqual(3, stack.index); 69 84 try expectEqual(55, stack.stack[stack.index - 1]); 70 85 86 + // pop 71 87 _ = stack.pop(); 72 - // pop 88 + 73 89 try expectEqual(123, stack.pop()); 74 90 try expectEqual(55, stack.t); 75 91 try expectEqual(0, stack.s); ··· 108 124 // initalized to zeroes 109 125 try expectEqual(stack.index, 0); 110 126 try expectEqual(0, stack.t); 111 - try expectEqual(0, stack.s); 112 127 113 128 // push 114 129 stack.push(55); 115 130 try expectEqual(55, stack.t); 116 - try expectEqual(0, stack.s); 117 131 try expectEqual(1, stack.index); 118 132 try expectEqual(0, stack.stack[0]); 119 133 120 134 stack.push(123); 121 135 try expectEqual(123, stack.t); 122 - try expectEqual(0, stack.s); 123 136 try expectEqual(2, stack.index); 124 137 try expectEqual(55, stack.stack[stack.index - 1]); 125 138 126 139 // pop 127 140 try expectEqual(123, stack.pop()); 128 141 try expectEqual(0, stack.stack[stack.index - 1]); 129 - try expectEqual(0, stack.s); 130 142 131 143 try expectEqual(55, stack.pop()); 132 144 try expectEqual(0, stack.index); 133 145 try expectEqual(0, stack.t); 134 - try expectEqual(0, stack.s); 135 146 136 147 // circular stack 137 148 stack.push(123);