expr.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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(nil)
  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(nil)
  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. fmt.Println(res, l, r)
  152. case operation.LESS_EQUAL:
  153. res = scope.Ops.Leq(l, r)
  154. case operation.LEN:
  155. res = scope.Ops.Len(op.Left().Object(), l, r)
  156. case operation.NOT_EQUAL:
  157. res = scope.Ops.Neq(l, r)
  158. case operation.GREATER:
  159. res = scope.Ops.Gtr(l, r)
  160. case operation.MAX:
  161. res = scope.Ops.Max(l, r)
  162. case operation.MIN:
  163. res = scope.Ops.Min(l, r)
  164. case operation.DIV:
  165. res = scope.Ops.Div(l, r)
  166. case operation.MOD:
  167. res = scope.Ops.Mod(l, r)
  168. case operation.ALIEN_MSK:
  169. res = scope.Ops.Msk(l, r)
  170. case operation.TIMES:
  171. res = scope.Ops.Mult(l, r)
  172. case operation.SLASH:
  173. res = scope.Ops.Divide(l, r)
  174. case operation.IN:
  175. res = scope.Ops.In(l, r)
  176. case operation.ASH:
  177. res = scope.Ops.Ash(l, r)
  178. case operation.AND:
  179. res = scope.Ops.And(l, r)
  180. case operation.OR:
  181. res = scope.Ops.Or(l, r)
  182. case operation.GREAT_EQUAL:
  183. res = scope.Ops.Geq(l, r)
  184. default:
  185. halt.As(100, op.Operation())
  186. }
  187. assert.For(res != nil, 40)
  188. rt2.ValueOf(in.Parent)[op.Adr()] = res
  189. rt2.RegOf(in.Parent)[in.Key] = op.Adr()
  190. return End()
  191. }
  192. next := func(IN) OUT {
  193. return GetExpression(in, right, op.Right(), do)
  194. }
  195. short := func(IN) OUT {
  196. id := KeyOf(in, left)
  197. lv := rt2.ValueOf(in.Frame)[id]
  198. switch op.Operation() {
  199. case operation.AND:
  200. val := scope.GoTypeFrom(lv).(bool)
  201. if val {
  202. return Now(next)
  203. } else {
  204. rt2.ValueOf(in.Parent)[op.Adr()] = scope.TypeFromGo(false)
  205. rt2.RegOf(in.Parent)[in.Key] = op.Adr()
  206. return End()
  207. }
  208. case operation.OR:
  209. val := scope.GoTypeFrom(lv).(bool)
  210. if !val {
  211. return Now(next)
  212. } else {
  213. rt2.ValueOf(in.Parent)[op.Adr()] = scope.TypeFromGo(true)
  214. rt2.RegOf(in.Parent)[in.Key] = op.Adr()
  215. return End()
  216. }
  217. default:
  218. return Now(next)
  219. }
  220. }
  221. return GetExpression(in, left, op.Left(), short)
  222. }
  223. func getMop(in IN) OUT {
  224. const left = "mop:left"
  225. op := in.IR.(node.MonadicNode)
  226. do := func(in IN) OUT {
  227. lv := rt2.ValueOf(in.Frame)[KeyOf(in, left)]
  228. var res scope.Value
  229. switch op.Operation() {
  230. case operation.ALIEN_CONV:
  231. if op.Type() != object.NOTYPE {
  232. res = scope.Ops.Conv(lv, op.Type())
  233. } else {
  234. res = scope.Ops.Conv(lv, op.Type(), op.Complex())
  235. }
  236. case operation.NOT:
  237. res = scope.Ops.Not(lv)
  238. case operation.IS:
  239. res = scope.Ops.Is(lv, op.Object().Complex())
  240. case operation.ABS:
  241. res = scope.Ops.Abs(lv)
  242. case operation.ODD:
  243. res = scope.Ops.Odd(lv)
  244. case operation.CAP:
  245. res = scope.Ops.Cap(lv)
  246. case operation.BITS:
  247. res = scope.Ops.Bits(lv)
  248. case operation.MINUS:
  249. res = scope.Ops.Minus(lv)
  250. default:
  251. halt.As(100, "unknown op", op.Operation())
  252. }
  253. assert.For(res != nil, 60)
  254. rt2.ValueOf(in.Parent)[op.Adr()] = res
  255. rt2.RegOf(in.Parent)[in.Key] = op.Adr()
  256. return End()
  257. }
  258. return GetExpression(in, left, op.Left(), do)
  259. }
  260. func getGuard(in IN) OUT {
  261. const left = "guard:left"
  262. g := in.IR.(node.GuardNode)
  263. return GetDesignator(in, left, g.Left(), func(IN) OUT {
  264. v := rt2.ValueOf(in.Frame)[KeyOf(in, left)]
  265. assert.For(v != nil, 20)
  266. if scope.GoTypeFrom(scope.Ops.Is(v, g.Type())).(bool) {
  267. rt2.ValueOf(in.Parent)[g.Adr()] = v
  268. rt2.RegOf(in.Parent)[in.Key] = g.Adr()
  269. return End()
  270. } else {
  271. return makeTrap(in.Frame, 0)
  272. }
  273. })
  274. }
  275. func bit_range(_f scope.Value, _t scope.Value) scope.Value {
  276. f := scope.GoTypeFrom(_f).(int32)
  277. t := scope.GoTypeFrom(_t).(int32)
  278. ret := big.NewInt(0)
  279. for i := f; i <= t; i++ {
  280. ret = ret.SetBit(ret, int(i), 1)
  281. }
  282. fmt.Println("bits", ret)
  283. return scope.TypeFromGo(ret)
  284. }
  285. func getRange(in IN) OUT {
  286. const (
  287. left = "range:left"
  288. right = "range:right"
  289. )
  290. r := in.IR.(node.RangeNode)
  291. return GetExpression(in, left, r.Left(), func(IN) OUT {
  292. return GetExpression(in, right, r.Right(), func(in IN) OUT {
  293. lv := rt2.ValueOf(in.Frame)[KeyOf(in, left)]
  294. rv := rt2.ValueOf(in.Frame)[KeyOf(in, right)]
  295. res := bit_range(lv, rv)
  296. rt2.ValueOf(in.Parent)[r.Adr()] = res
  297. rt2.RegOf(in.Parent)[in.Key] = r.Adr()
  298. return End()
  299. })
  300. })
  301. }