expr.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package eval
  2. import (
  3. "fmt"
  4. "fw/cp/constant/operation"
  5. "fw/cp/node"
  6. "fw/cp/object"
  7. "fw/rt2"
  8. "fw/rt2/scope"
  9. "reflect"
  10. "ypk/assert"
  11. "ypk/halt"
  12. )
  13. func getConst(in IN) OUT {
  14. c := in.IR.(node.ConstantNode)
  15. sc := rt2.ThisScope(in.Frame)
  16. fn := sc.Provide(c)
  17. assert.For(fn != nil, 40)
  18. rt2.ValueOf(in.Parent)[c.Adr()] = fn(nil)
  19. rt2.RegOf(in.Parent)[in.Key] = c.Adr()
  20. return End()
  21. }
  22. func getVar(in IN) OUT {
  23. v := in.IR.(node.VariableNode)
  24. rt2.ScopeFor(in.Frame, v.Object().Adr(), func(val scope.Value) {
  25. rt2.ValueOf(in.Parent)[v.Adr()] = val
  26. rt2.RegOf(in.Parent)[in.Key] = v.Adr()
  27. })
  28. return End()
  29. }
  30. func getVarPar(in IN) OUT {
  31. v := in.IR.(node.ParameterNode)
  32. rt2.ScopeFor(in.Frame, v.Object().Adr(), func(val scope.Value) {
  33. rt2.ValueOf(in.Parent)[v.Adr()] = val
  34. rt2.RegOf(in.Parent)[in.Key] = v.Adr()
  35. })
  36. return End()
  37. }
  38. func getField(in IN) OUT {
  39. const left = "field:left"
  40. f := in.IR.(node.FieldNode)
  41. return GetDesignator(in, left, f.Left(), func(in IN) OUT {
  42. _v := rt2.ValueOf(in.Frame)[KeyOf(in, left)]
  43. switch v := _v.(type) {
  44. case scope.Record:
  45. fld := v.Get(f.Object().Adr())
  46. rt2.ValueOf(in.Parent)[f.Adr()] = fld
  47. rt2.RegOf(in.Parent)[in.Key] = f.Adr()
  48. return End()
  49. default:
  50. halt.As(100, reflect.TypeOf(v))
  51. }
  52. panic(0)
  53. })
  54. }
  55. func getIndex(in IN) OUT {
  56. const (
  57. left = "index:left"
  58. right = "index:right"
  59. )
  60. i := in.IR.(node.IndexNode)
  61. return GetExpression(in, right, i.Right(), func(IN) OUT {
  62. idx := rt2.ValueOf(in.Frame)[KeyOf(in, right)]
  63. assert.For(idx != nil, 40)
  64. return GetDesignator(in, left, i.Left(), func(IN) OUT {
  65. arr := rt2.ValueOf(in.Frame)[KeyOf(in, left)]
  66. assert.For(arr != nil, 41)
  67. switch a := arr.(type) {
  68. case scope.Array:
  69. rt2.ValueOf(in.Parent)[i.Adr()] = a.Get(idx)
  70. rt2.RegOf(in.Parent)[in.Key] = i.Adr()
  71. return End()
  72. default:
  73. halt.As(100, reflect.TypeOf(a))
  74. }
  75. panic(890)
  76. })
  77. })
  78. }
  79. func getProc(in IN) OUT {
  80. p := in.IR.(node.ProcedureNode)
  81. sc := rt2.ThisScope(in.Frame)
  82. fn := sc.Provide(p.Object())
  83. assert.For(fn != nil, 40)
  84. rt2.ValueOf(in.Parent)[p.Adr()] = fn(nil)
  85. rt2.RegOf(in.Parent)[in.Key] = p.Adr()
  86. return End()
  87. }
  88. func getDeref(in IN) OUT {
  89. const left = "design:left"
  90. d := in.IR.(node.DerefNode)
  91. return GetDesignator(in, left, d.Left(), func(in IN) OUT {
  92. _v := rt2.ValueOf(in.Frame)[KeyOf(in, left)]
  93. switch v := _v.(type) {
  94. case scope.Array:
  95. assert.For(!d.Ptr(), 40)
  96. t, c := scope.Ops.TypeOf(v)
  97. switch cc := c.(type) {
  98. case object.ArrayType:
  99. assert.For(cc.Base() == object.CHAR || cc.Base() == object.SHORTCHAR, 41)
  100. rt2.ValueOf(in.Parent)[d.Adr()] = scope.TypeFromGo(scope.GoTypeFrom(v))
  101. rt2.RegOf(in.Parent)[in.Key] = d.Adr()
  102. return End()
  103. case object.DynArrayType:
  104. assert.For(cc.Base() == object.CHAR || cc.Base() == object.SHORTCHAR, 41)
  105. rt2.ValueOf(in.Parent)[d.Adr()] = scope.TypeFromGo(scope.GoTypeFrom(v))
  106. rt2.RegOf(in.Parent)[in.Key] = d.Adr()
  107. return End()
  108. default:
  109. halt.As(100, t, reflect.TypeOf(cc))
  110. }
  111. default:
  112. halt.As(100, reflect.TypeOf(v))
  113. }
  114. panic(0)
  115. })
  116. }
  117. func getDop(in IN) OUT {
  118. const (
  119. left = "dop:left"
  120. right = "dop:right"
  121. )
  122. op := in.IR.(node.DyadicNode)
  123. do := func(in IN) OUT {
  124. var (
  125. res scope.Value
  126. )
  127. l := rt2.ValueOf(in.Frame)[KeyOf(in, left)]
  128. r := rt2.ValueOf(in.Frame)[KeyOf(in, right)]
  129. switch op.Operation() {
  130. case operation.PLUS:
  131. res = scope.Ops.Sum(l, r)
  132. case operation.MINUS:
  133. res = scope.Ops.Sub(l, r)
  134. case operation.EQUAL:
  135. res = scope.Ops.Eq(l, r)
  136. case operation.LESSER:
  137. res = scope.Ops.Lss(l, r)
  138. fmt.Println(res, l, r)
  139. case operation.LESS_EQUAL:
  140. res = scope.Ops.Leq(l, r)
  141. case operation.LEN:
  142. res = scope.Ops.Len(op.Left().Object(), l, r)
  143. case operation.NOT_EQUAL:
  144. res = scope.Ops.Neq(l, r)
  145. case operation.GREATER:
  146. res = scope.Ops.Gtr(l, r)
  147. case operation.MAX:
  148. res = scope.Ops.Max(l, r)
  149. case operation.MIN:
  150. res = scope.Ops.Min(l, r)
  151. case operation.DIV:
  152. res = scope.Ops.Div(l, r)
  153. case operation.MOD:
  154. res = scope.Ops.Mod(l, r)
  155. case operation.ALIEN_MSK:
  156. res = scope.Ops.Msk(l, r)
  157. case operation.TIMES:
  158. res = scope.Ops.Mult(l, r)
  159. case operation.SLASH:
  160. res = scope.Ops.Divide(l, r)
  161. case operation.IN:
  162. res = scope.Ops.In(l, r)
  163. case operation.ASH:
  164. res = scope.Ops.Ash(l, r)
  165. case operation.AND:
  166. res = scope.Ops.And(l, r)
  167. case operation.OR:
  168. res = scope.Ops.Or(l, r)
  169. case operation.GREAT_EQUAL:
  170. res = scope.Ops.Geq(l, r)
  171. default:
  172. halt.As(100, op.Operation())
  173. }
  174. assert.For(res != nil, 40)
  175. rt2.ValueOf(in.Parent)[op.Adr()] = res
  176. rt2.RegOf(in.Parent)[in.Key] = op.Adr()
  177. return End()
  178. }
  179. next := func(IN) OUT {
  180. return GetExpression(in, right, op.Right(), do)
  181. }
  182. short := func(IN) OUT {
  183. id := KeyOf(in, left)
  184. lv := rt2.ValueOf(in.Frame)[id]
  185. switch op.Operation() {
  186. case operation.AND:
  187. val := scope.GoTypeFrom(lv).(bool)
  188. if val {
  189. return Now(next)
  190. } else {
  191. rt2.ValueOf(in.Parent)[op.Adr()] = scope.TypeFromGo(false)
  192. rt2.RegOf(in.Parent)[in.Key] = op.Adr()
  193. return End()
  194. }
  195. case operation.OR:
  196. val := scope.GoTypeFrom(lv).(bool)
  197. if !val {
  198. return Now(next)
  199. } else {
  200. rt2.ValueOf(in.Parent)[op.Adr()] = scope.TypeFromGo(true)
  201. rt2.RegOf(in.Parent)[in.Key] = op.Adr()
  202. return End()
  203. }
  204. default:
  205. return Now(next)
  206. }
  207. }
  208. return GetExpression(in, left, op.Left(), short)
  209. }
  210. func getMop(in IN) OUT {
  211. const left = "mop:left"
  212. op := in.IR.(node.MonadicNode)
  213. do := func(in IN) OUT {
  214. lv := rt2.ValueOf(in.Frame)[KeyOf(in, left)]
  215. var res scope.Value
  216. switch op.Operation() {
  217. case operation.ALIEN_CONV:
  218. if op.Type() != object.NOTYPE {
  219. res = scope.Ops.Conv(lv, op.Type())
  220. } else {
  221. res = scope.Ops.Conv(lv, op.Type(), op.Complex())
  222. }
  223. case operation.NOT:
  224. res = scope.Ops.Not(lv)
  225. case operation.IS:
  226. /*sc := rt2.ScopeFor(f, n.Left().Object().Adr())
  227. rt2.ValueOf(f.Parent())[n.Adr()] = scope.Ops.Is(sc.Select(n.Left().Object().Adr()), n.Object().Complex())
  228. return frame.End()*/
  229. panic(0)
  230. case operation.ABS:
  231. res = scope.Ops.Abs(lv)
  232. case operation.ODD:
  233. res = scope.Ops.Odd(lv)
  234. case operation.CAP:
  235. res = scope.Ops.Cap(lv)
  236. case operation.BITS:
  237. res = scope.Ops.Bits(lv)
  238. case operation.MINUS:
  239. res = scope.Ops.Minus(lv)
  240. default:
  241. halt.As(100, "unknown op", op.Operation())
  242. }
  243. assert.For(res != nil, 60)
  244. rt2.ValueOf(in.Parent)[op.Adr()] = res
  245. rt2.RegOf(in.Parent)[in.Key] = op.Adr()
  246. return End()
  247. }
  248. return GetExpression(in, left, op.Left(), do)
  249. }