Add parser for double

This commit is contained in:
lash 2023-04-05 08:14:08 +01:00
parent 7d788fe172
commit 54865b5e4d
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 58 additions and 1 deletions

View File

@ -158,6 +158,35 @@ func writeSize(n uint32, w *bytes.Buffer) (int, error) {
return w.Write(bn[c:]) return w.Write(bn[c:])
} }
func parseSingle(op vm.Opcode, arg Arg, w io.Writer) (int, error) {
var rn int
v := arg.ArgSingle
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
}
if w != nil {
rn, err = w.Write(b.Bytes())
} else {
rn = 0
}
return rn, err
}
func parseDisplay(op vm.Opcode, arg Arg, w io.Writer) (int, error) { func parseDisplay(op vm.Opcode, arg Arg, w io.Writer) (int, error) {
var rn int var rn int
@ -297,7 +326,14 @@ func Parse(s string, w io.Writer) (int, error) {
rn += n rn += n
continue continue
} }
n, err = parseSingle(op, v.OpArg, w)
if err != nil {
return n, err
}
if n > 0 {
rn += n
continue
}
} }
return rn, err return rn, err
} }

View File

@ -89,3 +89,24 @@ func TestParseDouble(t *testing.T) {
t.Fatalf("expected %x, got %x", expect, rb) t.Fatalf("expected %x, got %x", expect, rb)
} }
} }
func TestParseSingle(t *testing.T) {
var b []byte
b = vm.NewLine(b, vm.MAP, []string{"xyzzy"}, 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 != 8 {
t.Fatalf("expected 8 byte write count, got %v", n)
}
rb := r.Bytes()
expect := []byte{0x00, vm.MAP, 0x05, 0x78, 0x79, 0x7a, 0x7a, 0x79}
if !bytes.Equal(rb, expect) {
t.Fatalf("expected %x, got %x", expect, rb)
}
}