github.com/expr-lang/expr@v1.16.9/optimizer/in_range.go (about)

     1  package optimizer
     2  
     3  import (
     4  	"reflect"
     5  
     6  	. "github.com/expr-lang/expr/ast"
     7  )
     8  
     9  type inRange struct{}
    10  
    11  func (*inRange) Visit(node *Node) {
    12  	switch n := (*node).(type) {
    13  	case *BinaryNode:
    14  		if n.Operator == "in" {
    15  			t := n.Left.Type()
    16  			if t == nil {
    17  				return
    18  			}
    19  			if t.Kind() != reflect.Int {
    20  				return
    21  			}
    22  			if rangeOp, ok := n.Right.(*BinaryNode); ok && rangeOp.Operator == ".." {
    23  				if from, ok := rangeOp.Left.(*IntegerNode); ok {
    24  					if to, ok := rangeOp.Right.(*IntegerNode); ok {
    25  						Patch(node, &BinaryNode{
    26  							Operator: "and",
    27  							Left: &BinaryNode{
    28  								Operator: ">=",
    29  								Left:     n.Left,
    30  								Right:    from,
    31  							},
    32  							Right: &BinaryNode{
    33  								Operator: "<=",
    34  								Left:     n.Left,
    35  								Right:    to,
    36  							},
    37  						})
    38  					}
    39  				}
    40  			}
    41  		}
    42  	}
    43  }