From 7c8f4d0b1232c7afb0fb4addcfbb6e486ddb06d3 Mon Sep 17 00:00:00 2001 From: Dave Cunningham Date: Wed, 10 Jan 2018 20:43:55 -0500 Subject: [PATCH] Call top level function even if there are no TLAs (match cpp semantics) (#169) * Call top level function even if there are no TLAs (match cpp semantics) --- ast/ast.go | 3 ++ interpreter.go | 30 +++++++++---------- ...tion.golden => function_manifested.golden} | 0 testdata/function_manifested.jsonnet | 3 ++ testdata/function_no_params.golden | 1 + ...ion.jsonnet => function_no_params.jsonnet} | 0 testdata/function_too_many_params.golden | 5 ++++ testdata/function_too_many_params.jsonnet | 1 + 8 files changed, 27 insertions(+), 16 deletions(-) rename testdata/{function.golden => function_manifested.golden} (100%) create mode 100644 testdata/function_manifested.jsonnet create mode 100644 testdata/function_no_params.golden rename testdata/{function.jsonnet => function_no_params.jsonnet} (100%) create mode 100644 testdata/function_too_many_params.golden create mode 100644 testdata/function_too_many_params.jsonnet diff --git a/ast/ast.go b/ast/ast.go index 1e9f389f3..41549146a 100644 --- a/ast/ast.go +++ b/ast/ast.go @@ -444,6 +444,9 @@ const ( ObjectFieldExpr // '['expr1']':[:[:]] expr2 ObjectFieldStr // expr1:[:[:]] expr2 ObjectLocal // local id = expr2 + ObjectNullID // null id + ObjectNullExpr // null '['expr1']' + ObjectNullStr // null expr1 ) type ObjectFieldHide int diff --git a/interpreter.go b/interpreter.go index 3e9511611..cc7238c96 100644 --- a/interpreter.go +++ b/interpreter.go @@ -942,22 +942,20 @@ func evaluateAux(i *interpreter, node ast.Node, tla vmExtMap) (value, *TraceElem if err != nil { return nil, nil, err } - if len(tla) != 0 { - // If it's not a function, ignore TLA - if f, ok := result.(*valueFunction); ok { - toplevelArgMap := prepareExtVars(i, tla, "top-level-arg") - args := callArguments{} - for argName, pv := range toplevelArgMap { - args.named = append(args.named, namedCallArgument{name: ast.Identifier(argName), pv: pv}) - } - funcLoc := ast.MakeLocationRangeMessage("Top-level function") - funcTrace := &TraceElement{ - loc: &funcLoc, - } - result, err = f.call(args).getValue(i, funcTrace) - if err != nil { - return nil, nil, err - } + // If it's not a function, ignore TLA + if f, ok := result.(*valueFunction); ok { + toplevelArgMap := prepareExtVars(i, tla, "top-level-arg") + args := callArguments{} + for argName, pv := range toplevelArgMap { + args.named = append(args.named, namedCallArgument{name: ast.Identifier(argName), pv: pv}) + } + funcLoc := ast.MakeLocationRangeMessage("Top-level function") + funcTrace := &TraceElement{ + loc: &funcLoc, + } + result, err = f.call(args).getValue(i, funcTrace) + if err != nil { + return nil, nil, err } } manifestationLoc := ast.MakeLocationRangeMessage("During manifestation") diff --git a/testdata/function.golden b/testdata/function_manifested.golden similarity index 100% rename from testdata/function.golden rename to testdata/function_manifested.golden diff --git a/testdata/function_manifested.jsonnet b/testdata/function_manifested.jsonnet new file mode 100644 index 000000000..265a3763f --- /dev/null +++ b/testdata/function_manifested.jsonnet @@ -0,0 +1,3 @@ +{ + f(x): 3, +} diff --git a/testdata/function_no_params.golden b/testdata/function_no_params.golden new file mode 100644 index 000000000..d81cc0710 --- /dev/null +++ b/testdata/function_no_params.golden @@ -0,0 +1 @@ +42 diff --git a/testdata/function.jsonnet b/testdata/function_no_params.jsonnet similarity index 100% rename from testdata/function.jsonnet rename to testdata/function_no_params.jsonnet diff --git a/testdata/function_too_many_params.golden b/testdata/function_too_many_params.golden new file mode 100644 index 000000000..273e011c1 --- /dev/null +++ b/testdata/function_too_many_params.golden @@ -0,0 +1,5 @@ +RUNTIME ERROR: Missing argument: x +------------------------------------------------- + Top-level function + + diff --git a/testdata/function_too_many_params.jsonnet b/testdata/function_too_many_params.jsonnet new file mode 100644 index 000000000..1285c2e83 --- /dev/null +++ b/testdata/function_too_many_params.jsonnet @@ -0,0 +1 @@ +function(x) 3