Add noarg parse, multi write test

This commit is contained in:
lash 2023-04-05 08:21:46 +01:00
parent f186424197
commit 19d8c66afe
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 73 additions and 0 deletions

View File

@ -294,6 +294,24 @@ func parseSized(op vm.Opcode, arg Arg, w io.Writer) (int, error) {
return rn, err return rn, err
} }
func parseNoarg(op vm.Opcode, arg Arg, w io.Writer) (int, error) {
var rn int
b := bytes.NewBuffer(nil)
n, err := writeOpcode(op, b)
rn += n
if err != nil {
return rn, err
}
if w != nil {
rn, err = w.Write(b.Bytes())
} else {
rn = 0
}
return rn, err
}
func Parse(s string, w io.Writer) (int, error) { func Parse(s string, w io.Writer) (int, error) {
rd := strings.NewReader(s) rd := strings.NewReader(s)
ast, err := asmParser.Parse("file", rd) ast, err := asmParser.Parse("file", rd)
@ -334,6 +352,15 @@ func Parse(s string, w io.Writer) (int, error) {
rn += n rn += n
continue continue
} }
n, err = parseNoarg(op, v.OpArg, w)
if err != nil {
return n, err
}
if n > 0 {
rn += n
continue
}
} }
return rn, err return rn, err
} }

View File

@ -110,3 +110,49 @@ func TestParseSingle(t *testing.T) {
t.Fatalf("expected %x, got %x", expect, rb) t.Fatalf("expected %x, got %x", expect, rb)
} }
} }
func TestParseNoarg(t *testing.T) {
var b []byte
b = vm.NewLine(b, vm.HALT, nil, nil, nil)
s, err := vm.ToString(b)
log.Printf("parsing:\n%s\n", s)
r := bytes.NewBuffer(nil)
n, err := Parse(s, r)
if err != nil {
t.Fatal(err)
}
if n != 2 {
t.Fatalf("expected 8 byte write count, got %v", n)
}
rb := r.Bytes()
expect := []byte{0x00, vm.HALT}
if !bytes.Equal(rb, expect) {
t.Fatalf("expected %x, got %x", expect, rb)
}
}
func TestParserWriteMultiple(t *testing.T) {
var b []byte
b = vm.NewLine(b, vm.HALT, nil, nil, nil)
b = vm.NewLine(b, vm.CATCH, []string{"xyzzy"}, []byte{0x02, 0x9a}, []uint8{1})
b = vm.NewLine(b, vm.INCMP, []string{"inky", "pinky"}, nil, nil)
b = vm.NewLine(b, vm.LOAD, []string{"foo"}, []byte{42}, nil)
b = vm.NewLine(b, vm.MOUT, []string{"bar", "barbarbaz"}, nil, nil)
s, err := vm.ToString(b)
log.Printf("parsing:\n%s\n", s)
r := bytes.NewBuffer(nil)
n, err := Parse(s, r)
if err != nil {
t.Fatal(err)
}
n_expect := 2 // halt
n_expect += 2 + 6 + 2 + 1 // catch
n_expect += 2 + 5 + 6 // incmp
n_expect += 2 + 4 + 2 // load
n_expect += 2 + 4 + 10 // mout
if n != n_expect {
t.Fatalf("expected total %v bytes output, got %v", n_expect, n)
}
}