aboutsummaryrefslogtreecommitdiff
path: root/pkg/pointer/mycology_test.go
blob: 0fec52f2ba422ccea4cba4f5dc0bc0c307f2ed5d (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
package pointer

import (
	"fmt"
	"os"
	"testing"

	"git.adyxax.org/adyxax/gofunge98/pkg/field"
	"github.com/stretchr/testify/require"
)

func TestMycology(t *testing.T) {
	file, err := os.Open("../../mycology/mycology.b98")
	if err != nil {
		t.Skip("mycology test suite not found, skipping")
	}

	f, err := field.Load(file)
	require.NoError(t, err)
	p := NewPointer()
	p.Argv = []string{"../../mycology/mycology.b98"}
	output := ""
	// TODO test expected output
	p.CharacterOutput = func(c int) {
		output += fmt.Sprintf("%c", c)
	}
	i := NewInterpreter(f, p)

	v := i.Run()
	require.Equal(t, 15, v)
}

type Interpreter struct {
	f *field.Field
	p *Pointer
}

func NewInterpreter(f *field.Field, p *Pointer) *Interpreter {
	return &Interpreter{f: f, p: p}
}

func (i *Interpreter) Run() int {
	for i.p != nil {
		if v := i.step(); v != nil {
			return *v
		}
	}
	return 0
}

func (i *Interpreter) step() *int {
	var prev *Pointer = nil
	for p := i.p; p != nil; p = p.Next {
		done, v := p.Exec(i.f)
		if v != nil {
			return v
		}
		if done {
			if prev == nil {
				i.p = p.Next
			} else {
				prev.Next = p.Next
			}
		}
	}
	return nil
}