aboutsummaryrefslogtreecommitdiff
path: root/pkg/stack/stack-stack.go
blob: db5a405c41696d99a2b14bf3596d19e048bf5877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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()
}