Add parser for double
This commit is contained in:
parent
7cf1de955e
commit
7d788fe172
@ -70,8 +70,8 @@ type Arg struct {
|
||||
ArgNone string "Whitespace?"
|
||||
ArgDisplay *Display `@@?`
|
||||
ArgSized *Sized `@@?`
|
||||
ArgSingle *Single `@@?`
|
||||
ArgDouble *Double `@@?`
|
||||
ArgSingle *Single `@@?`
|
||||
}
|
||||
|
||||
func (a Arg) String() string {
|
||||
@ -194,6 +194,42 @@ func parseDisplay(op vm.Opcode, arg Arg, w io.Writer) (int, error) {
|
||||
return rn, err
|
||||
}
|
||||
|
||||
func parseDouble(op vm.Opcode, arg Arg, w io.Writer) (int, error) {
|
||||
var rn int
|
||||
|
||||
v := arg.ArgDouble
|
||||
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.One, b)
|
||||
rn += n
|
||||
if err != nil {
|
||||
return rn, err
|
||||
}
|
||||
|
||||
n, err = writeSym(v.Two, 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) {
|
||||
var rn int
|
||||
|
||||
@ -253,6 +289,15 @@ func Parse(s string, w io.Writer) (int, error) {
|
||||
rn += n
|
||||
continue
|
||||
}
|
||||
n, err = parseDouble(op, v.OpArg, w)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
if n > 0 {
|
||||
rn += n
|
||||
continue
|
||||
}
|
||||
|
||||
}
|
||||
return rn, err
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ func TestParserInit(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)
|
||||
@ -67,3 +68,24 @@ func TestParseDisplay(t *testing.T) {
|
||||
t.Fatalf("expected %x, got %x", expect, rb)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDouble(t *testing.T) {
|
||||
var b []byte
|
||||
b = vm.NewLine(b, vm.INCMP, []string{"foo", "bar"}, 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 != 10 {
|
||||
t.Fatalf("expected 18 byte write count, got %v", n)
|
||||
}
|
||||
rb := r.Bytes()
|
||||
expect := []byte{0x00, vm.INCMP, 0x03, 0x66, 0x6f, 0x6f, 0x03, 0x62, 0x61, 0x72}
|
||||
if !bytes.Equal(rb, expect) {
|
||||
t.Fatalf("expected %x, got %x", expect, rb)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user