expr.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package eval
  2. import (
  3. "fw/cp/constant/operation"
  4. "fw/cp/node"
  5. "fw/cp/object"
  6. "fw/rt2"
  7. "fw/rt2/scope"
  8. "ypk/assert"
  9. "ypk/halt"
  10. )
  11. func getConst(in IN) OUT {
  12. c := in.IR.(node.ConstantNode)
  13. sc := rt2.ThisScope(in.Frame)
  14. fn := sc.Provide(c)
  15. assert.For(fn != nil, 40)
  16. rt2.ValueOf(in.Parent)[c.Adr()] = fn(nil)
  17. rt2.RegOf(in.Parent)[in.Key] = c.Adr()
  18. return End()
  19. }
  20. func getVar(in IN) OUT {
  21. v := in.IR.(node.VariableNode)
  22. rt2.ScopeFor(in.Frame, v.Object().Adr(), func(val scope.Value) {
  23. rt2.ValueOf(in.Parent)[v.Adr()] = val
  24. rt2.RegOf(in.Parent)[in.Key] = v.Adr()
  25. })
  26. return End()
  27. }
  28. func getVarPar(in IN) OUT {
  29. v := in.IR.(node.ParameterNode)
  30. rt2.ScopeFor(in.Frame, v.Object().Adr(), func(val scope.Value) {
  31. rt2.ValueOf(in.Parent)[v.Adr()] = val
  32. rt2.RegOf(in.Parent)[in.Key] = v.Adr()
  33. })
  34. return End()
  35. }
  36. func getDop(in IN) OUT {
  37. const (
  38. left = "dop:left"
  39. right = "dop:right"
  40. )
  41. op := in.IR.(node.DyadicNode)
  42. do := func(in IN) OUT {
  43. var (
  44. res scope.Value
  45. )
  46. l := rt2.ValueOf(in.Frame)[KeyOf(in, left)]
  47. r := rt2.ValueOf(in.Frame)[KeyOf(in, right)]
  48. switch op.Operation() {
  49. case operation.PLUS:
  50. res = scope.Ops.Sum(l, r)
  51. case operation.MINUS:
  52. res = scope.Ops.Sub(l, r)
  53. case operation.EQUAL:
  54. res = scope.Ops.Eq(l, r)
  55. case operation.LESSER:
  56. res = scope.Ops.Lss(l, r)
  57. case operation.LESS_EQUAL:
  58. res = scope.Ops.Leq(l, r)
  59. case operation.LEN:
  60. res = scope.Ops.Len(op.Left().Object(), l, r)
  61. case operation.NOT_EQUAL:
  62. res = scope.Ops.Neq(l, r)
  63. case operation.GREATER:
  64. res = scope.Ops.Gtr(l, r)
  65. case operation.MAX:
  66. res = scope.Ops.Max(l, r)
  67. case operation.MIN:
  68. res = scope.Ops.Min(l, r)
  69. case operation.DIV:
  70. res = scope.Ops.Div(l, r)
  71. case operation.MOD:
  72. res = scope.Ops.Mod(l, r)
  73. case operation.ALIEN_MSK:
  74. res = scope.Ops.Msk(l, r)
  75. case operation.TIMES:
  76. res = scope.Ops.Mult(l, r)
  77. case operation.SLASH:
  78. res = scope.Ops.Divide(l, r)
  79. case operation.IN:
  80. res = scope.Ops.In(l, r)
  81. case operation.ASH:
  82. res = scope.Ops.Ash(l, r)
  83. case operation.AND:
  84. res = scope.Ops.And(l, r)
  85. case operation.OR:
  86. res = scope.Ops.Or(l, r)
  87. case operation.GREAT_EQUAL:
  88. res = scope.Ops.Geq(l, r)
  89. default:
  90. halt.As(100, op.Operation())
  91. }
  92. assert.For(res != nil, 40)
  93. rt2.ValueOf(in.Parent)[op.Adr()] = res
  94. rt2.RegOf(in.Parent)[in.Key] = op.Adr()
  95. return End()
  96. }
  97. next := func(IN) OUT {
  98. return GetExpression(in, right, op.Right(), do)
  99. }
  100. short := func(IN) OUT {
  101. id := KeyOf(in, left)
  102. lv := rt2.ValueOf(in.Frame)[id]
  103. switch op.Operation() {
  104. case operation.AND:
  105. val := scope.GoTypeFrom(lv).(bool)
  106. if val {
  107. return Now(next)
  108. } else {
  109. rt2.ValueOf(in.Parent)[op.Adr()] = scope.TypeFromGo(false)
  110. rt2.RegOf(in.Parent)[in.Key] = op.Adr()
  111. return End()
  112. }
  113. case operation.OR:
  114. val := scope.GoTypeFrom(lv).(bool)
  115. if !val {
  116. return Now(next)
  117. } else {
  118. rt2.ValueOf(in.Parent)[op.Adr()] = scope.TypeFromGo(true)
  119. rt2.RegOf(in.Parent)[in.Key] = op.Adr()
  120. return End()
  121. }
  122. default:
  123. return Now(next)
  124. }
  125. }
  126. return GetExpression(in, left, op.Left(), short)
  127. }
  128. func getMop(in IN) OUT {
  129. const left = "mop:left"
  130. op := in.IR.(node.MonadicNode)
  131. do := func(in IN) OUT {
  132. lv := rt2.ValueOf(in.Frame)[KeyOf(in, left)]
  133. var res scope.Value
  134. switch op.Operation() {
  135. case operation.ALIEN_CONV:
  136. if op.Type() != object.NOTYPE {
  137. res = scope.Ops.Conv(lv, op.Type())
  138. } else {
  139. res = scope.Ops.Conv(lv, op.Type(), op.Complex())
  140. }
  141. default:
  142. halt.As(100, "unknown op", op.Operation())
  143. }
  144. assert.For(res != nil, 60)
  145. rt2.ValueOf(in.Parent)[op.Adr()] = res
  146. rt2.RegOf(in.Parent)[in.Key] = op.Adr()
  147. return End()
  148. }
  149. return GetExpression(in, left, op.Left(), do)
  150. }