expr.go 7.7 KB

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