Add parser for double
This commit is contained in:
parent
7d788fe172
commit
54865b5e4d
@ -158,6 +158,35 @@ func writeSize(n uint32, w *bytes.Buffer) (int, error) {
|
||||
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) {
|
||||
var rn int
|
||||
@ -297,7 +326,14 @@ func Parse(s string, w io.Writer) (int, error) {
|
||||
rn += n
|
||||
continue
|
||||
}
|
||||
|
||||
n, err = parseSingle(op, v.OpArg, w)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
if n > 0 {
|
||||
rn += n
|
||||
continue
|
||||
}
|
||||
}
|
||||
return rn, err
|
||||
}
|
||||
|
@ -89,3 +89,24 @@ func TestParseDouble(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user