Skip to content

Commit

Permalink
Call top level function even if there are no TLAs (match cpp semantic…
Browse files Browse the repository at this point in the history
…s) (google#169)

* Call top level function even if there are no TLAs (match cpp semantics)
  • Loading branch information
sparkprime authored Jan 11, 2018
1 parent c7a5b68 commit 7c8f4d0
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 16 deletions.
3 changes: 3 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 14 additions & 16 deletions interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions testdata/function_manifested.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
f(x): 3,
}
1 change: 1 addition & 0 deletions testdata/function_no_params.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
File renamed without changes.
5 changes: 5 additions & 0 deletions testdata/function_too_many_params.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RUNTIME ERROR: Missing argument: x
-------------------------------------------------
Top-level function


1 change: 1 addition & 0 deletions testdata/function_too_many_params.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function(x) 3

0 comments on commit 7c8f4d0

Please sign in to comment.