zclsyntax: UnaryOpExpr.Value

This commit is contained in:
Martin Atkins 2017-06-12 07:13:17 -07:00
parent e709d7bcc0
commit 9be04673c3
2 changed files with 50 additions and 1 deletions

View File

@ -211,7 +211,44 @@ func (e *UnaryOpExpr) walkChildNodes(w internalWalkFunc) {
}
func (e *UnaryOpExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) {
panic("UnaryOpExpr.Value not yet implemented")
impl := e.Op.Impl // assumed to be a function taking exactly one argument
params := impl.Params()
param := params[0]
givenVal, diags := e.Val.Value(ctx)
val, err := convert.Convert(givenVal, param.Type)
if err != nil {
diags = append(diags, &zcl.Diagnostic{
Severity: zcl.DiagError,
Summary: "Invalid operand",
Detail: fmt.Sprintf("Unsuitable value for unary operand: %s.", err),
Subject: e.Val.Range().Ptr(),
Context: &e.SrcRange,
})
}
if diags.HasErrors() {
// Don't actually try the call if we have errors already, since the
// this will probably just produce a confusing duplicative diagnostic.
return cty.UnknownVal(e.Op.Type), diags
}
args := []cty.Value{val}
result, err := impl.Call(args)
if err != nil {
diags = append(diags, &zcl.Diagnostic{
// FIXME: This diagnostic is useless.
Severity: zcl.DiagError,
Summary: "Operation failed",
Detail: fmt.Sprintf("Error during operation: %s.", err),
Subject: e.Val.Range().Ptr(),
Context: &e.SrcRange,
})
return cty.UnknownVal(e.Op.Type), diags
}
return result, diags
}
func (e *UnaryOpExpr) Range() zcl.Range {

View File

@ -114,6 +114,18 @@ func TestExpressionParseAndValue(t *testing.T) {
cty.True,
0,
},
{
`(- 2)`,
nil,
cty.NumberIntVal(-2),
0,
},
{
`(! true)`,
nil,
cty.False,
0,
},
{
`(
1