expr.go 971 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package expr
  2. import "reflect"
  3. // A Var identifies a variable, e.g., x.
  4. type Var string
  5. // A literal is a numeric constant, e.g., 3.141.
  6. type literal struct {
  7. value interface{}
  8. }
  9. // An Expr is an arithmetic expression.
  10. type Expr interface {
  11. // Eval returns the value of this Expr in the environment env.
  12. Eval(env Env) reflect.Value
  13. // Check reports errors in this Expr and adds its Vars to the set.
  14. Check(vars map[Var]interface{}) error
  15. }
  16. // A unary represents a unary operator expression, e.g., -x.
  17. type unary struct {
  18. op string // one of '+', '-', '!', '~'
  19. x Expr
  20. }
  21. // A binary represents a binary operator expression, e.g., x+y.
  22. type binary struct {
  23. op string
  24. x, y Expr
  25. }
  26. // A call represents a function call expression, e.g., sin(x).
  27. type call struct {
  28. fn string
  29. args []Expr
  30. }
  31. var exprFuncParams = map[string]int{
  32. "pow": 2,
  33. "sin": 1,
  34. "sqrt": 1,
  35. "rand": 0,
  36. "log": 1,
  37. "to_upper": 1,
  38. "to_lower": 1,
  39. "crc32": 1,
  40. }