expr.go 8.7 KB

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