From 11dac6494d90c73545f0a9e03732c6ef7bad88b7 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Tue, 21 Sep 2021 00:28:31 +0200 Subject: Began implementing the stack and the stack stack --- pkg/stack/stack-stack.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 pkg/stack/stack-stack.go (limited to 'pkg/stack/stack-stack.go') diff --git a/pkg/stack/stack-stack.go b/pkg/stack/stack-stack.go new file mode 100644 index 0000000..db5a405 --- /dev/null +++ b/pkg/stack/stack-stack.go @@ -0,0 +1,44 @@ +package stack + +import ( + "git.adyxax.org/adyxax/gofunge/pkg/pointer" +) + +type StackStack struct { + head *Stack + height int +} + +func NewStackStack() *StackStack { + return &StackStack{ + head: NewStack(), + height: 1, + } +} + +func (ss *StackStack) Begin(p *pointer.Pointer) { + soss := ss.head + n := soss.Pop() + np := n + if np < 0 { + np = -np + } + toss := &Stack{ + size: np, + height: np, + data: make([]int, np), + next: soss, + } + ss.head = toss + max := n - soss.height + if max < 0 { + max = 0 + } + for i := n - 1; i >= max; i-- { + toss.data[i] = soss.data[soss.height-n+i] + } + x, y := p.GetStorageOffset() + soss.Push(x) + soss.Push(y) + p.CalculateNewStorageOffset() +} -- cgit v1.2.3