1
0

expr.go 2.8 KB

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