aboutsummaryrefslogtreecommitdiff
path: root/stack.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-01-09 01:29:38 +0100
committerDimitri Sokolyuk <demon@dim13.org>2018-01-09 01:29:38 +0100
commitf8a2141c0776a28c6a7107849102154f9a959f48 (patch)
tree49c6743aeadea35218d5dfa7353b381baa3a9569 /stack.go
parent15eda60829ae0b4b084fe93bf419bf014a82a21c (diff)
guard stack and memory
Diffstat (limited to 'stack.go')
-rw-r--r--stack.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/stack.go b/stack.go
new file mode 100644
index 0000000..c192878
--- /dev/null
+++ b/stack.go
@@ -0,0 +1,36 @@
+package j1
+
+type stack struct {
+ data [stackSize]uint16 // stack
+ sp int8 // 5 bit stack pointer
+}
+
+func (s *stack) move(dir int8) {
+ s.sp = (s.sp + dir + stackSize) % stackSize
+}
+
+func (s *stack) push(v uint16) {
+ s.move(1)
+ s.set(v)
+}
+
+func (s *stack) pop() uint16 {
+ defer s.move(-1)
+ return s.peek()
+}
+
+func (s *stack) peek() uint16 {
+ return s.data[s.sp]
+}
+
+func (s *stack) set(v uint16) {
+ s.data[s.sp] = v
+}
+
+func (s *stack) depth() uint16 {
+ return uint16(s.sp)
+}
+
+func (s *stack) dump() []uint16 {
+ return s.data[:s.sp+1]
+}