-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (87 loc) · 1.73 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"os"
"strings"
"text/template"
"github.com/iancoleman/strcase"
)
func main() {
t, err := template.New("tmpl").Parse(opsTemplateSrc)
if err != nil {
panic(err)
}
f, err := os.Create("op.gen.go")
if err != nil {
panic(err)
}
defer f.Close()
d := &Data{}
for _, op := range Ops {
d.Ops = append(d.Ops, GenOp{
DisplayName: op.Name,
PascalName: strcase.ToCamel(strings.ReplaceAll(op.Name, "/", "_")),
CodeConst: fmt.Sprintf("%x", op.Code),
Fmt: op.Fmt,
})
}
if err := t.Execute(f, d); err != nil {
panic(err)
}
}
type Data struct {
Ops []GenOp
}
type GenOp struct {
DisplayName string
PascalName string
CodeConst string
Fmt string
}
var opsTemplateSrc = `{{$top := . -}}
package dextk
import "fmt"
const (
{{- range $o := $top.Ops}}
OpCode{{$o.PascalName}} OpCode = 0x{{$o.CodeConst}}
{{- end}}
)
var opConfigsExtra = map[OpCode]opConfig{}
var opConfigs = [256]opConfig{
{{- range $o := $top.Ops}}
OpCode{{$o.PascalName}}: {
Name: "{{$o.DisplayName}}",
Size: fmt{{$o.Fmt}}Size,
Reader: func(r *OpReader) (Op, error) {
pos := r.pos
f, err := r.readFmt{{$o.Fmt}}()
if err != nil {
return nil, err
}
return Op{{$o.PascalName}} {
opBase{pos: pos},
f,
}, nil
},
},
{{- end}}
}
{{range $o := $top.Ops}}
type Op{{$o.PascalName}} struct {
opBase
Fmt{{$o.Fmt}}
}
func (o Op{{$o.PascalName}}) Code() OpCode {
return OpCode{{$o.PascalName}}
}
func (o Op{{$o.PascalName}}) Fmt() Fmt {
return o.Fmt{{$o.Fmt}}
}
func (o Op{{$o.PascalName}}) String() string {
f := o.Fmt{{$o.Fmt}}.String()
if f != "" {
return fmt.Sprintf("0x%x: {{$o.DisplayName}} %v", o.pos, f)
}
return fmt.Sprintf("0x%x: {{$o.DisplayName}}", o.pos)
}
{{end}}`