expr.go 11 KB

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