Basic parsing

This commit is contained in:
Ian Gulliver
2021-11-16 21:08:46 -10:00
parent db33045391
commit 6449d4762a
5 changed files with 60 additions and 0 deletions

5
assembler/go.mod Normal file
View File

@@ -0,0 +1,5 @@
module github.com/firestuff/subcoding/assembler
go 1.17
require gopkg.in/yaml.v2 v2.4.0 // indirect

3
assembler/go.sum Normal file
View File

@@ -0,0 +1,3 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

25
assembler/parse.go Normal file
View File

@@ -0,0 +1,25 @@
package assembler
import "gopkg.in/yaml.v2"
type Program struct {
Functions []*Function `yaml:"functions"`
}
type Function []*Operation
type Operation []string
func NewProgramFromBytes(in []byte) (*Program, error) {
prog := &Program{}
err := yaml.UnmarshalStrict(in, &prog)
if err != nil {
return nil, err
}
return prog, nil
}
func NewProgramFromString(in string) (*Program, error) {
return NewProgramFromBytes([]byte(in))
}

20
assembler/parse_test.go Normal file
View File

@@ -0,0 +1,20 @@
package assembler
import "testing"
func TestSimple(t *testing.T) {
prog, err := NewProgramFromString(`
functions:
- - [add, f0, 1]
- [call, +1]
- [ltu, f0, 3]
- [jmpt, -3]
- - [add, g0, 1]
`)
if err != nil {
t.Fatal(err)
}
t.Logf("%+v", prog)
}

7
assembler/test.yaml Normal file
View File

@@ -0,0 +1,7 @@
functions:
- - [add, f0, 1]
- [call, +1]
- [ltu, f0, 3]
- [jmpt, -3]
- - [add, g0, 1]