expr.go 8.1 KB

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