From 46170dd5b70588c9b1c6f4aa5aaea2649d8c74f0 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 20 Sep 2021 01:23:39 +0200 Subject: Began implementing the Instruction Pointer --- pkg/pointer/delta.go | 10 ++++++++++ pkg/pointer/pointer.go | 21 +++++++++++++++++++++ pkg/pointer/pointer_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 pkg/pointer/delta.go create mode 100644 pkg/pointer/pointer.go create mode 100644 pkg/pointer/pointer_test.go (limited to 'pkg') diff --git a/pkg/pointer/delta.go b/pkg/pointer/delta.go new file mode 100644 index 0000000..0c3e074 --- /dev/null +++ b/pkg/pointer/delta.go @@ -0,0 +1,10 @@ +package pointer + +type Delta struct { + x int + y int +} + +func NewDelta(x, y int) *Delta { + return &Delta{x: x, y: y} +} diff --git a/pkg/pointer/pointer.go b/pkg/pointer/pointer.go new file mode 100644 index 0000000..5b4b860 --- /dev/null +++ b/pkg/pointer/pointer.go @@ -0,0 +1,21 @@ +package pointer + +import "git.adyxax.org/adyxax/gofunge/pkg/field" + +type Pointer struct { + x int + y int + delta *Delta +} + +func NewPointer() *Pointer { + return &Pointer{delta: NewDelta(1, 0)} +} + +func (p Pointer) ForkPointer() *Pointer { + return &p // p is already a copy +} + +func (p *Pointer) Step(f field.Field) { + p.x, p.y = f.Step(p.x, p.y, p.delta.x, p.delta.y) +} diff --git a/pkg/pointer/pointer_test.go b/pkg/pointer/pointer_test.go new file mode 100644 index 0000000..3a77507 --- /dev/null +++ b/pkg/pointer/pointer_test.go @@ -0,0 +1,41 @@ +package pointer + +import ( + "os" + "testing" + + "git.adyxax.org/adyxax/gofunge/pkg/field" + "github.com/stretchr/testify/require" +) + +func TestNewPointer(t *testing.T) { + require.Equal(t, NewPointer(), &Pointer{delta: &Delta{1, 0}}) +} + +func TestForkPointer(t *testing.T) { + file, err := os.Open("../field/test_data/hello.b98") + require.NoError(t, err, "Failed to open file") + defer file.Close() + f, err := field.Load(file) + require.NoError(t, err) + p := NewPointer() + p2 := p.ForkPointer() + // We check that p2 is a real copy + p.Step(*f) + p2.Step(*f) + require.Equal(t, p, &Pointer{x: 1, y: 0, delta: &Delta{1, 0}}) + require.Equal(t, p2, &Pointer{x: 1, y: 0, delta: &Delta{1, 0}}) +} + +func TestStep(t *testing.T) { // Step is thoroughly tested in the field package + defaultPointer := NewPointer() + // File of one char + file, err := os.Open("../field/test_data/minimal.b98") + require.NoError(t, err, "Failed to open file") + defer file.Close() + f, err := field.Load(file) + require.NoError(t, err) + p := NewPointer() + p.Step(*f) + require.Equal(t, defaultPointer, p) +} -- cgit v1.2.3