vmOperator.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package vm
  2. import (
  3. "reflect"
  4. "strings"
  5. "github.com/mattn/anko/ast"
  6. )
  7. // invokeOperator evaluates one Operator.
  8. func (runInfo *runInfoStruct) invokeOperator() {
  9. switch operator := runInfo.operator.(type) {
  10. // BinaryOperator
  11. case *ast.BinaryOperator:
  12. runInfo.expr = operator.LHS
  13. runInfo.invokeExpr()
  14. if runInfo.err != nil {
  15. return
  16. }
  17. if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
  18. runInfo.rv = runInfo.rv.Elem()
  19. }
  20. switch operator.Operator {
  21. case "||":
  22. if toBool(runInfo.rv) {
  23. runInfo.rv = trueValue
  24. return
  25. }
  26. case "&&":
  27. if !toBool(runInfo.rv) {
  28. runInfo.rv = falseValue
  29. return
  30. }
  31. default:
  32. runInfo.err = newStringError(operator, "unknown operator")
  33. runInfo.rv = nilValue
  34. return
  35. }
  36. runInfo.expr = operator.RHS
  37. runInfo.invokeExpr()
  38. if runInfo.err != nil {
  39. return
  40. }
  41. if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
  42. runInfo.rv = runInfo.rv.Elem()
  43. }
  44. if toBool(runInfo.rv) {
  45. runInfo.rv = trueValue
  46. } else {
  47. runInfo.rv = falseValue
  48. }
  49. // ComparisonOperator
  50. case *ast.ComparisonOperator:
  51. runInfo.expr = operator.LHS
  52. runInfo.invokeExpr()
  53. if runInfo.err != nil {
  54. return
  55. }
  56. if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
  57. runInfo.rv = runInfo.rv.Elem()
  58. }
  59. lhsV := runInfo.rv
  60. runInfo.expr = operator.RHS
  61. runInfo.invokeExpr()
  62. if runInfo.err != nil {
  63. return
  64. }
  65. if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
  66. runInfo.rv = runInfo.rv.Elem()
  67. }
  68. switch operator.Operator {
  69. case "==":
  70. runInfo.rv = reflect.ValueOf(equal(lhsV, runInfo.rv))
  71. case "!=":
  72. runInfo.rv = reflect.ValueOf(!equal(lhsV, runInfo.rv))
  73. case "<":
  74. runInfo.rv = reflect.ValueOf(toFloat64(lhsV) < toFloat64(runInfo.rv))
  75. case "<=":
  76. runInfo.rv = reflect.ValueOf(toFloat64(lhsV) <= toFloat64(runInfo.rv))
  77. case ">":
  78. runInfo.rv = reflect.ValueOf(toFloat64(lhsV) > toFloat64(runInfo.rv))
  79. case ">=":
  80. runInfo.rv = reflect.ValueOf(toFloat64(lhsV) >= toFloat64(runInfo.rv))
  81. default:
  82. runInfo.err = newStringError(operator, "unknown operator")
  83. runInfo.rv = nilValue
  84. }
  85. // AddOperator
  86. case *ast.AddOperator:
  87. runInfo.expr = operator.LHS
  88. runInfo.invokeExpr()
  89. if runInfo.err != nil {
  90. return
  91. }
  92. if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
  93. runInfo.rv = runInfo.rv.Elem()
  94. }
  95. lhsV := runInfo.rv
  96. runInfo.expr = operator.RHS
  97. runInfo.invokeExpr()
  98. if runInfo.err != nil {
  99. return
  100. }
  101. if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
  102. runInfo.rv = runInfo.rv.Elem()
  103. }
  104. switch operator.Operator {
  105. case "+":
  106. lhsKind := lhsV.Kind()
  107. rhsKind := runInfo.rv.Kind()
  108. if lhsKind == reflect.Slice || lhsKind == reflect.Array {
  109. if rhsKind == reflect.Slice || rhsKind == reflect.Array {
  110. // append slice to slice
  111. runInfo.rv, runInfo.err = appendSlice(operator, lhsV, runInfo.rv)
  112. return
  113. }
  114. // try to append rhs non-slice to lhs slice
  115. runInfo.rv, runInfo.err = convertReflectValueToType(runInfo.rv, lhsV.Type().Elem())
  116. if runInfo.err != nil {
  117. runInfo.err = newStringError(operator, "invalid type conversion")
  118. runInfo.rv = nilValue
  119. return
  120. }
  121. runInfo.rv = reflect.Append(lhsV, runInfo.rv)
  122. return
  123. }
  124. if rhsKind == reflect.Slice || rhsKind == reflect.Array {
  125. // can not append rhs slice to lhs non-slice
  126. runInfo.err = newStringError(operator, "invalid type conversion")
  127. runInfo.rv = nilValue
  128. return
  129. }
  130. kind := precedenceOfKinds(lhsKind, rhsKind)
  131. switch kind {
  132. case reflect.String:
  133. runInfo.rv = reflect.ValueOf(toString(lhsV) + toString(runInfo.rv))
  134. case reflect.Float64, reflect.Float32:
  135. runInfo.rv = reflect.ValueOf(toFloat64(lhsV) + toFloat64(runInfo.rv))
  136. default:
  137. runInfo.rv = reflect.ValueOf(toInt64(lhsV) + toInt64(runInfo.rv))
  138. }
  139. case "-":
  140. switch lhsV.Kind() {
  141. case reflect.Float64, reflect.Float32:
  142. runInfo.rv = reflect.ValueOf(toFloat64(lhsV) - toFloat64(runInfo.rv))
  143. return
  144. }
  145. switch runInfo.rv.Kind() {
  146. case reflect.Float64, reflect.Float32:
  147. runInfo.rv = reflect.ValueOf(toFloat64(lhsV) - toFloat64(runInfo.rv))
  148. default:
  149. runInfo.rv = reflect.ValueOf(toInt64(lhsV) - toInt64(runInfo.rv))
  150. }
  151. case "|":
  152. runInfo.rv = reflect.ValueOf(toInt64(lhsV) | toInt64(runInfo.rv))
  153. default:
  154. runInfo.err = newStringError(operator, "unknown operator")
  155. runInfo.rv = nilValue
  156. }
  157. // MultiplyOperator
  158. case *ast.MultiplyOperator:
  159. runInfo.expr = operator.LHS
  160. runInfo.invokeExpr()
  161. if runInfo.err != nil {
  162. return
  163. }
  164. if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
  165. runInfo.rv = runInfo.rv.Elem()
  166. }
  167. lhsV := runInfo.rv
  168. runInfo.expr = operator.RHS
  169. runInfo.invokeExpr()
  170. if runInfo.err != nil {
  171. return
  172. }
  173. if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
  174. runInfo.rv = runInfo.rv.Elem()
  175. }
  176. switch operator.Operator {
  177. case "*":
  178. if lhsV.Kind() == reflect.String && (runInfo.rv.Kind() == reflect.Int || runInfo.rv.Kind() == reflect.Int32 || runInfo.rv.Kind() == reflect.Int64) {
  179. runInfo.rv = reflect.ValueOf(strings.Repeat(toString(lhsV), int(toInt64(runInfo.rv))))
  180. return
  181. }
  182. if lhsV.Kind() == reflect.Float64 || runInfo.rv.Kind() == reflect.Float64 {
  183. runInfo.rv = reflect.ValueOf(toFloat64(lhsV) * toFloat64(runInfo.rv))
  184. return
  185. }
  186. runInfo.rv = reflect.ValueOf(toInt64(lhsV) * toInt64(runInfo.rv))
  187. case "/":
  188. runInfo.rv = reflect.ValueOf(toFloat64(lhsV) / toFloat64(runInfo.rv))
  189. case "%":
  190. runInfo.rv = reflect.ValueOf(toInt64(lhsV) % toInt64(runInfo.rv))
  191. case ">>":
  192. runInfo.rv = reflect.ValueOf(toInt64(lhsV) >> uint64(toInt64(runInfo.rv)))
  193. case "<<":
  194. runInfo.rv = reflect.ValueOf(toInt64(lhsV) << uint64(toInt64(runInfo.rv)))
  195. case "&":
  196. runInfo.rv = reflect.ValueOf(toInt64(lhsV) & toInt64(runInfo.rv))
  197. default:
  198. runInfo.err = newStringError(operator, "unknown operator")
  199. runInfo.rv = nilValue
  200. }
  201. default:
  202. runInfo.err = newStringError(operator, "unknown operator")
  203. runInfo.rv = nilValue
  204. }
  205. }