Simplistic argument checking

To be expanded when optional arguments arrive.
This commit is contained in:
Stanisław Barzowski 2017-09-26 17:55:14 -04:00 committed by Dave Cunningham
parent a92b30146a
commit 544fe25700
8 changed files with 20 additions and 1 deletions

1
testdata/bad_function_call.golden vendored Normal file
View File

@ -0,0 +1 @@
RUNTIME ERROR: function expected 1 argument(s), but got 0

1
testdata/bad_function_call.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
(function(x) x)()

1
testdata/bad_function_call2.golden vendored Normal file
View File

@ -0,0 +1 @@
RUNTIME ERROR: function expected 1 argument(s), but got 2

1
testdata/bad_function_call2.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
(function(x) x)(1, 2)

View File

@ -0,0 +1 @@
RUNTIME ERROR: function expected 1 argument(s), but got 2

View File

@ -0,0 +1 @@
(function(x) x)(error "x", error "y")

View File

@ -80,7 +80,10 @@ func makeCallThunk(ec evalCallable, args callArguments) potentialValue {
func (th *callThunk) getValue(i *interpreter, trace *TraceElement) (value, error) {
evaluator := makeEvaluator(i, trace)
// TODO(sbarzowski): actually this trace is kinda useless inside...
err := checkArguments(evaluator, th.args, th.function.Parameters())
if err != nil {
return nil, err
}
return th.function.EvalCall(th.args, evaluator)
}

View File

@ -244,6 +244,16 @@ func (f *valueFunction) parameters() ast.Identifiers {
return f.ec.Parameters()
}
func checkArguments(e *evaluator, args callArguments, params ast.Identifiers) error {
// TODO(sbarzowski) this will get much more complicated with named params
numPassed := len(args.positional)
numExpected := len(params)
if numPassed != numExpected {
return e.Error(fmt.Sprintf("function expected %v argument(s), but got %v", numExpected, numPassed))
}
return nil
}
func (f *valueFunction) typename() string {
return "function"
}