| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"reflect" |
| 6 |
"strings" |
| 7 |
) |
| 8 |
|
| 9 |
type TypeClass func(a reflect.Kind) bool |
| 10 |
|
| 11 |
func isParameterized(a reflect.Kind) bool { |
| 12 |
for _, v := range parameterizedKinds { |
| 13 |
if v == a { |
| 14 |
return true |
| 15 |
} |
| 16 |
} |
| 17 |
return false |
| 18 |
} |
| 19 |
|
| 20 |
func isNotParameterized(a reflect.Kind) bool { return !isParameterized(a) } |
| 21 |
|
| 22 |
func isRangeable(a reflect.Kind) bool { |
| 23 |
for _, v := range rangeable { |
| 24 |
if v == a { |
| 25 |
return true |
| 26 |
} |
| 27 |
} |
| 28 |
return false |
| 29 |
} |
| 30 |
|
| 31 |
func isSpecialized(a reflect.Kind) bool { |
| 32 |
for _, v := range specialized { |
| 33 |
if v == a { |
| 34 |
return true |
| 35 |
} |
| 36 |
} |
| 37 |
return false |
| 38 |
} |
| 39 |
|
| 40 |
func isNumber(a reflect.Kind) bool { |
| 41 |
for _, v := range number { |
| 42 |
if v == a { |
| 43 |
return true |
| 44 |
} |
| 45 |
} |
| 46 |
return false |
| 47 |
} |
| 48 |
|
| 49 |
func isSignedNumber(a reflect.Kind) bool { |
| 50 |
for _, v := range signedNumber { |
| 51 |
if v == a { |
| 52 |
return true |
| 53 |
} |
| 54 |
} |
| 55 |
return false |
| 56 |
} |
| 57 |
|
| 58 |
func isNonComplexNumber(a reflect.Kind) bool { |
| 59 |
for _, v := range nonComplexNumber { |
| 60 |
if v == a { |
| 61 |
return true |
| 62 |
} |
| 63 |
} |
| 64 |
return false |
| 65 |
} |
| 66 |
|
| 67 |
func isAddable(a reflect.Kind) bool { |
| 68 |
if a == reflect.String { |
| 69 |
return true |
| 70 |
} |
| 71 |
return isNumber(a) |
| 72 |
} |
| 73 |
|
| 74 |
func isComplex(a reflect.Kind) bool { |
| 75 |
if a == reflect.Complex128 || a == reflect.Complex64 { |
| 76 |
return true |
| 77 |
} |
| 78 |
return false |
| 79 |
} |
| 80 |
|
| 81 |
func panicsDiv0(a reflect.Kind) bool { |
| 82 |
for _, v := range div0panics { |
| 83 |
if v == a { |
| 84 |
return true |
| 85 |
} |
| 86 |
} |
| 87 |
return false |
| 88 |
} |
| 89 |
|
| 90 |
func isEq(a reflect.Kind) bool { |
| 91 |
for _, v := range elEq { |
| 92 |
if v == a { |
| 93 |
return true |
| 94 |
} |
| 95 |
} |
| 96 |
return false |
| 97 |
} |
| 98 |
|
| 99 |
func isOrd(a reflect.Kind) bool { |
| 100 |
for _, v := range elOrd { |
| 101 |
if v == a { |
| 102 |
return true |
| 103 |
} |
| 104 |
} |
| 105 |
return false |
| 106 |
} |
| 107 |
|
| 108 |
func isBoolRepr(a reflect.Kind) bool { |
| 109 |
for _, v := range boolRepr { |
| 110 |
if v == a { |
| 111 |
return true |
| 112 |
} |
| 113 |
} |
| 114 |
return false |
| 115 |
} |
| 116 |
|
| 117 |
func mathPkg(a reflect.Kind) string { |
| 118 |
if a == reflect.Float64 { |
| 119 |
return "math." |
| 120 |
} |
| 121 |
if a == reflect.Float32 { |
| 122 |
return "math32." |
| 123 |
} |
| 124 |
if a == reflect.Complex64 || a == reflect.Complex128 { |
| 125 |
return "cmplx." |
| 126 |
} |
| 127 |
return "" |
| 128 |
} |
| 129 |
|
| 130 |
func vecPkg(a reflect.Kind) string { |
| 131 |
if a == reflect.Float64 { |
| 132 |
return "vecf64." |
| 133 |
} |
| 134 |
if a == reflect.Float32 { |
| 135 |
return "vecf32." |
| 136 |
} |
| 137 |
return "" |
| 138 |
} |
| 139 |
|
| 140 |
func getalias(name string) string { |
| 141 |
if nice, ok := nameMaps[name]; ok { |
| 142 |
return nice |
| 143 |
} |
| 144 |
return name |
| 145 |
} |
| 146 |
|
| 147 |
func interfaceName(name string) string { |
| 148 |
switch name { |
| 149 |
case "Square": |
| 150 |
return "Squarer" |
| 151 |
case "Cube": |
| 152 |
return "Cuber" |
| 153 |
case "Eq", "ElEq": |
| 154 |
return "ElEqer" |
| 155 |
case "Ne", "ElNe": |
| 156 |
return "ElEqer" |
| 157 |
default: |
| 158 |
return name + "er" |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
func bitSizeOf(a reflect.Kind) string { |
| 163 |
switch a { |
| 164 |
case reflect.Int, reflect.Uint: |
| 165 |
return "0" |
| 166 |
case reflect.Int8, reflect.Uint8: |
| 167 |
return "8" |
| 168 |
case reflect.Int16, reflect.Uint16: |
| 169 |
return "16" |
| 170 |
case reflect.Int32, reflect.Uint32, reflect.Float32: |
| 171 |
return "32" |
| 172 |
case reflect.Int64, reflect.Uint64, reflect.Float64: |
| 173 |
return "64" |
| 174 |
} |
| 175 |
return "UNKNOWN BIT SIZE" |
| 176 |
} |
| 177 |
|
| 178 |
func trueValue(a reflect.Kind) string { |
| 179 |
switch a { |
| 180 |
case reflect.String: |
| 181 |
return `"true"` |
| 182 |
case reflect.Bool: |
| 183 |
return "true" |
| 184 |
default: |
| 185 |
return "1" |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
func falseValue(a reflect.Kind) string { |
| 190 |
switch a { |
| 191 |
case reflect.String: |
| 192 |
return `"false"` |
| 193 |
case reflect.Bool: |
| 194 |
return "false" |
| 195 |
default: |
| 196 |
return "0" |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
func isFloat(a reflect.Kind) bool { |
| 201 |
if a == reflect.Float32 || a == reflect.Float64 { |
| 202 |
return true |
| 203 |
} |
| 204 |
return false |
| 205 |
} |
| 206 |
|
| 207 |
func isFloatCmplx(a reflect.Kind) bool { |
| 208 |
if a == reflect.Float32 || a == reflect.Float64 || a == reflect.Complex64 || a == reflect.Complex128 { |
| 209 |
return true |
| 210 |
} |
| 211 |
return false |
| 212 |
} |
| 213 |
|
| 214 |
func isntFloat(a reflect.Kind) bool { return !isFloat(a) } |
| 215 |
|
| 216 |
func isntComplex(a reflect.Kind) bool { return !isComplex(a) } |
| 217 |
|
| 218 |
func short(a reflect.Kind) string { |
| 219 |
return shortNames[a] |
| 220 |
} |
| 221 |
|
| 222 |
func clean(a string) string { |
| 223 |
if a == "unsafe.pointer" { |
| 224 |
return "unsafe.Pointer" |
| 225 |
} |
| 226 |
return a |
| 227 |
} |
| 228 |
|
| 229 |
func unexport(a string) string { |
| 230 |
return strings.ToLower(string(a[0])) + a[1:] |
| 231 |
} |
| 232 |
|
| 233 |
func strip(a string) string { |
| 234 |
return strings.Replace(a, ".", "", -1) |
| 235 |
} |
| 236 |
|
| 237 |
func reflectKind(a reflect.Kind) string { |
| 238 |
return strip(strings.Title(a.String())) |
| 239 |
} |
| 240 |
|
| 241 |
func asType(a reflect.Kind) string { |
| 242 |
return clean(a.String()) |
| 243 |
} |
| 244 |
|
| 245 |
func sliceOf(a reflect.Kind) string { |
| 246 |
s := fmt.Sprintf("%ss()", strings.Title(a.String())) |
| 247 |
return strip(clean(s)) |
| 248 |
} |
| 249 |
|
| 250 |
func getOne(a reflect.Kind) string { |
| 251 |
return fmt.Sprintf("Get%s", short(a)) |
| 252 |
} |
| 253 |
|
| 254 |
func setOne(a reflect.Kind) string { |
| 255 |
return fmt.Sprintf("Set%s", short(a)) |
| 256 |
} |
| 257 |
|
| 258 |
func filter(a []reflect.Kind, is func(reflect.Kind) bool) (retVal []reflect.Kind) { |
| 259 |
for _, k := range a { |
| 260 |
if is(k) { |
| 261 |
retVal = append(retVal, k) |
| 262 |
} |
| 263 |
} |
| 264 |
return |
| 265 |
} |