/
index.go
79 lines (64 loc) · 1.86 KB
/
index.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package ast
import (
"fmt"
"strings"
)
// Index represents an indexing operation into another data structure
type Index struct {
Target Node
Key Node
Posx Pos
}
func (n *Index) Accept(v Visitor) Node {
n.Target = n.Target.Accept(v)
n.Key = n.Key.Accept(v)
return v(n)
}
func (n *Index) Pos() Pos {
return n.Posx
}
func (n *Index) String() string {
return fmt.Sprintf("Index(%s, %s)", n.Target, n.Key)
}
func (n *Index) Type(s Scope) (Type, error) {
variableAccess, ok := n.Target.(*VariableAccess)
if !ok {
return TypeInvalid, fmt.Errorf("target is not a variable")
}
variable, ok := s.LookupVar(variableAccess.Name)
if !ok {
return TypeInvalid, fmt.Errorf("unknown variable accessed: %s", variableAccess.Name)
}
switch variable.Type {
case TypeList:
return n.typeList(variable, variableAccess.Name)
case TypeMap:
return n.typeMap(variable, variableAccess.Name)
default:
return TypeInvalid, fmt.Errorf("invalid index operation into non-indexable type: %s", variable.Type)
}
}
func (n *Index) typeList(variable Variable, variableName string) (Type, error) {
// We assume type checking has already determined that this is a list
list := variable.Value.([]Variable)
return VariableListElementTypesAreHomogenous(variableName, list)
}
func (n *Index) typeMap(variable Variable, variableName string) (Type, error) {
// We assume type checking has already determined that this is a map
vmap := variable.Value.(map[string]Variable)
return VariableMapValueTypesAreHomogenous(variableName, vmap)
}
func reportTypes(typesFound map[Type]struct{}) string {
stringTypes := make([]string, len(typesFound))
i := 0
for k, _ := range typesFound {
stringTypes[0] = k.String()
i++
}
return strings.Join(stringTypes, ", ")
}
func (n *Index) GoString() string {
return fmt.Sprintf("*%#v", *n)
}