-
Notifications
You must be signed in to change notification settings - Fork 3
/
binary.go
53 lines (43 loc) · 1.02 KB
/
binary.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package velty
import (
"github.com/viant/velty/ast/expr"
eexpr "github.com/viant/velty/est/expr"
"github.com/viant/velty/est/op"
types "github.com/viant/xunsafe/converter"
"reflect"
)
func (p *Planner) compileBinary(actual *expr.Binary) (*op.Expression, error) {
x, err := p.compileExpr(actual.X)
if err != nil {
return nil, err
}
y, err := p.compileExpr(actual.Y)
if err != nil {
return nil, err
}
unify, err := types.NormalizeAndUnify(x.Type, y.Type)
if err != nil {
return nil, err
}
x.Unify = unify.X
y.Unify = unify.Y
resultType := notNilType(types.NormalizeType(actual.Type()), unify.RType)
acc := p.accumulator(resultType)
resultExpr := &op.Expression{Selector: acc, Type: acc.Type}
computeNew, err := eexpr.Binary(actual.Token, x, y, resultExpr)
if err != nil {
return nil, err
}
return &op.Expression{
Type: resultType,
New: computeNew,
}, nil
}
func notNilType(types ...reflect.Type) reflect.Type {
for _, rType := range types {
if rType != nil {
return rType
}
}
return nil
}