eval.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. package expr
  2. import (
  3. "fmt"
  4. "hash/crc32"
  5. "math"
  6. "math/rand"
  7. "reflect"
  8. "strings"
  9. )
  10. const kEpsilon = 1e-7
  11. type Env map[Var]interface{}
  12. type runtimePanic string
  13. func EvalBool(expr Expr, env Env) (value bool, err error) {
  14. defer func() {
  15. switch x := recover().(type) {
  16. case nil:
  17. // no panic
  18. case runtimePanic:
  19. value = false
  20. err = fmt.Errorf("%s", x)
  21. default:
  22. // unexpected panic: resume state of panic.
  23. panic(x)
  24. }
  25. }()
  26. if expr == nil {
  27. return false, nil
  28. }
  29. value = ConvertToBool(expr.Eval(env))
  30. return
  31. }
  32. func EvalInt(expr Expr, env Env) (value int64, err error) {
  33. defer func() {
  34. switch x := recover().(type) {
  35. case nil:
  36. // no panic
  37. case runtimePanic:
  38. value = 0
  39. err = fmt.Errorf("%s", x)
  40. default:
  41. // unexpected panic: resume state of panic.
  42. panic(x)
  43. }
  44. }()
  45. if expr == nil {
  46. return 0, nil
  47. }
  48. value = ConvertToInt(expr.Eval(env))
  49. return
  50. }
  51. func (v Var) Eval(env Env) reflect.Value {
  52. switch v {
  53. case "true":
  54. return reflect.ValueOf(true)
  55. case "false":
  56. return reflect.ValueOf(false)
  57. default:
  58. if i, ok := env[v]; ok {
  59. return reflect.ValueOf(i)
  60. }
  61. panic(runtimePanic(fmt.Sprintf("undefined variable: %s", v)))
  62. }
  63. }
  64. func (l literal) Eval(_ Env) reflect.Value {
  65. return reflect.ValueOf(l.value)
  66. }
  67. func (u unary) Eval(env Env) reflect.Value {
  68. switch u.op {
  69. case "+":
  70. return unaryPlus(u.x.Eval(env))
  71. case "-":
  72. return unaryMinus(u.x.Eval(env))
  73. case "!":
  74. return logicalNegation(u.x.Eval(env))
  75. case "~":
  76. return bitwiseComplement(u.x.Eval(env))
  77. }
  78. panic(runtimePanic(fmt.Sprintf("unsupported unary operator: %q", u.op)))
  79. }
  80. func (b binary) Eval(env Env) reflect.Value {
  81. switch b.op {
  82. case "+":
  83. return addition(b.x.Eval(env), b.y.Eval(env))
  84. case "-":
  85. return subtraction(b.x.Eval(env), b.y.Eval(env))
  86. case "*":
  87. return multiplication(b.x.Eval(env), b.y.Eval(env))
  88. case "/":
  89. return division(b.x.Eval(env), b.y.Eval(env))
  90. case "%":
  91. return modulus(b.x.Eval(env), b.y.Eval(env))
  92. case "&":
  93. return bitwiseAnd(b.x.Eval(env), b.y.Eval(env))
  94. case "&&":
  95. return logicalAnd(b.x.Eval(env), b.y.Eval(env))
  96. case "|":
  97. return bitwiseOr(b.x.Eval(env), b.y.Eval(env))
  98. case "||":
  99. return logicalOr(b.x.Eval(env), b.y.Eval(env))
  100. case "=", "==":
  101. return comparisonEqual(b.x.Eval(env), b.y.Eval(env))
  102. case ">":
  103. return comparisonGreater(b.x.Eval(env), b.y.Eval(env))
  104. case ">=":
  105. return comparisonGreaterOrEqual(b.x.Eval(env), b.y.Eval(env))
  106. case "<":
  107. return comparisonLess(b.x.Eval(env), b.y.Eval(env))
  108. case "<=":
  109. return comparisonLessOrEqual(b.x.Eval(env), b.y.Eval(env))
  110. case "!=":
  111. return comparisonNotEqual(b.x.Eval(env), b.y.Eval(env))
  112. }
  113. panic(runtimePanic(fmt.Sprintf("unsupported binary operator: %q", b.op)))
  114. }
  115. func (c call) Eval(env Env) reflect.Value {
  116. switch c.fn {
  117. case "pow":
  118. return reflect.ValueOf(math.Pow(ConvertToFloat(c.args[0].Eval(env)), ConvertToFloat(c.args[1].Eval(env))))
  119. case "sin":
  120. return reflect.ValueOf(math.Sin(ConvertToFloat(c.args[0].Eval(env))))
  121. case "sqrt":
  122. v := ConvertToFloat(c.args[0].Eval(env))
  123. if v < 0 {
  124. panic(runtimePanic(fmt.Sprintf("function call: %s only accept normal number", c.fn)))
  125. }
  126. return reflect.ValueOf(math.Sqrt(v))
  127. case "rand":
  128. return reflect.ValueOf(rand.Float64())
  129. case "log":
  130. v := ConvertToFloat(c.args[0].Eval(env))
  131. if v < 0 {
  132. panic(runtimePanic(fmt.Sprintf("function call: %s only accept normal number", c.fn)))
  133. }
  134. return reflect.ValueOf(math.Log10(v))
  135. case "to_upper":
  136. v := c.args[0].Eval(env)
  137. if v.Kind() != reflect.String {
  138. panic(runtimePanic(fmt.Sprintf("function call: %s only accept string", c.fn)))
  139. }
  140. return reflect.ValueOf(strings.ToUpper(v.String()))
  141. case "to_lower":
  142. v := c.args[0].Eval(env)
  143. if v.Kind() != reflect.String {
  144. panic(runtimePanic(fmt.Sprintf("function call: %s only accept string", c.fn)))
  145. }
  146. return reflect.ValueOf(strings.ToLower(v.String()))
  147. case "crc32":
  148. v := c.args[0].Eval(env)
  149. if v.Kind() != reflect.String {
  150. panic(runtimePanic(fmt.Sprintf("function call: %s only accept string", c.fn)))
  151. }
  152. return reflect.ValueOf(crc32.ChecksumIEEE([]byte(v.String())))
  153. }
  154. panic(runtimePanic(fmt.Sprintf("unsupported function call: %s", c.fn)))
  155. }
  156. func ConvertToBool(v reflect.Value) bool {
  157. switch v.Kind() {
  158. case reflect.Bool:
  159. return v.Bool()
  160. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  161. return v.Int() != 0
  162. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  163. return v.Uint() != 0
  164. case reflect.Float32, reflect.Float64:
  165. return v.Float() != 0
  166. case reflect.String:
  167. return v.String() != ""
  168. default:
  169. panic(runtimePanic(fmt.Sprintf("cannot convert data type: %s to bool", v.Kind().String())))
  170. }
  171. }
  172. func ConvertToInt(v reflect.Value) int64 {
  173. switch v.Kind() {
  174. case reflect.Bool:
  175. if v.Bool() {
  176. return 1
  177. } else {
  178. return 0
  179. }
  180. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  181. return v.Int()
  182. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  183. return int64(v.Uint())
  184. case reflect.Float32, reflect.Float64:
  185. return int64(v.Float())
  186. default:
  187. panic(runtimePanic(fmt.Sprintf("cannot convert data type: %s to int", v.Kind().String())))
  188. }
  189. }
  190. func ConvertToUint(v reflect.Value) uint64 {
  191. switch v.Kind() {
  192. case reflect.Bool:
  193. if v.Bool() {
  194. return 1
  195. } else {
  196. return 0
  197. }
  198. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  199. return uint64(v.Int())
  200. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  201. return v.Uint()
  202. case reflect.Float32, reflect.Float64:
  203. return uint64(v.Float())
  204. default:
  205. panic(runtimePanic(fmt.Sprintf("cannot convert data type: %s to uint", v.Kind().String())))
  206. }
  207. }
  208. func ConvertToFloat(v reflect.Value) float64 {
  209. switch v.Kind() {
  210. case reflect.Bool:
  211. if v.Bool() {
  212. return 1
  213. } else {
  214. return 0
  215. }
  216. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  217. return float64(v.Int())
  218. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  219. return float64(v.Uint())
  220. case reflect.Float32, reflect.Float64:
  221. return v.Float()
  222. default:
  223. panic(runtimePanic(fmt.Sprintf("cannot convert data type: %s to float", v.Kind().String())))
  224. }
  225. }
  226. func unaryPlus(v reflect.Value) reflect.Value {
  227. return v
  228. }
  229. func unaryMinus(v reflect.Value) reflect.Value {
  230. switch v.Kind() {
  231. case reflect.Bool:
  232. return v
  233. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  234. return reflect.ValueOf(-v.Int())
  235. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  236. return reflect.ValueOf(-v.Uint())
  237. case reflect.Float32, reflect.Float64:
  238. return reflect.ValueOf(-v.Float())
  239. default:
  240. panic(runtimePanic(fmt.Sprintf("%v(%s) not support unary minus", v.Interface(), v.Kind().String())))
  241. }
  242. }
  243. func logicalNegation(v reflect.Value) reflect.Value {
  244. return reflect.ValueOf(!ConvertToBool(v))
  245. }
  246. func bitwiseComplement(v reflect.Value) reflect.Value {
  247. switch v.Kind() {
  248. case reflect.Bool:
  249. return reflect.ValueOf(!v.Bool())
  250. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  251. return reflect.ValueOf(^v.Int())
  252. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  253. return reflect.ValueOf(^v.Uint())
  254. case reflect.Float32, reflect.Float64:
  255. panic(runtimePanic("cannot eval ~ for float"))
  256. default:
  257. panic(runtimePanic(fmt.Sprintf("%v(%s) not support bitwise complement", v.Interface(), v.Kind().String())))
  258. }
  259. }
  260. func typeLevel(k reflect.Kind) int {
  261. switch k {
  262. case reflect.String:
  263. return 5
  264. case reflect.Float32, reflect.Float64:
  265. return 4
  266. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  267. return 3
  268. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  269. return 2
  270. case reflect.Bool:
  271. return 1
  272. default:
  273. return 0
  274. }
  275. }
  276. func typeAscend(a reflect.Kind, b reflect.Kind) reflect.Kind {
  277. if typeLevel(a) >= typeLevel(b) {
  278. return a
  279. } else {
  280. return b
  281. }
  282. }
  283. func addition(left reflect.Value, right reflect.Value) reflect.Value {
  284. k := typeAscend(left.Kind(), right.Kind())
  285. switch k {
  286. case reflect.Float32, reflect.Float64:
  287. r := ConvertToFloat(left) + ConvertToFloat(right)
  288. return reflect.ValueOf(r)
  289. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  290. r := ConvertToUint(left) + ConvertToUint(right)
  291. return reflect.ValueOf(r)
  292. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  293. r := ConvertToInt(left) + ConvertToInt(right)
  294. return reflect.ValueOf(r)
  295. case reflect.Bool:
  296. r := ConvertToInt(left) + ConvertToInt(right)
  297. return reflect.ValueOf(r != 0)
  298. default:
  299. panic(runtimePanic(fmt.Sprintf("%v(%s) and %v(%s) not support addition",
  300. left.Interface(), left.Kind().String(), right.Interface(), right.Kind().String())))
  301. }
  302. }
  303. func subtraction(left reflect.Value, right reflect.Value) reflect.Value {
  304. k := typeAscend(left.Kind(), right.Kind())
  305. switch k {
  306. case reflect.Float32, reflect.Float64:
  307. r := ConvertToFloat(left) - ConvertToFloat(right)
  308. return reflect.ValueOf(r)
  309. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  310. r := ConvertToUint(left) - ConvertToUint(right)
  311. return reflect.ValueOf(r)
  312. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  313. r := ConvertToInt(left) - ConvertToInt(right)
  314. return reflect.ValueOf(r)
  315. case reflect.Bool:
  316. r := ConvertToInt(left) - ConvertToInt(right)
  317. return reflect.ValueOf(r != 0)
  318. default:
  319. panic(runtimePanic(fmt.Sprintf("%v(%s) and %v(%s) not support subtraction",
  320. left.Interface(), left.Kind().String(), right.Interface(), right.Kind().String())))
  321. }
  322. }
  323. func multiplication(left reflect.Value, right reflect.Value) reflect.Value {
  324. k := typeAscend(left.Kind(), right.Kind())
  325. switch k {
  326. case reflect.Float32, reflect.Float64:
  327. r := ConvertToFloat(left) * ConvertToFloat(right)
  328. return reflect.ValueOf(r)
  329. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  330. r := ConvertToUint(left) * ConvertToUint(right)
  331. return reflect.ValueOf(r)
  332. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  333. r := ConvertToInt(left) * ConvertToInt(right)
  334. return reflect.ValueOf(r)
  335. case reflect.Bool:
  336. r := ConvertToInt(left) * ConvertToInt(right)
  337. return reflect.ValueOf(r != 0)
  338. default:
  339. panic(runtimePanic(fmt.Sprintf("%v(%s) and %v(%s) not support multiplication",
  340. left.Interface(), left.Kind().String(), right.Interface(), right.Kind().String())))
  341. }
  342. }
  343. func division(left reflect.Value, right reflect.Value) reflect.Value {
  344. k := typeAscend(left.Kind(), right.Kind())
  345. switch k {
  346. case reflect.Float32, reflect.Float64:
  347. lv := ConvertToFloat(left)
  348. rv := ConvertToFloat(right)
  349. if math.Abs(rv) < kEpsilon {
  350. panic(runtimePanic(fmt.Sprintf("%f div %f, divide by zero", lv, rv)))
  351. }
  352. return reflect.ValueOf(lv / rv)
  353. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  354. lv := ConvertToUint(left)
  355. rv := ConvertToUint(right)
  356. if rv == 0 {
  357. panic(runtimePanic(fmt.Sprintf("%d div %d, divide by zero", lv, rv)))
  358. }
  359. return reflect.ValueOf(lv / rv)
  360. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  361. lv := ConvertToInt(left)
  362. rv := ConvertToInt(right)
  363. if rv == 0 {
  364. panic(runtimePanic(fmt.Sprintf("%d div %d, divide by zero", lv, rv)))
  365. }
  366. return reflect.ValueOf(lv / rv)
  367. case reflect.Bool:
  368. lv := ConvertToInt(left)
  369. rv := ConvertToInt(right)
  370. if rv == 0 {
  371. panic(runtimePanic(fmt.Sprintf("%d div %d, divide by zero", lv, rv)))
  372. }
  373. return reflect.ValueOf(lv/rv != 0)
  374. default:
  375. panic(runtimePanic(fmt.Sprintf("%v(%s) and %v(%s) not support division",
  376. left.Interface(), left.Kind().String(), right.Interface(), right.Kind().String())))
  377. }
  378. }
  379. func modulus(left reflect.Value, right reflect.Value) reflect.Value {
  380. k := typeAscend(left.Kind(), right.Kind())
  381. switch k {
  382. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  383. r := ConvertToUint(left) % ConvertToUint(right)
  384. return reflect.ValueOf(r)
  385. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  386. r := ConvertToInt(left) % ConvertToInt(right)
  387. return reflect.ValueOf(r)
  388. case reflect.Bool:
  389. r := ConvertToInt(left) % ConvertToInt(right)
  390. return reflect.ValueOf(r != 0)
  391. default:
  392. panic(runtimePanic(fmt.Sprintf("%v(%s) and %v(%s) not support division",
  393. left.Interface(), left.Kind().String(), right.Interface(), right.Kind().String())))
  394. }
  395. }
  396. func bitwiseAnd(left reflect.Value, right reflect.Value) reflect.Value {
  397. k := typeAscend(left.Kind(), right.Kind())
  398. switch k {
  399. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  400. r := ConvertToUint(left) & ConvertToUint(right)
  401. return reflect.ValueOf(r)
  402. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  403. r := ConvertToInt(left) & ConvertToInt(right)
  404. return reflect.ValueOf(r)
  405. case reflect.Bool:
  406. r := ConvertToBool(left) && ConvertToBool(right)
  407. return reflect.ValueOf(r)
  408. default:
  409. panic(runtimePanic(fmt.Sprintf("%v(%s) and %v(%s) not support bitwise and",
  410. left.Interface(), left.Kind().String(), right.Interface(), right.Kind().String())))
  411. }
  412. }
  413. func bitwiseOr(left reflect.Value, right reflect.Value) reflect.Value {
  414. k := typeAscend(left.Kind(), right.Kind())
  415. switch k {
  416. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  417. r := ConvertToUint(left) | ConvertToUint(right)
  418. return reflect.ValueOf(r)
  419. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  420. r := ConvertToInt(left) | ConvertToInt(right)
  421. return reflect.ValueOf(r)
  422. case reflect.Bool:
  423. r := ConvertToBool(left) || ConvertToBool(right)
  424. return reflect.ValueOf(r)
  425. default:
  426. panic(runtimePanic(fmt.Sprintf("%v(%s) and %v(%s) not support bitwise or",
  427. left.Interface(), left.Kind().String(), right.Interface(), right.Kind().String())))
  428. }
  429. }
  430. func logicalAnd(left reflect.Value, right reflect.Value) reflect.Value {
  431. r := ConvertToBool(left) && ConvertToBool(right)
  432. return reflect.ValueOf(r)
  433. }
  434. func logicalOr(left reflect.Value, right reflect.Value) reflect.Value {
  435. r := ConvertToBool(left) || ConvertToBool(right)
  436. return reflect.ValueOf(r)
  437. }
  438. func comparisonEqual(left reflect.Value, right reflect.Value) reflect.Value {
  439. k := typeAscend(left.Kind(), right.Kind())
  440. switch k {
  441. case reflect.String:
  442. if left.Kind() != reflect.String || right.Kind() != reflect.String {
  443. panic(runtimePanic(fmt.Sprintf("%v(%s) and %v(%s) not support comparison equal",
  444. left.Interface(), left.Kind().String(), right.Interface(), right.Kind().String())))
  445. }
  446. r := strings.Compare(left.String(), right.String()) == 0
  447. return reflect.ValueOf(r)
  448. case reflect.Float32, reflect.Float64:
  449. r := ConvertToFloat(left) == ConvertToFloat(right)
  450. return reflect.ValueOf(r)
  451. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  452. r := ConvertToUint(left) == ConvertToUint(right)
  453. return reflect.ValueOf(r)
  454. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  455. r := ConvertToInt(left) == ConvertToInt(right)
  456. return reflect.ValueOf(r)
  457. case reflect.Bool:
  458. r := ConvertToInt(left) == ConvertToInt(right)
  459. return reflect.ValueOf(r)
  460. default:
  461. panic(runtimePanic(fmt.Sprintf("%v(%s) and %v(%s) not support comparison equal",
  462. left.Interface(), left.Kind().String(), right.Interface(), right.Kind().String())))
  463. }
  464. }
  465. func comparisonNotEqual(left reflect.Value, right reflect.Value) reflect.Value {
  466. k := typeAscend(left.Kind(), right.Kind())
  467. switch k {
  468. case reflect.String:
  469. if left.Kind() != reflect.String || right.Kind() != reflect.String {
  470. panic(runtimePanic(fmt.Sprintf("%v(%s) and %v(%s) not support comparison equal",
  471. left.Interface(), left.Kind().String(), right.Interface(), right.Kind().String())))
  472. }
  473. r := strings.Compare(left.String(), right.String()) != 0
  474. return reflect.ValueOf(r)
  475. case reflect.Float32, reflect.Float64:
  476. r := ConvertToFloat(left) != ConvertToFloat(right)
  477. return reflect.ValueOf(r)
  478. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  479. r := ConvertToUint(left) != ConvertToUint(right)
  480. return reflect.ValueOf(r)
  481. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  482. r := ConvertToInt(left) != ConvertToInt(right)
  483. return reflect.ValueOf(r)
  484. case reflect.Bool:
  485. r := ConvertToInt(left) != ConvertToInt(right)
  486. return reflect.ValueOf(r)
  487. default:
  488. panic(runtimePanic(fmt.Sprintf("type %s and %s not support comparison not equal", left.Kind().String(), right.Kind().String())))
  489. }
  490. }
  491. func comparisonGreater(left reflect.Value, right reflect.Value) reflect.Value {
  492. k := typeAscend(left.Kind(), right.Kind())
  493. switch k {
  494. case reflect.Float32, reflect.Float64:
  495. r := ConvertToFloat(left) > ConvertToFloat(right)
  496. return reflect.ValueOf(r)
  497. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  498. r := ConvertToUint(left) > ConvertToUint(right)
  499. return reflect.ValueOf(r)
  500. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  501. r := ConvertToInt(left) > ConvertToInt(right)
  502. return reflect.ValueOf(r)
  503. case reflect.Bool:
  504. r := ConvertToInt(left) > ConvertToInt(right)
  505. return reflect.ValueOf(r)
  506. default:
  507. panic(runtimePanic(fmt.Sprintf("type %s and %s not support comparison greater", left.Kind().String(), right.Kind().String())))
  508. }
  509. }
  510. func comparisonGreaterOrEqual(left reflect.Value, right reflect.Value) reflect.Value {
  511. k := typeAscend(left.Kind(), right.Kind())
  512. switch k {
  513. case reflect.Float32, reflect.Float64:
  514. r := ConvertToFloat(left) >= ConvertToFloat(right)
  515. return reflect.ValueOf(r)
  516. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  517. r := ConvertToUint(left) >= ConvertToUint(right)
  518. return reflect.ValueOf(r)
  519. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  520. r := ConvertToInt(left) >= ConvertToInt(right)
  521. return reflect.ValueOf(r)
  522. case reflect.Bool:
  523. r := ConvertToInt(left) >= ConvertToInt(right)
  524. return reflect.ValueOf(r)
  525. default:
  526. panic(runtimePanic(fmt.Sprintf("type %s and %s not support comparison greater or equal", left.Kind().String(), right.Kind().String())))
  527. }
  528. }
  529. func comparisonLess(left reflect.Value, right reflect.Value) reflect.Value {
  530. k := typeAscend(left.Kind(), right.Kind())
  531. switch k {
  532. case reflect.Float32, reflect.Float64:
  533. r := ConvertToFloat(left) < ConvertToFloat(right)
  534. return reflect.ValueOf(r)
  535. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  536. r := ConvertToUint(left) < ConvertToUint(right)
  537. return reflect.ValueOf(r)
  538. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  539. r := ConvertToInt(left) < ConvertToInt(right)
  540. return reflect.ValueOf(r)
  541. case reflect.Bool:
  542. r := ConvertToInt(left) < ConvertToInt(right)
  543. return reflect.ValueOf(r)
  544. default:
  545. panic(runtimePanic(fmt.Sprintf("type %s and %s not support comparison less", left.Kind().String(), right.Kind().String())))
  546. }
  547. }
  548. func comparisonLessOrEqual(left reflect.Value, right reflect.Value) reflect.Value {
  549. k := typeAscend(left.Kind(), right.Kind())
  550. switch k {
  551. case reflect.Float32, reflect.Float64:
  552. r := ConvertToFloat(left) <= ConvertToFloat(right)
  553. return reflect.ValueOf(r)
  554. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  555. r := ConvertToUint(left) <= ConvertToUint(right)
  556. return reflect.ValueOf(r)
  557. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  558. r := ConvertToInt(left) <= ConvertToInt(right)
  559. return reflect.ValueOf(r)
  560. case reflect.Bool:
  561. r := ConvertToInt(left) <= ConvertToInt(right)
  562. return reflect.ValueOf(r)
  563. default:
  564. panic(runtimePanic(fmt.Sprintf("type %s and %s not support comparison less or equal", left.Kind().String(), right.Kind().String())))
  565. }
  566. }