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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
// +build ignore
// Copyright 2014 The ql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
)
type t int
const (
qNil t = iota
idealComplex
idealFloat
idealInt
idealRune
idealUint
qBool
qComplex64
qComplex128
qFloat32
qFloat64
qInt8
qInt16
qInt32
qInt64
qString
qUint8
qUint16
qUint32
qUint64
qBigInt
qBigRat
qTime
qDuration
qEnd
)
func (n t) String() string {
switch n {
case qNil:
return "nil"
case idealComplex:
return "idealComplex"
case idealFloat:
return "idealFloat"
case idealInt:
return "idealInt"
case idealRune:
return "idealRune"
case idealUint:
return "idealUint"
case qBool:
return "bool"
case qComplex64:
return "complex64"
case qComplex128:
return "complex128"
case qFloat32:
return "float32"
case qFloat64:
return "float64"
case qInt8:
return "int8"
case qInt16:
return "int16"
case qInt32:
return "int32"
case qInt64:
return "int64"
case qString:
return "string"
case qUint8:
return "uint8"
case qUint16:
return "uint16"
case qUint32:
return "uint32"
case qUint64:
return "uint64"
case qBigInt:
return "*big.Int"
case qBigRat:
return "*big.Rat"
case qTime:
return "time.Time"
case qDuration:
return "time.Duration"
default:
panic("internal error 046")
}
}
func coerceIdealComplex(typ t) string {
switch typ {
case qComplex64, qComplex128:
return fmt.Sprintf("return %s(x)\n", typ)
default:
return ""
}
}
func coerceIdealFloat(typ t) string {
switch typ {
case idealComplex:
return fmt.Sprintf("return %s(complex(float64(x), 0))\n", typ)
case qComplex64:
return fmt.Sprintf("return %s(complex(float32(x), 0))\n", typ)
case qComplex128:
return fmt.Sprintf("return %s(complex(float64(x), 0))\n", typ)
case idealFloat, qFloat32, qFloat64:
return fmt.Sprintf("return %s(float64(x))\n", typ)
case qBigRat:
return fmt.Sprintf("return big.NewRat(1, 1).SetFloat64(float64(x))\n")
default:
return ""
}
return ""
}
func coerceIdealInt(typ t) string {
switch typ {
case idealComplex:
return fmt.Sprintf("return %s(complex(float64(x), 0))\n", typ)
case qComplex64:
return fmt.Sprintf("return %s(complex(float32(x), 0))\n", typ)
case qComplex128:
return fmt.Sprintf("return %s(complex(float64(x), 0))\n", typ)
case idealFloat, idealInt, qFloat32, qFloat64, qInt64:
return fmt.Sprintf("return %s(int64(x))\n", typ)
case idealUint:
return fmt.Sprintf("if x >= 0 { return %s(int64(x)) }\n", typ)
case qInt8:
return fmt.Sprintf("if x >= math.MinInt8 && x<= math.MaxInt8 { return %s(int64(x)) }\n", typ)
case qInt16:
return fmt.Sprintf("if x >= math.MinInt16 && x<= math.MaxInt16 { return %s(int64(x)) }\n", typ)
case qInt32:
return fmt.Sprintf("if x >= math.MinInt32 && x<= math.MaxInt32 { return %s(int64(x)) }\n", typ)
case qUint8:
return fmt.Sprintf("if x >= 0 && x<= math.MaxUint8 { return %s(int64(x)) }\n", typ)
case qUint16:
return fmt.Sprintf("if x >= 0 && x<= math.MaxUint16 { return %s(int64(x)) }\n", typ)
case qUint32:
return fmt.Sprintf("if x >= 0 && x<= math.MaxUint32 { return %s(int64(x)) }\n", typ)
case qUint64:
return fmt.Sprintf("if x >= 0 { return %s(int64(x)) }\n", typ)
case qBigInt:
return fmt.Sprintf("return big.NewInt(int64(x))\n")
case qBigRat:
return fmt.Sprintf("return big.NewRat(1, 1).SetInt64(int64(x))\n")
case qDuration:
return fmt.Sprintf("return time.Duration(int64(x))\n")
default:
return ""
}
return ""
}
func coerceIdealRune(typ t) string {
switch typ {
case idealComplex:
return fmt.Sprintf("return %s(complex(float64(x), 0))\n", typ)
case qComplex64:
return fmt.Sprintf("return %s(complex(float32(x), 0))\n", typ)
case qComplex128:
return fmt.Sprintf("return %s(complex(float64(x), 0))\n", typ)
case idealFloat, idealInt, idealRune, idealUint, qFloat32, qFloat64, qInt8, qInt16, qInt32, qInt64, qUint8, qUint16, qUint32, qUint64:
return fmt.Sprintf("return %s(int64(x))\n", typ)
case qBigInt:
return fmt.Sprintf("return big.NewInt(int64(x))\n")
case qBigRat:
return fmt.Sprintf("return big.NewRat(1, 1).SetInt64(int64(x))\n")
case qDuration:
return fmt.Sprintf("return time.Duration(int64(x))\n")
default:
return ""
}
return ""
}
func coerceIdealUint(typ t) string {
switch typ {
case idealComplex:
return fmt.Sprintf("return %s(complex(float64(x), 0))\n", typ)
case qComplex64:
return fmt.Sprintf("return %s(complex(float32(x), 0))\n", typ)
case qComplex128:
return fmt.Sprintf("return %s(complex(float64(x), 0))\n", typ)
case idealFloat, idealUint, qFloat32, qFloat64, qUint64:
return fmt.Sprintf("return %s(uint64(x))\n", typ)
case idealInt:
return fmt.Sprintf("if x <= math.MaxInt64 { return %s(int64(x)) }\n", typ)
case qInt8:
return fmt.Sprintf("if x <= math.MaxInt8 { return %s(int64(x)) }\n", typ)
case qInt16:
return fmt.Sprintf("if x<= math.MaxInt16 { return %s(int64(x)) }\n", typ)
case qInt32:
return fmt.Sprintf("if x<= math.MaxInt32 { return %s(int64(x)) }\n", typ)
case qInt64:
return fmt.Sprintf("if x<= math.MaxInt64 { return %s(int64(x)) }\n", typ)
case qUint8:
return fmt.Sprintf("if x >= 0 && x<= math.MaxUint8 { return %s(int64(x)) }\n", typ)
case qUint16:
return fmt.Sprintf("if x >= 0 && x<= math.MaxUint16 { return %s(int64(x)) }\n", typ)
case qUint32:
return fmt.Sprintf("if x >= 0 && x<= math.MaxUint32 { return %s(int64(x)) }\n", typ)
case qBigInt:
return fmt.Sprintf("return big.NewInt(0).SetUint64(uint64(x))\n")
case qBigRat:
return fmt.Sprintf("return big.NewRat(1, 1).SetInt(big.NewInt(0).SetUint64(uint64(x)))\n")
case qDuration:
return fmt.Sprintf("if x <= math.MaxInt64 { return time.Duration(int64(x)) }\n")
default:
return ""
}
return ""
}
func genCoerce1(w io.Writer, in t, f func(out t) string) {
fmt.Fprintf(w, "\tcase %s:\n", in)
fmt.Fprintf(w, "\t\tswitch otherVal.(type) {\n")
for i := idealComplex; i < qEnd; i++ {
s := f(i)
switch s {
case "":
fmt.Fprintf(w, "\t\t//case %s:\n", i)
default:
fmt.Fprintf(w, "\t\tcase %s:\n", i)
fmt.Fprintf(w, "\t\t\t%s", s)
}
}
fmt.Fprintf(w, "\t\t}\n") // switch
}
func genCoerce(w io.Writer) {
fmt.Fprintf(w,
`
func coerce1(inVal, otherVal interface{}) (coercedInVal interface{}) {
coercedInVal = inVal
if otherVal == nil {
return
}
switch x := inVal.(type) {
case nil:
return
`)
genCoerce1(w, idealComplex, coerceIdealComplex)
genCoerce1(w, idealFloat, coerceIdealFloat)
genCoerce1(w, idealInt, coerceIdealInt)
genCoerce1(w, idealRune, coerceIdealRune)
genCoerce1(w, idealUint, coerceIdealUint)
fmt.Fprintf(w, "\t}\n") // switch
fmt.Fprintf(w, "\treturn\n}\n") // func
}
func main() {
ofn := flag.String("o", "", "")
flag.Parse()
_, err := os.Stat(*ofn)
if err == nil {
log.Fatalf("%s exists", *ofn)
}
w := bufio.NewWriter(os.Stdout)
if s := *ofn; s != "" {
f, err := os.Create(s)
if err != nil {
log.Fatal(err)
}
defer f.Close()
w = bufio.NewWriter(f)
}
defer w.Flush()
fmt.Fprintf(w, `// Copyright 2013 The ql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// CAUTION: This file was generated automatically by
//
// $ go run helper/helper.go -o coerce.go
//
// DO NOT EDIT!
package ql
import (
"math"
"math/big"
"reflect"
"time"
)
func coerce(a, b interface{}) (x, y interface{}) {
if reflect.TypeOf(a) == reflect.TypeOf(b) {
return a, b
}
switch a.(type) {
case idealComplex, idealFloat, idealInt, idealRune, idealUint:
switch b.(type) {
case idealComplex, idealFloat, idealInt, idealRune, idealUint:
x, y = coerce1(a, b), b
if reflect.TypeOf(x) == reflect.TypeOf(y) {
return
}
return a, coerce1(b, a)
default:
return coerce1(a, b), b
}
default:
switch b.(type) {
case idealComplex, idealFloat, idealInt, idealRune, idealUint:
return a, coerce1(b, a)
default:
return a, b
}
}
}
`)
genCoerce(w)
}
|