aboutsummaryrefslogtreecommitdiff
path: root/stack.go
blob: da87aa9b223f51dc32d44389819819ab37816545 (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
package j1

type stack struct {
	data [0x20]uint16 // stack
	sp   int8         // 5 bit stack pointer
}

func (s *stack) move(dir int8) {
	s.sp = (s.sp + dir) & int8(len(s.data)-1)
}

func (s *stack) push(v uint16) {
	s.sp = (s.sp + 1) & int8(len(s.data)-1)
	s.data[s.sp] = v
}

func (s *stack) pop() uint16 {
	sp := s.sp
	s.sp = (s.sp - 1) & int8(len(s.data)-1)
	return s.data[sp]
}

func (s *stack) peek() uint16 {
	return s.data[s.sp]
}

func (s *stack) replace(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]
}