go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit 7d788fe1723e78796af64d1772a755d5eb50849b
parent 7cf1de955e97c2d206be8b34515cab355063521d
Author: lash <dev@holbrook.no>
Date:   Wed,  5 Apr 2023 08:12:13 +0100

Add parser for double

Diffstat:
Mgo/asm/asm.go | 47++++++++++++++++++++++++++++++++++++++++++++++-
Mgo/asm/asm_test.go | 22++++++++++++++++++++++
2 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/go/asm/asm.go b/go/asm/asm.go @@ -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 } diff --git a/go/asm/asm_test.go b/go/asm/asm_test.go @@ -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) + } +}