/
geninterface.go
60 lines (50 loc) · 1.3 KB
/
geninterface.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
package main
import (
"go/parser"
"go/token"
"io"
"log"
"path"
"strings"
"text/template"
)
type UnaryOpInterfaceData struct {
OpTypes []string
Dtype string // f32, f64
}
const unaryOpInterfaceRaw = `func (f *s{{.Dtype}}UnaryOperator) unaryOpType() ʘUnaryOperatorType {
{{$dt := .Dtype -}}
switch f {
{{range $i, $op := .OpTypes -}}
case &{{$op}}{{$dt}}:
return {{$op}}OpType
{{end -}}
}
return maxʘUnaryOperator
}
func (f *s{{.Dtype}}UnaryOperator) String() string { return f.unaryOpType().String() }
`
var unaryOpInterface *template.Template
func init() {
unaryOpInterface = template.Must(template.New("UnOpInterface").Funcs(funcmap).Parse(unaryOpInterfaceRaw))
}
func generateUnaryInterface(outFile io.Writer) {
// parse operator_unary_const.go
filename := path.Join(gorgonialoc, unaryOps)
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, filename, nil, parser.AllErrors)
if err != nil {
log.Fatal(err)
}
unaryNames := constTypes(file.Decls, "ʘUnaryOperatorType", "maxʘUnaryOperator")
var opNames []string
for _, v := range unaryNames {
op := strings.TrimSuffix(v, "OpType")
opNames = append(opNames, op)
}
dtypes := []string{"f32", "f64"}
for _, dt := range dtypes {
data := UnaryOpInterfaceData{opNames, dt}
unaryOpInterface.Execute(outFile, data)
}
}