Files
expr/optimizer/sum_array.go
T
zeekay 3cfebaa7bd fix(go.mod): declare module path github.com/hanzoai/expr
Go 1.21+ rejects 'replace X => Y vN' when Y's go.mod declares a module
path other than Y. The prior v1.17.8 kept 'module github.com/expr-lang/expr'
while being hosted+required at github.com/hanzoai/expr, so consumers on
Go 1.26 fail with:

  module declares its path as: github.com/expr-lang/expr
          but was required as: github.com/hanzoai/expr

Rehome the declared module path to github.com/hanzoai/expr and repath all
internal imports to match. Consumers keep 'require github.com/expr-lang/expr'
and 'replace github.com/expr-lang/expr => github.com/hanzoai/expr vX' — the
standard renaming-fork pattern that Go's replace path-check accepts.
2026-06-30 20:12:29 -07:00

38 lines
826 B
Go

package optimizer
import (
"fmt"
. "github.com/hanzoai/expr/ast"
)
type sumArray struct{}
func (*sumArray) Visit(node *Node) {
if sumBuiltin, ok := (*node).(*BuiltinNode); ok &&
sumBuiltin.Name == "sum" &&
len(sumBuiltin.Arguments) == 1 {
if array, ok := sumBuiltin.Arguments[0].(*ArrayNode); ok &&
len(array.Nodes) >= 2 {
patchCopyType(node, sumArrayFold(array))
}
}
}
func sumArrayFold(array *ArrayNode) *BinaryNode {
if len(array.Nodes) > 2 {
return &BinaryNode{
Operator: "+",
Left: array.Nodes[0],
Right: sumArrayFold(&ArrayNode{Nodes: array.Nodes[1:]}),
}
} else if len(array.Nodes) == 2 {
return &BinaryNode{
Operator: "+",
Left: array.Nodes[0],
Right: array.Nodes[1],
}
}
panic(fmt.Errorf("sumArrayFold: invalid array length %d", len(array.Nodes)))
}