gorgonia.org/gorgonia@v0.9.17/cmd/genapi/geninterface.go (about) 1 package main 2 3 import ( 4 "go/parser" 5 "go/token" 6 "io" 7 "log" 8 "path" 9 "strings" 10 "text/template" 11 ) 12 13 type UnaryOpInterfaceData struct { 14 OpTypes []string 15 Dtype string // f32, f64 16 } 17 18 const unaryOpInterfaceRaw = `func (f *s{{.Dtype}}UnaryOperator) unaryOpType() ʘUnaryOperatorType { 19 {{$dt := .Dtype -}} 20 switch f { 21 {{range $i, $op := .OpTypes -}} 22 case &{{$op}}{{$dt}}: 23 return {{$op}}OpType 24 {{end -}} 25 } 26 return maxʘUnaryOperator 27 } 28 29 func (f *s{{.Dtype}}UnaryOperator) String() string { return f.unaryOpType().String() } 30 31 ` 32 33 var unaryOpInterface *template.Template 34 35 func init() { 36 unaryOpInterface = template.Must(template.New("UnOpInterface").Funcs(funcmap).Parse(unaryOpInterfaceRaw)) 37 } 38 39 func generateUnaryInterface(outFile io.Writer) { 40 // parse operator_unary_const.go 41 filename := path.Join(gorgonialoc, unaryOps) 42 fset := token.NewFileSet() 43 file, err := parser.ParseFile(fset, filename, nil, parser.AllErrors) 44 if err != nil { 45 log.Fatal(err) 46 } 47 48 unaryNames := constTypes(file.Decls, "ʘUnaryOperatorType", "maxʘUnaryOperator") 49 var opNames []string 50 for _, v := range unaryNames { 51 op := strings.TrimSuffix(v, "OpType") 52 opNames = append(opNames, op) 53 } 54 55 dtypes := []string{"f32", "f64"} 56 for _, dt := range dtypes { 57 data := UnaryOpInterfaceData{opNames, dt} 58 unaryOpInterface.Execute(outFile, data) 59 } 60 }