diff --git a/zcl/zclsyntax/expression.go b/zcl/zclsyntax/expression.go index 9ed23d8..5dcfbe3 100644 --- a/zcl/zclsyntax/expression.go +++ b/zcl/zclsyntax/expression.go @@ -83,7 +83,10 @@ func (e *RelativeTraversalExpr) walkChildNodes(w internalWalkFunc) { } func (e *RelativeTraversalExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { - panic("RelativeTraversalExpr.Value not yet implemented") + src, diags := e.Source.Value(ctx) + ret, travDiags := e.Traversal.TraverseRel(src) + diags = append(diags, travDiags...) + return ret, diags } func (e *RelativeTraversalExpr) Range() zcl.Range { @@ -413,7 +416,13 @@ func (e *IndexExpr) walkChildNodes(w internalWalkFunc) { } func (e *IndexExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { - panic("IndexExpr.Value not yet implemented") + var diags zcl.Diagnostics + coll, collDiags := e.Collection.Value(ctx) + key, keyDiags := e.Key.Value(ctx) + diags = append(diags, collDiags...) + diags = append(diags, keyDiags...) + + return zcl.Index(coll, key, &e.SrcRange) } func (e *IndexExpr) Range() zcl.Range { diff --git a/zcl/zclsyntax/expression_test.go b/zcl/zclsyntax/expression_test.go index cae5a05..c44822e 100644 --- a/zcl/zclsyntax/expression_test.go +++ b/zcl/zclsyntax/expression_test.go @@ -272,6 +272,45 @@ upper( }), 0, }, + + { + `["hello"][0]`, + nil, + cty.StringVal("hello"), + 0, + }, + { + `[][0]`, + nil, + cty.DynamicVal, + 1, // invalid index + }, + { + `["hello"][negate(0)]`, + &zcl.EvalContext{ + Functions: map[string]function.Function{ + "negate": stdlib.NegateFunc, + }, + }, + cty.StringVal("hello"), + 0, + }, + { + `[][negate(0)]`, + &zcl.EvalContext{ + Functions: map[string]function.Function{ + "negate": stdlib.NegateFunc, + }, + }, + cty.DynamicVal, + 1, // invalid index + }, + { + `["hello"]["0"]`, // key gets converted to number + nil, + cty.StringVal("hello"), + 0, + }, } for _, test := range tests {