expr.go 9.9 KB

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