Began coding the interpreter (only manages the minimal example for now!)
This commit is contained in:
parent
6f70a7237b
commit
e3bc1251e8
4 changed files with 99 additions and 0 deletions
42
cmd/headless_interpreter/main.go
Normal file
42
cmd/headless_interpreter/main.go
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"git.adyxax.org/adyxax/gofunge/pkg/field"
|
||||||
|
"git.adyxax.org/adyxax/gofunge/pkg/interpreter"
|
||||||
|
"git.adyxax.org/adyxax/gofunge/pkg/pointer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
filename := flag.String("f", "", "b98 file to interpret")
|
||||||
|
help := flag.Bool("h", false, "display this help message")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if *help {
|
||||||
|
flag.Usage()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
if *filename == "" {
|
||||||
|
fmt.Println("Error : no b98 file to interpret")
|
||||||
|
flag.Usage()
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
file, err := os.Open(*filename)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to open file %s : %+v", *filename, err)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
f, err := field.Load(file)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to load file %s : %+v", *filename, err)
|
||||||
|
os.Exit(3)
|
||||||
|
}
|
||||||
|
|
||||||
|
interpreter.NewInterpreter(f, pointer.NewPointer()).Run()
|
||||||
|
}
|
40
pkg/interpreter/interpreter.go
Normal file
40
pkg/interpreter/interpreter.go
Normal file
|
@ -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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,6 +12,8 @@ type Pointer struct {
|
||||||
// The Storage offset
|
// The Storage offset
|
||||||
sox int
|
sox int
|
||||||
soy int
|
soy int
|
||||||
|
// The next element for the multi-"threaded" b98 interpreter
|
||||||
|
Next *Pointer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPointer() *Pointer {
|
func NewPointer() *Pointer {
|
||||||
|
@ -25,3 +27,7 @@ func (p Pointer) Split() *Pointer {
|
||||||
func (p *Pointer) Step(f field.Field) {
|
func (p *Pointer) Step(f field.Field) {
|
||||||
p.x, p.y = f.Step(p.x, p.y, p.dx, p.dy)
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -39,3 +39,14 @@ func TestStep(t *testing.T) { // Step is thoroughly tested in the field package
|
||||||
p.Step(*f)
|
p.Step(*f)
|
||||||
require.Equal(t, defaultPointer, p)
|
require.Equal(t, defaultPointer, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGet(t *testing.T) {
|
||||||
|
// 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)
|
||||||
|
p := NewPointer()
|
||||||
|
v := p.Get(*f)
|
||||||
|
require.Equal(t, int('@'), v)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue