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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
package main
import (
"io"
"reflect"
"text/template"
)
const (
fnErrSet = `if {{.Range}}[i], err = {{template "callFunc" .}}; handleNoOp(err) != nil {
return
}`
fnErrIncr = `var x {{asType .Kind}}
if x, err = {{template "callFunc" .}}; err != nil {
if err = handleNoOp(err);err != nil {
return
}
}
{{.Range}}[i] = x
`
simpleUnaryCallFunc = `{{template "symbol" .}}({{.Left}}[{{.Index0}}])`
)
type Map struct {
k reflect.Kind
Iter bool
Incr bool
Err bool
}
func (fn *Map) Name() string {
switch {
case fn.Iter && fn.Incr && fn.Err:
return "MapIterIncrErr"
case fn.Iter && fn.Incr && !fn.Err:
return "MapIterIncr"
case fn.Iter && !fn.Incr && fn.Err:
return "MapIterErr"
case fn.Iter && !fn.Incr && !fn.Err:
return "MapIter"
case !fn.Iter && fn.Incr && fn.Err:
return "MapIncrErr"
case !fn.Iter && fn.Incr && !fn.Err:
return "MapIncr"
case !fn.Iter && !fn.Incr && fn.Err:
return "MapErr"
default:
return "Map"
}
}
func (fn *Map) Arity() int { return 1 }
func (fn *Map) SymbolTemplate() string { return "fn" }
func (fn *Map) TypeClass() TypeClass { return nil }
func (fn *Map) IsFunc() bool { return true }
func (fn *Map) Kind() reflect.Kind { return fn.k }
func (fn *Map) Signature() *Signature {
var retErr bool
paramNames := []string{"fn", "a"}
paramTemplates := []*template.Template{unaryFuncType, sliceType}
if fn.Iter {
paramNames = append(paramNames, "ait")
paramTemplates = append(paramTemplates, iteratorType)
retErr = true
}
if fn.Err {
paramTemplates[0] = unaryFuncErrType
retErr = true
}
return &Signature{
Name: fn.Name(),
NameTemplate: typeAnnotatedName,
ParamNames: paramNames,
ParamTemplates: paramTemplates,
Kind: fn.Kind(),
Err: retErr,
}
}
func (fn *Map) WriteBody(w io.Writer) {
Range := "a"
Left := "a"
var T *template.Template
var IterName0 string
if fn.Iter {
T = template.Must(template.New(fn.Name()).Funcs(funcs).Parse(genericUnaryIterLoopRaw))
IterName0 = "ait"
} else {
T = template.Must(template.New(fn.Name()).Funcs(funcs).Parse(genericLoopRaw))
}
switch {
case fn.Incr && fn.Err:
template.Must(T.New("loopbody").Funcs(funcs).Parse(fnErrIncr))
case fn.Incr && !fn.Err:
template.Must(T.New("loopbody").Funcs(funcs).Parse(basicIncr))
case !fn.Incr && fn.Err:
template.Must(T.New("loopbody").Funcs(funcs).Parse(fnErrSet))
default:
template.Must(T.New("loopbody").Funcs(funcs).Parse(basicSet))
}
template.Must(T.New("callFunc").Funcs(funcs).Parse(simpleUnaryCallFunc))
template.Must(T.New("symbol").Funcs(funcs).Parse("fn"))
template.Must(T.New("opDo").Funcs(funcs).Parse(""))
template.Must(T.New("check").Funcs(funcs).Parse(""))
lb := LoopBody{
TypedOp: fn,
Range: Range,
Left: Left,
Index0: "i",
IterName0: IterName0,
}
T.Execute(w, lb)
}
func (fn *Map) Write(w io.Writer) {
sig := fn.Signature()
w.Write([]byte("func "))
sig.Write(w)
w.Write([]byte("{\n"))
fn.WriteBody(w)
w.Write([]byte("\nreturn \n"))
w.Write([]byte("}\n\n"))
}
func makeGenericMaps(incr bool) (retVal []*Map) {
for _, k := range allKinds {
if incr {
if !isAddable(k) {
continue
}
}
if isParameterized(k) {
continue
}
m := &Map{k: k}
if incr {
m.Incr = true
}
retVal = append(retVal, m)
}
return
}
func generateGenericMap(f io.Writer, ak Kinds) {
gen0 := makeGenericMaps(false)
for _, m := range gen0 {
m.Write(f)
m.Err = true
}
for _, m := range gen0 {
m.Write(f)
m.Err = false
m.Iter = true
}
for _, m := range gen0 {
m.Write(f)
m.Err = true
}
for _, m := range gen0 {
m.Write(f)
}
gen1 := makeGenericMaps(true)
for _, m := range gen1 {
m.Write(f)
m.Err = true
}
for _, m := range gen1 {
m.Write(f)
m.Err = false
m.Iter = true
}
for _, m := range gen1 {
m.Write(f)
m.Err = true
}
for _, m := range gen1 {
m.Write(f)
}
}
|