forked from 0xfaded/eval
/
consttype.go
242 lines (222 loc) · 7.88 KB
/
consttype.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package eval
import "reflect"
// Type ConstType can annotate information needed for evaluating const
// expressions. It should not be used with the reflect package.
type ConstType interface {
reflect.Type
IsIntegral() bool
IsReal() bool
}
type ConstIntType struct { reflect.Type }
type ConstRuneType struct { reflect.Type }
type ConstFloatType struct { reflect.Type }
type ConstComplexType struct { reflect.Type }
type ConstStringType struct { reflect.Type }
type ConstNilType struct { reflect.Type }
type ConstBoolType struct { reflect.Type }
var (
ConstInt = ConstIntType { reflect.TypeOf(0) }
ConstRune = ConstRuneType { reflect.TypeOf('\000') }
ConstFloat = ConstFloatType { reflect.TypeOf(0.0) }
ConstComplex = ConstComplexType { reflect.TypeOf(0i) }
ConstString = ConstStringType { reflect.TypeOf("") }
ConstNil = ConstNilType { nil }
ConstBool = ConstBoolType { reflect.TypeOf(false) }
)
func (ConstIntType) String() string { return "int" }
func (ConstRuneType) String() string { return "rune" }
func (ConstFloatType) String() string { return "float64" }
func (ConstComplexType) String() string { return "complex128" }
func (ConstStringType) String() string { return "string" }
func (ConstNilType) String() string { return "<T>" }
func (ConstBoolType) String() string { return "bool" }
func (ConstIntType) IsIntegral() bool { return true }
func (ConstRuneType) IsIntegral() bool { return true }
func (ConstFloatType) IsIntegral() bool { return false }
func (ConstComplexType) IsIntegral() bool { return false }
func (ConstStringType) IsIntegral() bool { return false }
func (ConstNilType) IsIntegral() bool { return false }
func (ConstBoolType) IsIntegral() bool { return false }
func (ConstIntType) IsReal() bool { return true }
func (ConstRuneType) IsReal() bool { return true }
func (ConstFloatType) IsReal() bool { return true }
func (ConstComplexType) IsReal() bool { return false }
func (ConstStringType) IsReal() bool { return false }
func (ConstNilType) IsReal() bool { return false }
func (ConstBoolType) IsReal() bool { return false }
// promoteConsts returns the ConstType of a binary, a non-boolean,
// expression involving const types of x and y. Errors match those
// produced by gc and are as follows:
//
// If one type is a numeric type, and the other is not:
// ErrBadConstConversion other -> numeric
//
// If one type is a string type and the other is a bool type:
// ErrBadConstConversion bool -> int
// ErrBadConstConversion string -> int
//
// If one value is nil and the other a string value:
// ErrBadConstConversion nil -> int
// ErrBadConstConversion string -> int
//
// If one value is nil and the other a bool value:
// ErrBadConstConversion nil -> bool
func promoteConsts(ctx *Ctx, x, y ConstType, xexpr, yexpr Expr, xval, yval reflect.Value) (ConstType, []error) {
switch x.(type) {
case ConstIntType, ConstRuneType, ConstFloatType, ConstComplexType:
switch y.(type) {
case ConstIntType, ConstRuneType, ConstFloatType, ConstComplexType:
return promoteConstNumbers(x, y), nil
}
return nil, []error{ErrBadConstConversion{at(ctx, yexpr), y, x, yval}}
case ConstStringType:
switch y.(type) {
case ConstStringType:
return x, nil
case ConstIntType, ConstRuneType, ConstFloatType, ConstComplexType:
return nil, []error{ErrBadConstConversion{at(ctx, xexpr), x, y, xval}}
default:
return nil, []error{
ErrBadConstConversion{at(ctx, xexpr), x, ConstInt, xval},
ErrBadConstConversion{at(ctx, yexpr), y, ConstInt, yval},
}
}
case ConstNilType:
switch y.(type) {
case ConstNilType:
return x, nil
case ConstIntType, ConstRuneType, ConstFloatType, ConstComplexType:
return nil, []error{ErrBadConstConversion{at(ctx, xexpr), x, y, xval}}
default:
return nil, []error{
ErrBadConstConversion{at(ctx, xexpr), x, ConstInt, xval},
ErrBadConstConversion{at(ctx, yexpr), y, ConstInt, yval},
}
}
case ConstBoolType:
switch y.(type) {
case ConstBoolType:
return x, nil
case ConstIntType, ConstRuneType, ConstFloatType, ConstComplexType, ConstStringType, ConstNilType:
return nil, []error{ErrBadConstConversion{at(ctx, yexpr), y, x, yval}}
}
}
panic("go-interactive: impossible")
}
// promoteConstNumbers can't fail, but panics if x or y are not
// Const(Int|Rune|Float|Complex)Type
func promoteConstNumbers(x, y ConstType) ConstType {
switch x.(type) {
case ConstIntType:
switch y.(type) {
case ConstIntType:
return x
case ConstRuneType, ConstFloatType, ConstComplexType:
return y
}
case ConstRuneType:
switch y.(type) {
case ConstIntType, ConstRuneType:
return x
case ConstFloatType, ConstComplexType:
return y
}
case ConstFloatType:
switch y.(type) {
case ConstIntType, ConstRuneType, ConstFloatType:
return x
case ConstComplexType:
return y
}
case ConstComplexType:
switch y.(type) {
case ConstIntType, ConstRuneType, ConstFloatType, ConstComplexType:
return x
}
}
panic("go-interactive: promoteConstNumbers called with non-numbers")
}
func convertConstToTyped(ctx *Ctx, from ConstType, c constValue, to reflect.Type, expr Expr) (
constValue, []error) {
v := hackedNew(to).Elem()
switch from.(type) {
case ConstIntType, ConstRuneType, ConstFloatType, ConstComplexType:
underlying := reflect.Value(c).Interface().(*ConstNumber)
switch to.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var errs []error
i, truncation, overflow := underlying.Value.Int(to.Bits())
if truncation {
errs = append(errs, ErrTruncatedConstant{at(ctx, expr), ConstInt, underlying})
}
if overflow {
errs = append(errs, ErrOverflowedConstant{at(ctx, expr), from, to, underlying})
}
// For some reason, the erros produced are "complex -> int" then "complex -> real"
_, truncation = underlying.Value.Real()
if truncation {
errs = append(errs, ErrTruncatedConstant{at(ctx, expr), ConstFloat, underlying})
}
v.SetInt(i)
return constValue(v), errs
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
var errs []error
u, truncation, overflow := underlying.Value.Uint(to.Bits())
if truncation {
errs = append(errs, ErrTruncatedConstant{at(ctx, expr), ConstInt, underlying})
}
if overflow {
errs = append(errs, ErrOverflowedConstant{at(ctx, expr), from, to, underlying})
}
// For some reason, the erros produced are "complex -> int" then "complex -> real"
_, truncation = underlying.Value.Real()
if truncation {
errs = append(errs, ErrTruncatedConstant{at(ctx, expr), ConstFloat, underlying})
}
v.SetUint(u)
return constValue(v), errs
case reflect.Float32, reflect.Float64:
var errs []error
f, truncation, _ := underlying.Value.Float64()
if truncation {
errs = []error{ErrTruncatedConstant{at(ctx, expr), ConstFloat, underlying}}
}
v.SetFloat(f)
return constValue(v), errs
case reflect.Complex64, reflect.Complex128:
cmplx, _ := underlying.Value.Complex128()
v.SetComplex(cmplx)
return constValue(v), nil
// string(97) is legal, equivalent of string('a')
case reflect.String:
if from.IsIntegral() {
i, _, overflow := underlying.Value.Int(32)
if overflow {
err := ErrOverflowedConstant{at(ctx, expr), from, ConstString, underlying}
return constValue{}, []error{err}
}
v.SetString(string(i))
return constValue(v), nil
}
}
case ConstStringType:
if v.Type().Kind() == reflect.String {
v.SetString(reflect.Value(c).String())
return constValue(v), nil
}
case ConstBoolType:
if to.Kind() == reflect.Bool {
v.SetBool(reflect.Value(c).Bool())
return constValue(v), nil
}
case ConstNilType:
// Unfortunately there is no reflect.Type.CanNil()
switch to.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface,
reflect.Map, reflect.Ptr, reflect.Slice:
// v is already nil
return constValue(v), nil
}
}
return constValue{}, []error{ErrBadConstConversion{at(ctx, expr), from, to, reflect.Value(c)}}
}