-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathevalindexexpr.go
More file actions
41 lines (38 loc) · 870 Bytes
/
Copy pathevalindexexpr.go
File metadata and controls
41 lines (38 loc) · 870 Bytes
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
package eval
import (
"reflect"
)
func evalIndexExpr(index *IndexExpr, env Env) ([]reflect.Value, error) {
xs, err := EvalExpr(index.X, env)
if err != nil {
return []reflect.Value{}, err
}
x := xs[0]
t := index.X.KnownType()[0]
switch t.Kind() {
case reflect.Map:
k, err := evalTypedExpr(index.Index, knownType{t.Key()}, env)
if err != nil {
return []reflect.Value{}, err
}
v := x.MapIndex(k[0])
ok := v.IsValid()
if !ok {
v = reflect.New(t.Key()).Elem()
}
return []reflect.Value{v, reflect.ValueOf(ok)}, nil
case reflect.Ptr:
// Short hand for array pointers
x = x.Elem()
fallthrough
default:
i, err := evalInteger(index.Index, env)
if err != nil {
return []reflect.Value{}, err
}
if !(0 <= i && i < x.Len()) {
return []reflect.Value{}, PanicIndexOutOfBounds{}
}
return []reflect.Value{x.Index(i)}, nil
}
}