-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpromote.go
152 lines (140 loc) · 2.93 KB
/
promote.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package fgbase
import (
"reflect"
)
func biggerType(a, b interface{}) bool {
switch a.(type) {
case bool:
{
}
case int8, uint8:
{
switch b.(type) {
case bool:
{
return true
}
}
}
case int16, uint16:
{
switch b.(type) {
case bool, int8, uint8:
{
return true
}
}
}
case int32, uint32:
{
switch b.(type) {
case bool, int8, uint8, int16, uint16:
{
return true
}
}
}
case int, uint:
{
switch b.(type) {
case bool, int8, uint8, int16, uint16, int32, uint32:
{
return true
}
}
}
case int64, uint64:
{
switch b.(type) {
case bool, int8, uint8, int16, uint16, int32, uint32, int, uint:
{
return true
}
}
}
case float32:
{
switch b.(type) {
case bool, int8, uint8, int16, uint16, int32, uint32, int64, uint64, int, uint:
{
return true
}
}
}
case float64, complex64:
{
switch b.(type) {
case bool, int8, uint8, int16, uint16, int32, uint32, int64, uint64, int, uint, float32:
{
return true
}
}
}
case complex128:
{
switch b.(type) {
case bool, int8, uint8, int16, uint16, int32, uint32, int64, uint64, int, uint, float32, float64, complex64:
{
return true
}
}
}
}
return false
}
// Promote pair of numeric empty interfaces (interface{}) as necessary.
func Promote(n *Node, a, b interface{}) (aBig, bBig interface{}, same bool) {
var debug = false && n != nil
ta := reflect.TypeOf(a)
tb := reflect.TypeOf(b)
if debug {
n.Tracef("promote: ta %v of Kind %s, tb %v of Kind %s\n", ta, ta.Kind().String(), tb, tb.Kind().String())
}
if ta == tb {
return a, b, true
}
if ta == nil || tb == nil {
return a, b, false
}
if ta.Kind() == tb.Kind() {
if ta.Kind().String() == ta.String() {
if debug && TraceLevel >= VVV && n != nil {
n.Tracef("case -2: promoting %v to %v\n", tb, ta)
}
return a, reflect.ValueOf(b).Convert(ta).Interface(), true
}
if tb.Kind().String() == tb.String() {
if debug && TraceLevel >= VVV && n != nil {
n.Tracef("case -1: promoting %v to %v\n", ta, tb)
}
return reflect.ValueOf(a).Convert(tb).Interface(), b, true
}
}
aBigger := biggerType(a, b)
if aBigger {
if tb.ConvertibleTo(ta) {
if debug && TraceLevel >= VVV && n != nil {
n.Tracef("case 0: promoting %v to %v\n", tb, ta)
}
return a, reflect.ValueOf(b).Convert(ta).Interface(), true
}
}
if ta.ConvertibleTo(tb) {
if debug && TraceLevel >= VVV && n != nil {
n.Tracef("case 1: promoting %v to %v\n", ta, tb)
}
return reflect.ValueOf(a).Convert(tb).Interface(), b, true
}
if !aBigger {
if tb.ConvertibleTo(ta) {
if debug && TraceLevel >= VVV && n != nil {
n.Tracef("case 2: promoting %v to %v\n", tb, ta)
}
return a, reflect.ValueOf(b).Convert(ta).Interface(), true
}
}
if debug && TraceLevel >= VVV && n != nil {
n.Tracef("case 3: no promotion between %v to %v\n", tb, ta)
}
return a, b, false
}