-
Notifications
You must be signed in to change notification settings - Fork 7
/
evalsliceexpr.go
45 lines (41 loc) · 942 Bytes
/
evalsliceexpr.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
package eval
import (
"reflect"
)
// TODO[crc] support slice[::] syntax after go1.2 upgrade
func evalSliceExpr(slice *SliceExpr, env Env) (reflect.Value, error) {
xs, err := EvalExpr(slice.X, env)
if err != nil {
return reflect.Value{}, err
}
x := xs[0]
var l, h int
if slice.Low != nil {
if l, err = evalInteger(slice.Low, env); err != nil {
return reflect.Value{}, err
}
}
if slice.High != nil {
if h, err = evalInteger(slice.High, env); err != nil {
return reflect.Value{}, err
}
} else {
h = x.Len()
}
t := slice.KnownType()[0]
switch t.Kind() {
case reflect.Ptr:
// Short hand for array pointers
x = x.Elem()
fallthrough
case reflect.Array, reflect.String:
if l < 0 || h > x.Len() || h < l {
return reflect.Value{}, PanicSliceOutOfBounds{}
}
case reflect.Slice:
if l < 0 || h > x.Cap() || h < l {
return reflect.Value{}, PanicSliceOutOfBounds{}
}
}
return x.Slice(l, h), nil
}