-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from hashicorp/f-index-operation
Add support for indexing into variables
- Loading branch information
Showing
17 changed files
with
836 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.DS_Store | ||
.idea | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,4 +52,5 @@ const ( | |
TypeString | ||
TypeInt | ||
TypeFloat | ||
TypeList | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
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 { | ||
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) | ||
} | ||
if variable.Type != TypeList { | ||
return TypeInvalid, fmt.Errorf("invalid index operation into non-indexable type: %s", variable.Type) | ||
} | ||
|
||
list := variable.Value.([]Variable) | ||
|
||
// Ensure that the types of the list elements are homogenous | ||
listTypes := make(map[Type]struct{}) | ||
for _, v := range list { | ||
if _, ok := listTypes[v.Type]; ok { | ||
continue | ||
} | ||
listTypes[v.Type] = struct{}{} | ||
} | ||
|
||
if len(listTypes) != 1 { | ||
typesFound := make([]string, len(listTypes)) | ||
i := 0 | ||
for k, _ := range listTypes { | ||
typesFound[0] = k.String() | ||
i++ | ||
} | ||
types := strings.Join(typesFound, ", ") | ||
return TypeInvalid, fmt.Errorf("list %q does not have homogenous types. found %s", variableAccess.Name, types) | ||
} | ||
|
||
return list[0].Type, nil | ||
} | ||
|
||
func (n *Index) GoString() string { | ||
return fmt.Sprintf("*%#v", *n) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package ast | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestIndexType_string(t *testing.T) { | ||
i := &Index{ | ||
Target: &VariableAccess{Name: "foo"}, | ||
Key: &LiteralNode{ | ||
Typex: TypeInt, | ||
Value: 1, | ||
}, | ||
} | ||
|
||
scope := &BasicScope{ | ||
VarMap: map[string]Variable{ | ||
"foo": Variable{ | ||
Type: TypeList, | ||
Value: []Variable{ | ||
Variable{ | ||
Type: TypeString, | ||
Value: "Hello", | ||
}, | ||
Variable{ | ||
Type: TypeString, | ||
Value: "World", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
actual, err := i.Type(scope) | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
if actual != TypeString { | ||
t.Fatalf("bad: %s", actual) | ||
} | ||
} | ||
|
||
func TestIndexType_int(t *testing.T) { | ||
i := &Index{ | ||
Target: &VariableAccess{Name: "foo"}, | ||
Key: &LiteralNode{ | ||
Typex: TypeInt, | ||
Value: 1, | ||
}, | ||
} | ||
|
||
scope := &BasicScope{ | ||
VarMap: map[string]Variable{ | ||
"foo": Variable{ | ||
Type: TypeList, | ||
Value: []Variable{ | ||
Variable{ | ||
Type: TypeInt, | ||
Value: 34, | ||
}, | ||
Variable{ | ||
Type: TypeInt, | ||
Value: 54, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
actual, err := i.Type(scope) | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
if actual != TypeInt { | ||
t.Fatalf("bad: %s", actual) | ||
} | ||
} | ||
|
||
func TestIndexType_nonHomogenous(t *testing.T) { | ||
i := &Index{ | ||
Target: &VariableAccess{Name: "foo"}, | ||
Key: &LiteralNode{ | ||
Typex: TypeInt, | ||
Value: 1, | ||
}, | ||
} | ||
|
||
scope := &BasicScope{ | ||
VarMap: map[string]Variable{ | ||
"foo": Variable{ | ||
Type: TypeList, | ||
Value: []Variable{ | ||
Variable{ | ||
Type: TypeString, | ||
Value: "Hello", | ||
}, | ||
Variable{ | ||
Type: TypeInt, | ||
Value: 43, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
_, err := i.Type(scope) | ||
if err == nil { | ||
t.Fatalf("expected error") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.