helper.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // +build ignore
  2. // Copyright 2014 The ql Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package main
  6. import (
  7. "bufio"
  8. "flag"
  9. "fmt"
  10. "io"
  11. "log"
  12. "os"
  13. )
  14. type t int
  15. const (
  16. qNil t = iota
  17. idealComplex
  18. idealFloat
  19. idealInt
  20. idealRune
  21. idealUint
  22. qBool
  23. qComplex64
  24. qComplex128
  25. qFloat32
  26. qFloat64
  27. qInt8
  28. qInt16
  29. qInt32
  30. qInt64
  31. qString
  32. qUint8
  33. qUint16
  34. qUint32
  35. qUint64
  36. qBigInt
  37. qBigRat
  38. qTime
  39. qDuration
  40. qEnd
  41. )
  42. func (n t) String() string {
  43. switch n {
  44. case qNil:
  45. return "nil"
  46. case idealComplex:
  47. return "idealComplex"
  48. case idealFloat:
  49. return "idealFloat"
  50. case idealInt:
  51. return "idealInt"
  52. case idealRune:
  53. return "idealRune"
  54. case idealUint:
  55. return "idealUint"
  56. case qBool:
  57. return "bool"
  58. case qComplex64:
  59. return "complex64"
  60. case qComplex128:
  61. return "complex128"
  62. case qFloat32:
  63. return "float32"
  64. case qFloat64:
  65. return "float64"
  66. case qInt8:
  67. return "int8"
  68. case qInt16:
  69. return "int16"
  70. case qInt32:
  71. return "int32"
  72. case qInt64:
  73. return "int64"
  74. case qString:
  75. return "string"
  76. case qUint8:
  77. return "uint8"
  78. case qUint16:
  79. return "uint16"
  80. case qUint32:
  81. return "uint32"
  82. case qUint64:
  83. return "uint64"
  84. case qBigInt:
  85. return "*big.Int"
  86. case qBigRat:
  87. return "*big.Rat"
  88. case qTime:
  89. return "time.Time"
  90. case qDuration:
  91. return "time.Duration"
  92. default:
  93. panic("internal error 046")
  94. }
  95. }
  96. func coerceIdealComplex(typ t) string {
  97. switch typ {
  98. case qComplex64, qComplex128:
  99. return fmt.Sprintf("return %s(x)\n", typ)
  100. default:
  101. return ""
  102. }
  103. }
  104. func coerceIdealFloat(typ t) string {
  105. switch typ {
  106. case idealComplex:
  107. return fmt.Sprintf("return %s(complex(float64(x), 0))\n", typ)
  108. case qComplex64:
  109. return fmt.Sprintf("return %s(complex(float32(x), 0))\n", typ)
  110. case qComplex128:
  111. return fmt.Sprintf("return %s(complex(float64(x), 0))\n", typ)
  112. case idealFloat, qFloat32, qFloat64:
  113. return fmt.Sprintf("return %s(float64(x))\n", typ)
  114. case qBigRat:
  115. return fmt.Sprintf("return big.NewRat(1, 1).SetFloat64(float64(x))\n")
  116. default:
  117. return ""
  118. }
  119. return ""
  120. }
  121. func coerceIdealInt(typ t) string {
  122. switch typ {
  123. case idealComplex:
  124. return fmt.Sprintf("return %s(complex(float64(x), 0))\n", typ)
  125. case qComplex64:
  126. return fmt.Sprintf("return %s(complex(float32(x), 0))\n", typ)
  127. case qComplex128:
  128. return fmt.Sprintf("return %s(complex(float64(x), 0))\n", typ)
  129. case idealFloat, idealInt, qFloat32, qFloat64, qInt64:
  130. return fmt.Sprintf("return %s(int64(x))\n", typ)
  131. case idealUint:
  132. return fmt.Sprintf("if x >= 0 { return %s(int64(x)) }\n", typ)
  133. case qInt8:
  134. return fmt.Sprintf("if x >= math.MinInt8 && x<= math.MaxInt8 { return %s(int64(x)) }\n", typ)
  135. case qInt16:
  136. return fmt.Sprintf("if x >= math.MinInt16 && x<= math.MaxInt16 { return %s(int64(x)) }\n", typ)
  137. case qInt32:
  138. return fmt.Sprintf("if x >= math.MinInt32 && x<= math.MaxInt32 { return %s(int64(x)) }\n", typ)
  139. case qUint8:
  140. return fmt.Sprintf("if x >= 0 && x<= math.MaxUint8 { return %s(int64(x)) }\n", typ)
  141. case qUint16:
  142. return fmt.Sprintf("if x >= 0 && x<= math.MaxUint16 { return %s(int64(x)) }\n", typ)
  143. case qUint32:
  144. return fmt.Sprintf("if x >= 0 && x<= math.MaxUint32 { return %s(int64(x)) }\n", typ)
  145. case qUint64:
  146. return fmt.Sprintf("if x >= 0 { return %s(int64(x)) }\n", typ)
  147. case qBigInt:
  148. return fmt.Sprintf("return big.NewInt(int64(x))\n")
  149. case qBigRat:
  150. return fmt.Sprintf("return big.NewRat(1, 1).SetInt64(int64(x))\n")
  151. case qDuration:
  152. return fmt.Sprintf("return time.Duration(int64(x))\n")
  153. default:
  154. return ""
  155. }
  156. return ""
  157. }
  158. func coerceIdealRune(typ t) string {
  159. switch typ {
  160. case idealComplex:
  161. return fmt.Sprintf("return %s(complex(float64(x), 0))\n", typ)
  162. case qComplex64:
  163. return fmt.Sprintf("return %s(complex(float32(x), 0))\n", typ)
  164. case qComplex128:
  165. return fmt.Sprintf("return %s(complex(float64(x), 0))\n", typ)
  166. case idealFloat, idealInt, idealRune, idealUint, qFloat32, qFloat64, qInt8, qInt16, qInt32, qInt64, qUint8, qUint16, qUint32, qUint64:
  167. return fmt.Sprintf("return %s(int64(x))\n", typ)
  168. case qBigInt:
  169. return fmt.Sprintf("return big.NewInt(int64(x))\n")
  170. case qBigRat:
  171. return fmt.Sprintf("return big.NewRat(1, 1).SetInt64(int64(x))\n")
  172. case qDuration:
  173. return fmt.Sprintf("return time.Duration(int64(x))\n")
  174. default:
  175. return ""
  176. }
  177. return ""
  178. }
  179. func coerceIdealUint(typ t) string {
  180. switch typ {
  181. case idealComplex:
  182. return fmt.Sprintf("return %s(complex(float64(x), 0))\n", typ)
  183. case qComplex64:
  184. return fmt.Sprintf("return %s(complex(float32(x), 0))\n", typ)
  185. case qComplex128:
  186. return fmt.Sprintf("return %s(complex(float64(x), 0))\n", typ)
  187. case idealFloat, idealUint, qFloat32, qFloat64, qUint64:
  188. return fmt.Sprintf("return %s(uint64(x))\n", typ)
  189. case idealInt:
  190. return fmt.Sprintf("if x <= math.MaxInt64 { return %s(int64(x)) }\n", typ)
  191. case qInt8:
  192. return fmt.Sprintf("if x <= math.MaxInt8 { return %s(int64(x)) }\n", typ)
  193. case qInt16:
  194. return fmt.Sprintf("if x<= math.MaxInt16 { return %s(int64(x)) }\n", typ)
  195. case qInt32:
  196. return fmt.Sprintf("if x<= math.MaxInt32 { return %s(int64(x)) }\n", typ)
  197. case qInt64:
  198. return fmt.Sprintf("if x<= math.MaxInt64 { return %s(int64(x)) }\n", typ)
  199. case qUint8:
  200. return fmt.Sprintf("if x >= 0 && x<= math.MaxUint8 { return %s(int64(x)) }\n", typ)
  201. case qUint16:
  202. return fmt.Sprintf("if x >= 0 && x<= math.MaxUint16 { return %s(int64(x)) }\n", typ)
  203. case qUint32:
  204. return fmt.Sprintf("if x >= 0 && x<= math.MaxUint32 { return %s(int64(x)) }\n", typ)
  205. case qBigInt:
  206. return fmt.Sprintf("return big.NewInt(0).SetUint64(uint64(x))\n")
  207. case qBigRat:
  208. return fmt.Sprintf("return big.NewRat(1, 1).SetInt(big.NewInt(0).SetUint64(uint64(x)))\n")
  209. case qDuration:
  210. return fmt.Sprintf("if x <= math.MaxInt64 { return time.Duration(int64(x)) }\n")
  211. default:
  212. return ""
  213. }
  214. return ""
  215. }
  216. func genCoerce1(w io.Writer, in t, f func(out t) string) {
  217. fmt.Fprintf(w, "\tcase %s:\n", in)
  218. fmt.Fprintf(w, "\t\tswitch otherVal.(type) {\n")
  219. for i := idealComplex; i < qEnd; i++ {
  220. s := f(i)
  221. switch s {
  222. case "":
  223. fmt.Fprintf(w, "\t\t//case %s:\n", i)
  224. default:
  225. fmt.Fprintf(w, "\t\tcase %s:\n", i)
  226. fmt.Fprintf(w, "\t\t\t%s", s)
  227. }
  228. }
  229. fmt.Fprintf(w, "\t\t}\n") // switch
  230. }
  231. func genCoerce(w io.Writer) {
  232. fmt.Fprintf(w,
  233. `
  234. func coerce1(inVal, otherVal interface{}) (coercedInVal interface{}) {
  235. coercedInVal = inVal
  236. if otherVal == nil {
  237. return
  238. }
  239. switch x := inVal.(type) {
  240. case nil:
  241. return
  242. `)
  243. genCoerce1(w, idealComplex, coerceIdealComplex)
  244. genCoerce1(w, idealFloat, coerceIdealFloat)
  245. genCoerce1(w, idealInt, coerceIdealInt)
  246. genCoerce1(w, idealRune, coerceIdealRune)
  247. genCoerce1(w, idealUint, coerceIdealUint)
  248. fmt.Fprintf(w, "\t}\n") // switch
  249. fmt.Fprintf(w, "\treturn\n}\n") // func
  250. }
  251. func main() {
  252. ofn := flag.String("o", "", "")
  253. flag.Parse()
  254. _, err := os.Stat(*ofn)
  255. if err == nil {
  256. log.Fatalf("%s exists", *ofn)
  257. }
  258. w := bufio.NewWriter(os.Stdout)
  259. if s := *ofn; s != "" {
  260. f, err := os.Create(s)
  261. if err != nil {
  262. log.Fatal(err)
  263. }
  264. defer f.Close()
  265. w = bufio.NewWriter(f)
  266. }
  267. defer w.Flush()
  268. fmt.Fprintf(w, `// Copyright 2013 The ql Authors. All rights reserved.
  269. // Use of this source code is governed by a BSD-style
  270. // license that can be found in the LICENSE file.
  271. // CAUTION: This file was generated automatically by
  272. //
  273. // $ go run helper/helper.go -o coerce.go
  274. //
  275. // DO NOT EDIT!
  276. package ql
  277. import (
  278. "math"
  279. "math/big"
  280. "reflect"
  281. "time"
  282. )
  283. func coerce(a, b interface{}) (x, y interface{}) {
  284. if reflect.TypeOf(a) == reflect.TypeOf(b) {
  285. return a, b
  286. }
  287. switch a.(type) {
  288. case idealComplex, idealFloat, idealInt, idealRune, idealUint:
  289. switch b.(type) {
  290. case idealComplex, idealFloat, idealInt, idealRune, idealUint:
  291. x, y = coerce1(a, b), b
  292. if reflect.TypeOf(x) == reflect.TypeOf(y) {
  293. return
  294. }
  295. return a, coerce1(b, a)
  296. default:
  297. return coerce1(a, b), b
  298. }
  299. default:
  300. switch b.(type) {
  301. case idealComplex, idealFloat, idealInt, idealRune, idealUint:
  302. return a, coerce1(b, a)
  303. default:
  304. return a, b
  305. }
  306. }
  307. }
  308. `)
  309. genCoerce(w)
  310. }