coerce.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Copyright 2013 The ql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // CAUTION: This file was generated automatically by
  5. //
  6. // $ go run helper/helper.go -o coerce.go
  7. //
  8. // DO NOT EDIT!
  9. package ql
  10. import (
  11. "math"
  12. "math/big"
  13. "reflect"
  14. "time"
  15. )
  16. func coerce(a, b interface{}) (x, y interface{}) {
  17. if reflect.TypeOf(a) == reflect.TypeOf(b) {
  18. return a, b
  19. }
  20. switch a.(type) {
  21. case idealComplex, idealFloat, idealInt, idealRune, idealUint:
  22. switch b.(type) {
  23. case idealComplex, idealFloat, idealInt, idealRune, idealUint:
  24. x, y = coerce1(a, b), b
  25. if reflect.TypeOf(x) == reflect.TypeOf(y) {
  26. return
  27. }
  28. return a, coerce1(b, a)
  29. default:
  30. return coerce1(a, b), b
  31. }
  32. default:
  33. switch b.(type) {
  34. case idealComplex, idealFloat, idealInt, idealRune, idealUint:
  35. return a, coerce1(b, a)
  36. default:
  37. return a, b
  38. }
  39. }
  40. }
  41. func coerce1(inVal, otherVal interface{}) (coercedInVal interface{}) {
  42. coercedInVal = inVal
  43. if otherVal == nil {
  44. return
  45. }
  46. switch x := inVal.(type) {
  47. case nil:
  48. return
  49. case idealComplex:
  50. switch otherVal.(type) {
  51. //case idealComplex:
  52. //case idealFloat:
  53. //case idealInt:
  54. //case idealRune:
  55. //case idealUint:
  56. //case bool:
  57. case complex64:
  58. return complex64(x)
  59. case complex128:
  60. return complex128(x)
  61. //case float32:
  62. //case float64:
  63. //case int8:
  64. //case int16:
  65. //case int32:
  66. //case int64:
  67. //case string:
  68. //case uint8:
  69. //case uint16:
  70. //case uint32:
  71. //case uint64:
  72. //case *big.Int:
  73. //case *big.Rat:
  74. //case time.Time:
  75. //case time.Duration:
  76. }
  77. case idealFloat:
  78. switch otherVal.(type) {
  79. case idealComplex:
  80. return idealComplex(complex(float64(x), 0))
  81. case idealFloat:
  82. return idealFloat(float64(x))
  83. //case idealInt:
  84. //case idealRune:
  85. //case idealUint:
  86. //case bool:
  87. case complex64:
  88. return complex(float32(x), 0)
  89. case complex128:
  90. return complex(float64(x), 0)
  91. case float32:
  92. return float32(float64(x))
  93. case float64:
  94. return float64(x)
  95. //case int8:
  96. //case int16:
  97. //case int32:
  98. //case int64:
  99. //case string:
  100. //case uint8:
  101. //case uint16:
  102. //case uint32:
  103. //case uint64:
  104. //case *big.Int:
  105. case *big.Rat:
  106. return big.NewRat(1, 1).SetFloat64(float64(x))
  107. //case time.Time:
  108. //case time.Duration:
  109. }
  110. case idealInt:
  111. switch otherVal.(type) {
  112. case idealComplex:
  113. return idealComplex(complex(float64(x), 0))
  114. case idealFloat:
  115. return idealFloat(int64(x))
  116. case idealInt:
  117. return idealInt(int64(x))
  118. //case idealRune:
  119. case idealUint:
  120. if x >= 0 {
  121. return idealUint(int64(x))
  122. }
  123. //case bool:
  124. case complex64:
  125. return complex(float32(x), 0)
  126. case complex128:
  127. return complex(float64(x), 0)
  128. case float32:
  129. return float32(int64(x))
  130. case float64:
  131. return float64(int64(x))
  132. case int8:
  133. if x >= math.MinInt8 && x <= math.MaxInt8 {
  134. return int8(int64(x))
  135. }
  136. case int16:
  137. if x >= math.MinInt16 && x <= math.MaxInt16 {
  138. return int16(int64(x))
  139. }
  140. case int32:
  141. if x >= math.MinInt32 && x <= math.MaxInt32 {
  142. return int32(int64(x))
  143. }
  144. case int64:
  145. return int64(x)
  146. //case string:
  147. case uint8:
  148. if x >= 0 && x <= math.MaxUint8 {
  149. return uint8(int64(x))
  150. }
  151. case uint16:
  152. if x >= 0 && x <= math.MaxUint16 {
  153. return uint16(int64(x))
  154. }
  155. case uint32:
  156. if x >= 0 && x <= math.MaxUint32 {
  157. return uint32(int64(x))
  158. }
  159. case uint64:
  160. if x >= 0 {
  161. return uint64(int64(x))
  162. }
  163. case *big.Int:
  164. return big.NewInt(int64(x))
  165. case *big.Rat:
  166. return big.NewRat(1, 1).SetInt64(int64(x))
  167. //case time.Time:
  168. case time.Duration:
  169. return time.Duration(int64(x))
  170. }
  171. case idealRune:
  172. switch otherVal.(type) {
  173. case idealComplex:
  174. return idealComplex(complex(float64(x), 0))
  175. case idealFloat:
  176. return idealFloat(int64(x))
  177. case idealInt:
  178. return idealInt(int64(x))
  179. case idealRune:
  180. return idealRune(int64(x))
  181. case idealUint:
  182. return idealUint(int64(x))
  183. //case bool:
  184. case complex64:
  185. return complex(float32(x), 0)
  186. case complex128:
  187. return complex(float64(x), 0)
  188. case float32:
  189. return float32(int64(x))
  190. case float64:
  191. return float64(int64(x))
  192. case int8:
  193. return int8(int64(x))
  194. case int16:
  195. return int16(int64(x))
  196. case int32:
  197. return int32(int64(x))
  198. case int64:
  199. return int64(x)
  200. //case string:
  201. case uint8:
  202. return uint8(int64(x))
  203. case uint16:
  204. return uint16(int64(x))
  205. case uint32:
  206. return uint32(int64(x))
  207. case uint64:
  208. return uint64(int64(x))
  209. case *big.Int:
  210. return big.NewInt(int64(x))
  211. case *big.Rat:
  212. return big.NewRat(1, 1).SetInt64(int64(x))
  213. //case time.Time:
  214. case time.Duration:
  215. return time.Duration(int64(x))
  216. }
  217. case idealUint:
  218. switch otherVal.(type) {
  219. case idealComplex:
  220. return idealComplex(complex(float64(x), 0))
  221. case idealFloat:
  222. return idealFloat(uint64(x))
  223. case idealInt:
  224. if x <= math.MaxInt64 {
  225. return idealInt(int64(x))
  226. }
  227. //case idealRune:
  228. case idealUint:
  229. return idealUint(uint64(x))
  230. //case bool:
  231. case complex64:
  232. return complex(float32(x), 0)
  233. case complex128:
  234. return complex(float64(x), 0)
  235. case float32:
  236. return float32(uint64(x))
  237. case float64:
  238. return float64(uint64(x))
  239. case int8:
  240. if x <= math.MaxInt8 {
  241. return int8(int64(x))
  242. }
  243. case int16:
  244. if x <= math.MaxInt16 {
  245. return int16(int64(x))
  246. }
  247. case int32:
  248. if x <= math.MaxInt32 {
  249. return int32(int64(x))
  250. }
  251. case int64:
  252. if x <= math.MaxInt64 {
  253. return int64(x)
  254. }
  255. //case string:
  256. case uint8:
  257. if x >= 0 && x <= math.MaxUint8 {
  258. return uint8(int64(x))
  259. }
  260. case uint16:
  261. if x >= 0 && x <= math.MaxUint16 {
  262. return uint16(int64(x))
  263. }
  264. case uint32:
  265. if x >= 0 && x <= math.MaxUint32 {
  266. return uint32(int64(x))
  267. }
  268. case uint64:
  269. return uint64(x)
  270. case *big.Int:
  271. return big.NewInt(0).SetUint64(uint64(x))
  272. case *big.Rat:
  273. return big.NewRat(1, 1).SetInt(big.NewInt(0).SetUint64(uint64(x)))
  274. //case time.Time:
  275. case time.Duration:
  276. if x <= math.MaxInt64 {
  277. return time.Duration(int64(x))
  278. }
  279. }
  280. }
  281. return
  282. }