Files
expr/optimizer/optimizer.go
T
2020-03-10 08:36:12 +03:00

41 lines
792 B
Go

package optimizer
import (
. "github.com/antonmedv/expr/ast"
"github.com/antonmedv/expr/internal/conf"
)
func Optimize(node *Node, config *conf.Config) error {
Walk(node, &inArray{})
for limit := 1000; limit >= 0; limit-- {
fold := &fold{}
Walk(node, fold)
if !fold.applied {
break
}
}
if config != nil && len(config.ConstExprFns) > 0 {
for limit := 100; limit >= 0; limit-- {
constExpr := &constExpr{
fns: config.ConstExprFns,
}
Walk(node, constExpr)
if constExpr.err != nil {
return constExpr.err
}
if !constExpr.applied {
break
}
}
}
Walk(node, &inRange{})
Walk(node, &constRange{})
return nil
}
func patch(node *Node, newNode Node) {
newNode.SetType((*node).Type())
newNode.SetLocation((*node).Location())
*node = newNode
}