From e3bc1251e8cb20b30d0c5a27cd1da8f147a82de7 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 23 Sep 2021 00:24:53 +0200 Subject: Began coding the interpreter (only manages the minimal example for now!) --- pkg/interpreter/interpreter.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 pkg/interpreter/interpreter.go (limited to 'pkg/interpreter/interpreter.go') diff --git a/pkg/interpreter/interpreter.go b/pkg/interpreter/interpreter.go new file mode 100644 index 0000000..ce2499d --- /dev/null +++ b/pkg/interpreter/interpreter.go @@ -0,0 +1,40 @@ +package interpreter + +import ( + "log" + + "git.adyxax.org/adyxax/gofunge/pkg/field" + "git.adyxax.org/adyxax/gofunge/pkg/pointer" +) + +type Interpreter struct { + f *field.Field + p *pointer.Pointer +} + +func NewInterpreter(f *field.Field, p *pointer.Pointer) *Interpreter { + return &Interpreter{f: f, p: p} +} + +func (i *Interpreter) Run() { + for i.p != nil { + i.Step() + } +} + +func (i *Interpreter) Step() { + var prev *pointer.Pointer = nil + for p := i.p; p != nil; p = p.Next { + switch p.Get(*i.f) { + case '@': + if prev == nil { + i.p = p.Next + } else { + prev.Next = p.Next + } + break + default: + log.Fatalf("Non implemented instruction code %d : %c", p.Get(*i.f), p.Get(*i.f)) + } + } +} -- cgit v1.2.3