ast.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // Copyright 2024 CloudWeGo Authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. package expr
  17. import (
  18. "fmt"
  19. )
  20. // Type is tyep expression type.
  21. type Type int
  22. const (
  23. // CONST indicates that the expression is a constant.
  24. CONST Type = iota
  25. // TERM indicates that the expression is a Term reference.
  26. TERM
  27. // EXPR indicates that the expression is a unary or binary expression.
  28. EXPR
  29. )
  30. var typeNames = map[Type]string{
  31. EXPR: "Expr",
  32. TERM: "Term",
  33. CONST: "Const",
  34. }
  35. // String returns the string representation of a Type.
  36. func (self Type) String() string {
  37. if v, ok := typeNames[self]; ok {
  38. return v
  39. } else {
  40. return fmt.Sprintf("expr.Type(%d)", self)
  41. }
  42. }
  43. // Operator represents an operation to perform when Type is EXPR.
  44. type Operator uint8
  45. const (
  46. // ADD performs "Add Expr.Left and Expr.Right".
  47. ADD Operator = iota
  48. // SUB performs "Subtract Expr.Left by Expr.Right".
  49. SUB
  50. // MUL performs "Multiply Expr.Left by Expr.Right".
  51. MUL
  52. // DIV performs "Divide Expr.Left by Expr.Right".
  53. DIV
  54. // MOD performs "Modulo Expr.Left by Expr.Right".
  55. MOD
  56. // AND performs "Bitwise AND Expr.Left and Expr.Right".
  57. AND
  58. // OR performs "Bitwise OR Expr.Left and Expr.Right".
  59. OR
  60. // XOR performs "Bitwise XOR Expr.Left and Expr.Right".
  61. XOR
  62. // SHL performs "Bitwise Shift Expr.Left to the Left by Expr.Right Bits".
  63. SHL
  64. // SHR performs "Bitwise Shift Expr.Left to the Right by Expr.Right Bits".
  65. SHR
  66. // POW performs "Raise Expr.Left to the power of Expr.Right"
  67. POW
  68. // NOT performs "Bitwise Invert Expr.Left".
  69. NOT
  70. // NEG performs "Negate Expr.Left".
  71. NEG
  72. )
  73. var operatorNames = map[Operator]string{
  74. ADD: "Add",
  75. SUB: "Subtract",
  76. MUL: "Multiply",
  77. DIV: "Divide",
  78. MOD: "Modulo",
  79. AND: "And",
  80. OR: "Or",
  81. XOR: "ExclusiveOr",
  82. SHL: "ShiftLeft",
  83. SHR: "ShiftRight",
  84. POW: "Power",
  85. NOT: "Invert",
  86. NEG: "Negate",
  87. }
  88. // String returns the string representation of a Type.
  89. func (self Operator) String() string {
  90. if v, ok := operatorNames[self]; ok {
  91. return v
  92. } else {
  93. return fmt.Sprintf("expr.Operator(%d)", self)
  94. }
  95. }
  96. // Expr represents an expression node.
  97. type Expr struct {
  98. Type Type
  99. Term Term
  100. Op Operator
  101. Left *Expr
  102. Right *Expr
  103. Const int64
  104. }
  105. // Ref creates an expression from a Term.
  106. func Ref(t Term) (p *Expr) {
  107. p = newExpression()
  108. p.Term = t
  109. p.Type = TERM
  110. return
  111. }
  112. // Int creates an expression from an integer.
  113. func Int(v int64) (p *Expr) {
  114. p = newExpression()
  115. p.Type = CONST
  116. p.Const = v
  117. return
  118. }
  119. func (self *Expr) clear() {
  120. if self.Term != nil {
  121. self.Term.Free()
  122. }
  123. if self.Left != nil {
  124. self.Left.Free()
  125. }
  126. if self.Right != nil {
  127. self.Right.Free()
  128. }
  129. }
  130. // Free returns the Expr into pool.
  131. // Any operation performed after Free is undefined behavior.
  132. func (self *Expr) Free() {
  133. self.clear()
  134. freeExpression(self)
  135. }
  136. // Evaluate evaluates the expression into an integer.
  137. // It also implements the Term interface.
  138. func (self *Expr) Evaluate() (int64, error) {
  139. switch self.Type {
  140. case EXPR:
  141. return self.eval()
  142. case TERM:
  143. return self.Term.Evaluate()
  144. case CONST:
  145. return self.Const, nil
  146. default:
  147. panic("invalid expression type: " + self.Type.String())
  148. }
  149. }
  150. /** Expression Combinator **/
  151. func combine(a *Expr, op Operator, b *Expr) (r *Expr) {
  152. r = newExpression()
  153. r.Op = op
  154. r.Type = EXPR
  155. r.Left = a
  156. r.Right = b
  157. return
  158. }
  159. func (self *Expr) Add(v *Expr) *Expr { return combine(self, ADD, v) }
  160. func (self *Expr) Sub(v *Expr) *Expr { return combine(self, SUB, v) }
  161. func (self *Expr) Mul(v *Expr) *Expr { return combine(self, MUL, v) }
  162. func (self *Expr) Div(v *Expr) *Expr { return combine(self, DIV, v) }
  163. func (self *Expr) Mod(v *Expr) *Expr { return combine(self, MOD, v) }
  164. func (self *Expr) And(v *Expr) *Expr { return combine(self, AND, v) }
  165. func (self *Expr) Or(v *Expr) *Expr { return combine(self, OR, v) }
  166. func (self *Expr) Xor(v *Expr) *Expr { return combine(self, XOR, v) }
  167. func (self *Expr) Shl(v *Expr) *Expr { return combine(self, SHL, v) }
  168. func (self *Expr) Shr(v *Expr) *Expr { return combine(self, SHR, v) }
  169. func (self *Expr) Pow(v *Expr) *Expr { return combine(self, POW, v) }
  170. func (self *Expr) Not() *Expr { return combine(self, NOT, nil) }
  171. func (self *Expr) Neg() *Expr { return combine(self, NEG, nil) }
  172. /** Expression Evaluator **/
  173. var binaryEvaluators = [256]func(int64, int64) (int64, error){
  174. ADD: func(a, b int64) (int64, error) { return a + b, nil },
  175. SUB: func(a, b int64) (int64, error) { return a - b, nil },
  176. MUL: func(a, b int64) (int64, error) { return a * b, nil },
  177. DIV: idiv,
  178. MOD: imod,
  179. AND: func(a, b int64) (int64, error) { return a & b, nil },
  180. OR: func(a, b int64) (int64, error) { return a | b, nil },
  181. XOR: func(a, b int64) (int64, error) { return a ^ b, nil },
  182. SHL: func(a, b int64) (int64, error) { return a << b, nil },
  183. SHR: func(a, b int64) (int64, error) { return a >> b, nil },
  184. POW: ipow,
  185. }
  186. func (self *Expr) eval() (int64, error) {
  187. var lhs int64
  188. var rhs int64
  189. var err error
  190. var vfn func(int64, int64) (int64, error)
  191. /* evaluate LHS */
  192. if lhs, err = self.Left.Evaluate(); err != nil {
  193. return 0, err
  194. }
  195. /* check for unary operators */
  196. switch self.Op {
  197. case NOT:
  198. return self.unaryNot(lhs)
  199. case NEG:
  200. return self.unaryNeg(lhs)
  201. }
  202. /* check for operators */
  203. if vfn = binaryEvaluators[self.Op]; vfn == nil {
  204. panic("invalid operator: " + self.Op.String())
  205. }
  206. /* must be a binary expression */
  207. if self.Right == nil {
  208. panic("operator " + self.Op.String() + " is a binary operator")
  209. }
  210. /* evaluate RHS, and call the operator */
  211. if rhs, err = self.Right.Evaluate(); err != nil {
  212. return 0, err
  213. } else {
  214. return vfn(lhs, rhs)
  215. }
  216. }
  217. func (self *Expr) unaryNot(v int64) (int64, error) {
  218. if self.Right == nil {
  219. return ^v, nil
  220. } else {
  221. panic("operator Invert is an unary operator")
  222. }
  223. }
  224. func (self *Expr) unaryNeg(v int64) (int64, error) {
  225. if self.Right == nil {
  226. return -v, nil
  227. } else {
  228. panic("operator Negate is an unary operator")
  229. }
  230. }