diff options
Diffstat (limited to '')
-rw-r--r-- | pkg/interpreter/interpreter.go | 10 | ||||
-rw-r--r-- | pkg/interpreter/interpreter_test.go | 50 |
2 files changed, 58 insertions, 2 deletions
diff --git a/pkg/interpreter/interpreter.go b/pkg/interpreter/interpreter.go index ce2499d..b64e90a 100644 --- a/pkg/interpreter/interpreter.go +++ b/pkg/interpreter/interpreter.go @@ -25,7 +25,8 @@ func (i *Interpreter) Run() { func (i *Interpreter) Step() { var prev *pointer.Pointer = nil for p := i.p; p != nil; p = p.Next { - switch p.Get(*i.f) { + c := p.Get(*i.f) + switch c { case '@': if prev == nil { i.p = p.Next @@ -33,8 +34,13 @@ func (i *Interpreter) Step() { prev.Next = p.Next } break + case '#': + p.Step(*i.f) default: - log.Fatalf("Non implemented instruction code %d : %c", p.Get(*i.f), p.Get(*i.f)) + if !p.Redirect(c) { + log.Fatalf("Non implemented instruction code %d : %c", c, c) + } } + p.Step(*i.f) } } diff --git a/pkg/interpreter/interpreter_test.go b/pkg/interpreter/interpreter_test.go new file mode 100644 index 0000000..ff633a2 --- /dev/null +++ b/pkg/interpreter/interpreter_test.go @@ -0,0 +1,50 @@ +package interpreter + +import ( + "os" + "testing" + + "git.adyxax.org/adyxax/gofunge/pkg/field" + "git.adyxax.org/adyxax/gofunge/pkg/pointer" + "github.com/stretchr/testify/require" +) + +func TestRun(t *testing.T) { + file, err := os.Open("../field/test_data/minimal.b98") + require.NoError(t, err) + defer file.Close() + f, err := field.Load(file) + require.NoError(t, err) + NewInterpreter(f, pointer.NewPointer()).Run() +} + +func TestStep(t *testing.T) { + testCases := []struct { + name string + filename string + pointer *pointer.Pointer + expectedField *field.Field + expectedPointer *pointer.Pointer + }{ + {"minimal", "../field/test_data/minimal.b98", nil, nil, nil}, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + file, err := os.Open(tc.filename) + require.NoError(t, err) + defer file.Close() + f, err := field.Load(file) + require.NoError(t, err) + if tc.pointer == nil { + tc.pointer = pointer.NewPointer() + } + NewInterpreter(f, tc.pointer).Step() + if tc.expectedField != nil { + require.Equal(t, tc.expectedField, f) + } + if tc.expectedPointer != nil { + require.Equal(t, tc.expectedPointer, tc.pointer) + } + }) + } +} |