aboutsummaryrefslogtreecommitdiff
path: root/pkg/pointer/pointer.go
blob: 1044cff863ba29142d64bb28ea3c347b6e09292c (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package pointer

import (
	"math/rand"
	"time"

	"git.adyxax.org/adyxax/gofunge98/pkg/field"
)

var myRand = rand.New(rand.NewSource(time.Now().UnixNano()))

type InputFunction func() (int, error)
type OutputFunction func(v int)

type Pointer struct {
	// the position
	x int
	y int
	// The delta
	dx int
	dy int
	// The Storage offset
	sox int
	soy int
	// The stringmode flag
	stringMode       bool
	lastCharWasSpace bool
	// The stack
	ss *StackStack
	// The next element for the multi-"threaded" b98 interpreter
	Next *Pointer
	// The input/output functions
	CharacterInput  InputFunction
	DecimalInput    InputFunction
	CharacterOutput OutputFunction
	DecimalOutput   OutputFunction
	// command line arguments
	Argv []string
}

func NewPointer() *Pointer {
	return &Pointer{
		dx:              1,
		ss:              NewStackStack(),
		CharacterInput:  DefaultCharacterInput,
		DecimalInput:    DefaultDecimalInput,
		CharacterOutput: DefaultCharacterOutput,
		DecimalOutput:   DefaultDecimalOutput,
	}
}

func (p Pointer) Split() *Pointer {
	return &p // p is already a copy TODO we need to duplicate the stack and handle the Next
}

func (p *Pointer) Step(f field.Field) {
	p.x, p.y = f.Step(p.x, p.y, p.dx, p.dy)
}

func (p Pointer) Get(f field.Field) int {
	return f.Get(p.x, p.y)
}

func (p *Pointer) StepAndGet(f field.Field) int {
	p.Step(f)
	return p.Get(f)
}

func (p *Pointer) Set(x, y int) {
	p.x, p.y = x, y
}

func (p *Pointer) RedirectTo(dx, dy int) {
	p.dx, p.dy = dx, dy
}

func (p *Pointer) Reverse() {
	p.dx, p.dy = -p.dx, -p.dy
}

func (p *Pointer) Redirect(c int) bool {
	switch c {
	case '^':
		p.dx, p.dy = 0, -1
	case '>':
		p.dx, p.dy = 1, 0
	case 'v':
		p.dx, p.dy = 0, 1
	case '<':
		p.dx, p.dy = -1, 0
	case '?':
		directions := []int{0, -1, 1, 0, 0, 1, -1, 0}
		r := 2 * myRand.Intn(4)
		p.dx, p.dy = directions[r], directions[r+1]
	case '[':
		p.dx, p.dy = p.dy, -p.dx
	case ']':
		p.dx, p.dy = -p.dy, p.dx
	case 'r':
		p.Reverse()
	case 'x':
		p.dy = p.ss.head.Pop()
		p.dx = p.ss.head.Pop()
	default:
		return false
	}
	return true
}