File: const_range.go

package info (click to toggle)
golang-github-antonmedv-expr 1.8.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 4,524 kB
  • sloc: makefile: 6
file content (33 lines) | stat: -rw-r--r-- 703 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package optimizer

import (
	. "github.com/antonmedv/expr/ast"
)

type constRange struct{}

func (*constRange) Enter(*Node) {}
func (*constRange) Exit(node *Node) {
	switch n := (*node).(type) {
	case *BinaryNode:
		if n.Operator == ".." {
			if min, ok := n.Left.(*IntegerNode); ok {
				if max, ok := n.Right.(*IntegerNode); ok {
					size := max.Value - min.Value + 1
					// In this case array is too big. Skip generation,
					// and wait for memory budget detection on runtime.
					if size > 1e6 {
						return
					}
					value := make([]int, size)
					for i := range value {
						value[i] = min.Value + i
					}
					Patch(node, &ConstantNode{
						Value: value,
					})
				}
			}
		}
	}
}