Fix bad int render for sig

This commit is contained in:
lash 2023-04-05 11:22:13 +01:00
parent be736c1ff6
commit b8c8802421
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 39 additions and 106 deletions

View File

@ -7,7 +7,6 @@ import (
"io" "io"
"log" "log"
"math" "math"
// "strconv"
"strings" "strings"
"github.com/alecthomas/participle/v2" "github.com/alecthomas/participle/v2"
@ -23,49 +22,15 @@ type Asm struct {
type Display struct { type Display struct {
Sym string `@Sym Whitespace` Sym string `@Sym Whitespace`
//Val string `Quote (@Desc @Whitespace?)+ Quote Whitespace` Val string `Quote (@Sym @Whitespace?)+ Quote`
Val string `Quote (@Sym @Whitespace?)+ Quote Whitespace? EOL`
} }
func(d Display) String() string { func(d Display) String() string {
return fmt.Sprintf("Display: %v %v", d.Sym, d.Val) return fmt.Sprintf("Display: %v %v", d.Sym, d.Val)
} }
type Sig struct {
Sym string `@Sym Whitespace`
Size uint32 `@Size Whitespace`
Val uint8 `@Size Whitespace? EOL`
}
//
//func(s Sig) Capture(v []string) error {
// log.Printf("considering capture %v %v", v[0], len(v))
// if len(v) < 3 {
// return nil
// }
// s.Sym = v[0]
// r, err := strconv.Atoi(v[1])
// if err != nil {
// return err
// }
// s.Size = uint32(r)
// r, err = strconv.Atoi(v[2])
// if err != nil {
// return err
// }
// if r != 0 {
// r = 1
// }
// s.Val = uint8(r)
// log.Printf("after considering capture: %v", s)
// return nil
//}
func(s Sig) String() string {
return fmt.Sprintf("Sig: %v %v %v", s.Sym, s.Size, s.Val)
}
type Single struct { type Single struct {
One string `@Sym Whitespace? EOL` One string `@Sym`
} }
func(s Single) String() string { func(s Single) String() string {
@ -74,7 +39,7 @@ func(s Single) String() string {
type Double struct { type Double struct {
One string `@Sym Whitespace` One string `@Sym Whitespace`
Two string `@Sym Whitespace? EOL` Two string `@Sym`
} }
func(d Double) String() string { func(d Double) String() string {
@ -83,12 +48,7 @@ func(d Double) String() string {
type Sized struct { type Sized struct {
Sym string `@Sym Whitespace` Sym string `@Sym Whitespace`
Size uint32 `@Size Whitespace? EOL` Size uint32 `@Size`
}
func(s Sized) Capture(v []Sized) error {
log.Printf("foofofofofo")
return fmt.Errorf("foo foo foo")
} }
func(s Sized) String() string { func(s Sized) String() string {
@ -96,20 +56,20 @@ func(s Sized) String() string {
} }
type Arg struct { type Arg struct {
ArgNone string "Discard?"
ArgDisplay *Display `@@?` ArgDisplay *Display `@@?`
ArgSized *Sized `@@?` ArgSized *Sized `@@?`
ArgSig *Sig `@@?` ArgFlag *uint8 `@Size?`
ArgDouble *Double `@@?` ArgDouble *Double `@@?`
ArgSingle *Single `@@?` ArgSingle *Single `@@?`
ArgNone string `Whitespace? EOL`
} }
func (a Arg) String() string { func (a Arg) String() string {
if a.ArgDisplay != nil { if a.ArgDisplay != nil {
return fmt.Sprintf("%s", a.ArgDisplay) return fmt.Sprintf("%s", a.ArgDisplay)
} }
if a.ArgSig != nil { if a.ArgFlag != nil {
return fmt.Sprintf("%s", a.ArgSig) return fmt.Sprintf("Flag: %v", *a.ArgFlag)
} }
if a.ArgSized != nil { if a.ArgSized != nil {
return fmt.Sprintf("%s", a.ArgSized) return fmt.Sprintf("%s", a.ArgSized)
@ -330,53 +290,6 @@ func parseSized(op vm.Opcode, arg Arg, w io.Writer) (int, error) {
return rn, err return rn, err
} }
func parseSig(op vm.Opcode, arg Arg, w io.Writer) (int, error) {
var rn int
v := arg.ArgSig
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 = writeSize(v.Size, b)
rn += n
if err != nil {
return rn, err
}
if v.Val == 0 {
n, err = b.Write([]byte{0x00})
} else {
n, err = b.Write([]byte{0x01})
}
rn += n
if err != nil {
return rn, err
}
if w != nil {
rn, err = w.Write(b.Bytes())
} else {
rn = 0
}
return rn, err
}
func parseNoarg(op vm.Opcode, arg Arg, w io.Writer) (int, error) { func parseNoarg(op vm.Opcode, arg Arg, w io.Writer) (int, error) {
var rn int var rn int
@ -395,6 +308,23 @@ func parseNoarg(op vm.Opcode, arg Arg, w io.Writer) (int, error) {
return rn, err return rn, err
} }
func parseFlag(op vm.Opcode, arg Arg, w io.Writer) (int, error) {
var rn int
var err error
v := arg.ArgFlag
if v == nil {
return 0, nil
}
if w != nil {
rn, err = w.Write([]byte{*v})
} else {
rn = 0
}
return rn, err
}
func Parse(s string, w io.Writer) (int, error) { func Parse(s string, w io.Writer) (int, error) {
rd := strings.NewReader(s) rd := strings.NewReader(s)
ast, err := asmParser.Parse("file", rd) ast, err := asmParser.Parse("file", rd)
@ -412,17 +342,14 @@ func Parse(s string, w io.Writer) (int, error) {
} }
if n > 0 { if n > 0 {
rn += n rn += n
continue n, err = parseFlag(op, v.OpArg, w)
}
n, err = parseDisplay(op, v.OpArg, w)
if err != nil { if err != nil {
return n, err return n, err
} }
if n > 0 {
rn += n rn += n
continue continue
} }
n, err = parseSig(op, v.OpArg, w) n, err = parseDisplay(op, v.OpArg, w)
if err != nil { if err != nil {
return n, err return n, err
} }

View File

@ -182,12 +182,18 @@ func TestParserWriteMultiple(t *testing.T) {
if n != n_expect { if n != n_expect {
t.Fatalf("expected total %v bytes output, got %v", n_expect, n) t.Fatalf("expected total %v bytes output, got %v", n_expect, n)
} }
r_expect_hex := "000700010578797a7a7902029a000804696e6b790570696e6b79000303666f6f023432000a036261720b626172206261726220617a" r_expect_hex := "000700010578797a7a7902029a01000804696e6b790570696e6b79000303666f6f012a000a036261720b626172206261726220617a"
r_expect, err := hex.DecodeString(r_expect_hex) r_expect, err := hex.DecodeString(r_expect_hex)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !bytes.Equal(r.Bytes(), r_expect) { rb := r.Bytes()
t.Fatalf("expected result %v, got %x", r_expect_hex, r.Bytes()) if !bytes.Equal(rb, r_expect) {
t.Fatalf("expected result %v, got %x", r_expect_hex, rb)
}
_, err = vm.ParseAll(rb, nil)
if err != nil {
t.Fatal(err)
} }
} }