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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
package main
import "text/template"
// generic loop templates
type LoopBody struct {
TypedOp
Range string
Left string
Right string
Index0, Index1, Index2 string
IterName0, IterName1, IterName2 string
}
const (
genericLoopRaw = `for i := range {{.Range}} {
{{template "check" . -}}
{{template "loopbody" . -}}
}`
genericUnaryIterLoopRaw = `var {{.Index0}} int
var valid{{.Index0}} bool
for {
if {{.Index0}}, valid{{.Index0}}, err = {{.IterName0}}.NextValidity(); err != nil {
err = handleNoOp(err)
break
}
if valid{{.Index0}} {
{{template "check" . -}}
{{template "loopbody" . -}}
}
}`
genericBinaryIterLoopRaw = `var {{.Index0}}, {{.Index1}} int
var valid{{.Index0}}, valid{{.Index1}} bool
for {
if {{.Index0}}, valid{{.Index0}}, err = {{.IterName0}}.NextValidity(); err != nil {
err = handleNoOp(err)
break
}
if {{.Index1}}, valid{{.Index1}}, err = {{.IterName1}}.NextValidity(); err != nil {
err = handleNoOp(err)
break
}
if valid{{.Index0}} && valid{{.Index1}} {
{{template "check" . -}}
{{template "loopbody" . -}}
}
}`
genericTernaryIterLoopRaw = `var {{.Index0}}, {{.Index1}}, {{.Index2}} int
var valid{{.Index0}}, valid{{.Index1}}, valid{{.Index2}} bool
for {
if {{.Index0}}, valid{{.Index0}}, err = {{.IterName0}}.NextValidity(); err != nil {
err = handleNoOp(err)
break
}
if {{.Index1}}, valid{{.Index1}}, err = {{.IterName1}}.NextValidity(); err != nil {
err = handleNoOp(err)
break
}
if {{.Index2}}, valid{{.Index2}}, err = {{.IterName2}}.NextValidity(); err != nil {
err = handleNoOp(err)
break
}
if valid{{.Index0}} && valid{{.Index1}} && valid{{.Index2}} {
{{template "check" . -}}
{{template "loopbody" . -}}
}
}`
// ALL THE SYNTACTIC ABSTRACTIONS!
// did I mention how much I hate C-style macros? Now I'm doing them instead
basicSet = `{{if .IsFunc -}}
{{.Range}}[i] = {{ template "callFunc" . -}}
{{else -}}
{{.Range}}[i] = {{template "opDo" . -}}
{{end -}}`
basicIncr = `{{if .IsFunc -}}
{{.Range}}[i] += {{template "callFunc" . -}}
{{else -}}
{{.Range}}[i] += {{template "opDo" . -}}
{{end -}}`
iterIncrLoopBody = `{{if .IsFunc -}}
{{.Range}}[k] += {{template "callFunc" . -}}
{{else -}}
{{.Range}}[k] += {{template "opDo" . -}}
{{end -}}`
sameSet = `if {{template "opDo" . }} {
{{.Range}}[i] = {{trueValue .Kind}}
}else{
{{.Range}}[i] = {{falseValue .Kind}}
}`
clampBody = `if {{.Range}}[i] < min {{if eq .Kind.String "float64"}}|| math.IsInf({{.Range}}[i], -1){{else if eq .Kind.String "float32"}}|| math32.IsInf({{.Range}}[i], -1){{end}} {
{{.Range}}[i] = min
continue
}
if {{.Range}}[i] > max {{if eq .Kind.String "float64"}}|| math.IsInf({{.Range}}[i], 1){{else if eq .Kind.String "float32"}}|| math32.IsInf({{.Range}}[i], 1){{end}} {
{{.Range}}[i] = max
}
`
ternaryIterSet = `{{.Range}}[k] = {{template "opDo" . -}}`
binOpCallFunc = `{{if eq "complex64" .Kind.String -}}
complex64({{template "symbol" .Kind}}(complex128({{.Left}}), complex128({{.Right}}))){{else -}}
{{template "symbol" .Kind}}({{.Left}}, {{.Right}}){{end -}}`
binOpDo = `{{.Left}} {{template "symbol" .Kind}} {{.Right}}`
unaryOpDo = `{{template "symbol" .}}{{.Left}}[{{.Index0}}]`
unaryOpCallFunc = `{{if eq "complex64" .Kind.String -}}
complex64({{template "symbol" .}}(complex128({{.Left}}[{{.Index0}}]))){{else -}}
{{template "symbol" .}}({{.Left}}[{{.Index0}}]){{end -}}
`
check0 = `if {{.Right}} == 0 {
errs = append(errs, i)
{{.Range}}[i] = 0
continue
}
`
maskCheck = `if mask[i] {
continue
}
`
genericArgmaxVarDeclRaw = `var set bool
var f {{asType .Kind}}
var {{.ArgX | lower}} int
`
)
// renamed
const (
vvLoopRaw = genericLoopRaw
vvIncrLoopRaw = genericLoopRaw
vvIterLoopRaw = genericBinaryIterLoopRaw
vvIterIncrLoopRaw = genericTernaryIterLoopRaw
mixedLoopRaw = genericLoopRaw
mixedIncrLoopRaw = genericLoopRaw
mixedIterLoopRaw = genericUnaryIterLoopRaw
mixedIterIncrLoopRaw = genericBinaryIterLoopRaw
)
var genericArgmaxVarDecl *template.Template
func init() {
genericArgmaxVarDecl = template.Must(template.New("genericArgmaxVarDecl").Funcs(funcs).Parse(genericArgmaxVarDeclRaw))
}
|