aboutsummaryrefslogtreecommitdiff
path: root/pkg/stack/stack.go
diff options
context:
space:
mode:
authorJulien Dessaux2021-09-21 00:28:31 +0200
committerJulien Dessaux2021-09-21 00:28:31 +0200
commit11dac6494d90c73545f0a9e03732c6ef7bad88b7 (patch)
tree992e9d0c69c7e1cb26f85ee59c5234bba1d3e753 /pkg/stack/stack.go
parentContinued implementing the Instruction Pointer (diff)
downloadgofunge98-11dac6494d90c73545f0a9e03732c6ef7bad88b7.tar.gz
gofunge98-11dac6494d90c73545f0a9e03732c6ef7bad88b7.tar.bz2
gofunge98-11dac6494d90c73545f0a9e03732c6ef7bad88b7.zip
Began implementing the stack and the stack stack
Diffstat (limited to '')
-rw-r--r--pkg/stack/stack.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/pkg/stack/stack.go b/pkg/stack/stack.go
new file mode 100644
index 0000000..4dbb72c
--- /dev/null
+++ b/pkg/stack/stack.go
@@ -0,0 +1,51 @@
+package stack
+
+type Stack struct {
+ size int
+ height int
+ data []int
+ next *Stack // Pointer to the next element in the stack stack
+}
+
+func NewStack() *Stack {
+ return &Stack{
+ size: 32,
+ height: 0,
+ data: make([]int, 32),
+ next: nil,
+ }
+}
+
+func (s *Stack) Clear() {
+ s.height = 0
+}
+
+func (s *Stack) Duplicate() {
+ if s.height > 0 {
+ s.Push(s.data[s.height-1])
+ }
+}
+
+func (s *Stack) Pop() int {
+ if s.height > 0 {
+ s.height--
+ return s.data[s.height]
+ }
+ return 0
+}
+
+func (s *Stack) Push(value int) {
+ if s.height >= s.size {
+ s.size += 32
+ s.data = append(s.data, make([]int, 32)...)
+ }
+ s.data[s.height] = value
+ s.height++
+}
+
+func (s *Stack) Swap() {
+ a := s.Pop()
+ b := s.Pop()
+ s.Push(a)
+ s.Push(b)
+}