expr.go 9.1 KB

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