Skip to content

Commit a0243c7

Browse files
committed
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.
1 parent bffc25e commit a0243c7

File tree

1 file changed

+58
-47
lines changed

1 file changed

+58
-47
lines changed

src/stack.zig

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,60 @@
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,
7-
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-
}
15-
16-
self.t = value;
17-
self.index +%= 1;
18-
}
19-
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-
}
29-
30-
return old_t;
31-
}
32-
33-
pub const empty: @This() = .{
34-
.stack = @splat(0),
35-
.index = 0,
36-
.t = 0,
37-
.s = 0,
38-
};
1+
pub const DataStack = struct {
2+
t: Word,
3+
s: Word,
4+
stack: [8]Word,
5+
index: u3 = 0,
6+
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,
3929
};
40-
}
41-
pub const DataStack = Stack(Word, true);
42-
pub const ReturnStack = Stack(Word, false);
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;
39+
40+
self.t = value;
41+
self.index +%= 1;
42+
}
43+
44+
pub fn pop(self: *@This()) Word {
45+
self.index -%= 1;
46+
const old_t = self.t;
47+
self.t = self.stack[self.index];
48+
49+
return old_t;
50+
}
51+
52+
pub const empty: @This() = .{
53+
.stack = @splat(0),
54+
.index = 0,
55+
.t = 0,
56+
};
57+
};
4358

4459
test "DataStack works" {
4560
var stack: DataStack = .empty;
@@ -68,8 +83,9 @@ test "DataStack works" {
6883
try expectEqual(3, stack.index);
6984
try expectEqual(55, stack.stack[stack.index - 1]);
7085

71-
_ = stack.pop();
7286
// pop
87+
_ = stack.pop();
88+
7389
try expectEqual(123, stack.pop());
7490
try expectEqual(55, stack.t);
7591
try expectEqual(0, stack.s);
@@ -108,30 +124,25 @@ test ReturnStack {
108124
// initalized to zeroes
109125
try expectEqual(stack.index, 0);
110126
try expectEqual(0, stack.t);
111-
try expectEqual(0, stack.s);
112127

113128
// push
114129
stack.push(55);
115130
try expectEqual(55, stack.t);
116-
try expectEqual(0, stack.s);
117131
try expectEqual(1, stack.index);
118132
try expectEqual(0, stack.stack[0]);
119133

120134
stack.push(123);
121135
try expectEqual(123, stack.t);
122-
try expectEqual(0, stack.s);
123136
try expectEqual(2, stack.index);
124137
try expectEqual(55, stack.stack[stack.index - 1]);
125138

126139
// pop
127140
try expectEqual(123, stack.pop());
128141
try expectEqual(0, stack.stack[stack.index - 1]);
129-
try expectEqual(0, stack.s);
130142

131143
try expectEqual(55, stack.pop());
132144
try expectEqual(0, stack.index);
133145
try expectEqual(0, stack.t);
134-
try expectEqual(0, stack.s);
135146

136147
// circular stack
137148
stack.push(123);

0 commit comments

Comments
 (0)