Add parser for display arg

This commit is contained in:
lash 2023-04-04 21:02:17 +01:00
parent 7bb479f4cb
commit 7cf1de955e
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 81 additions and 5 deletions

View File

@ -21,7 +21,8 @@ type Asm struct {
type Display struct { type Display struct {
Sym string `@Sym Whitespace` Sym string `@Sym Whitespace`
Val string `@Quote @Sym @Quote Whitespace` //Val string `Quote (@Desc @Whitespace?)+ Quote Whitespace`
Val string `Quote (@Sym @Whitespace?)+ Quote Whitespace`
} }
func(d Display) String() string { func(d Display) String() string {
@ -135,6 +136,16 @@ func writeSym(s string, w *bytes.Buffer) (int, error) {
return w.WriteString(s) return w.WriteString(s)
} }
func writeDisplay(s string, w *bytes.Buffer) (int, error) {
s = strings.Trim(s, "\"'")
sz := len(s)
if sz > 255 {
return 0, fmt.Errorf("string size %v too big", sz)
}
w.Write([]byte{byte(sz)})
return w.WriteString(s)
}
func writeSize(n uint32, w *bytes.Buffer) (int, error) { func writeSize(n uint32, w *bytes.Buffer) (int, error) {
bn := [4]byte{} bn := [4]byte{}
sz := numSize(n) sz := numSize(n)
@ -148,6 +159,41 @@ func writeSize(n uint32, w *bytes.Buffer) (int, error) {
} }
func parseDisplay(op vm.Opcode, arg Arg, w io.Writer) (int, error) {
var rn int
v := arg.ArgDisplay
if v == nil {
return 0, nil
}
b := bytes.NewBuffer(nil)
n, err := writeOpcode(op, b)
rn += n
if err != nil {
return rn, err
}
n, err = writeSym(v.Sym, b)
rn += n
if err != nil {
return rn, err
}
n, err = writeDisplay(v.Val, 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 parseSized(op vm.Opcode, arg Arg, w io.Writer) (int, error) { func parseSized(op vm.Opcode, arg Arg, w io.Writer) (int, error) {
var rn int var rn int
@ -198,6 +244,15 @@ func Parse(s string, w io.Writer) (int, error) {
rn += n rn += n
continue continue
} }
n, err = parseDisplay(op, v.OpArg, w)
if err != nil {
return n, err
}
if n > 0 {
rn += n
continue
}
} }
return rn, err return rn, err
} }

View File

@ -39,10 +39,31 @@ func TestParserSized(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if n != 8 { if n != 8 {
t.Fatalf("expected 0 byte write count, got %v", n) t.Fatalf("expected 8 byte write count, got %v", n)
} }
rb := r.Bytes() rb := r.Bytes()
if !bytes.Equal(rb, []byte{0x00, vm.LOAD, 0x03, 0x66, 0x6f, 0x6f, 0x01, 0x2a}) { if !bytes.Equal(rb, []byte{0x00, vm.LOAD, 0x03, 0x66, 0x6f, 0x6f, 0x01, 0x2a}) {
t.Fatalf("expected 0x00%x012a, got %v", vm.LOAD, rb) t.Fatalf("expected 0x00%x012a, got %v", vm.LOAD, rb)
} }
} }
func TestParseDisplay(t *testing.T) {
var b []byte
b = vm.NewLine(b, vm.MOUT, []string{"foo", "baz ba zbaz"}, 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 != 18 {
t.Fatalf("expected 18 byte write count, got %v", n)
}
rb := r.Bytes()
expect := []byte{0x00, vm.MOUT, 0x03, 0x66, 0x6f, 0x6f, 0x0b, 0x62, 0x61, 0x7a, 0x20, 0x62, 0x61, 0x20, 0x7a, 0x62, 0x61, 0x7a}
if !bytes.Equal(rb, expect) {
t.Fatalf("expected %x, got %x", expect, rb)
}
}

View File

@ -16,7 +16,7 @@ func TestToString(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
expect = "CATCH xyzzy 13 1 # invertmatch=true\n" expect = "CATCH xyzzy 13 1\n"
if r != expect { if r != expect {
t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r) t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r)
} }
@ -26,7 +26,7 @@ func TestToString(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
expect = "CROAK 13 1 # invertmatch=true\n" expect = "CROAK 13 1\n"
if r != expect { if r != expect {
t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r) t.Fatalf("expected:\n\t%v\ngot:\n\t%v", expect, r)
} }
@ -144,7 +144,7 @@ func TestToStringMultiple(t *testing.T) {
} }
expect := `INCMP 1 foo expect := `INCMP 1 foo
INCMP 2 bar INCMP 2 bar
CATCH aiee 666 0 # invertmatch=false CATCH aiee 666 0
LOAD inky 42 LOAD inky 42
HALT HALT
` `