Add support for variadic parameters

Everything was already in place, but not used for variadic parameters.

On the other hand fixed parameters sets are currently bugged, as they don't
reject extra arguments (they act like variadic parameters set). This commit adds
a TODO mentionning that.
This commit is contained in:
Georges Dubus 2018-03-09 14:09:43 +01:00
parent 6bee6ecc91
commit 0f7332636e
2 changed files with 15 additions and 4 deletions

View file

@ -93,10 +93,15 @@ atomText (NUri uri) = uri
buildArgument :: Params (NValue m) -> NValue m -> ValueSet m
buildArgument paramSpec arg = either error id $ case paramSpec of
Param name -> return $ Map.singleton name arg
ParamSet (FixedParamSet s) Nothing -> lookupParamSet s
ParamSet (FixedParamSet s) (Just name) ->
Map.insert name arg <$> lookupParamSet s
ParamSet _ _ -> error "Can't yet handle variadic param sets"
-- TODO FixedParamSet should check that no extra args are passed
ParamSet paramSet setName ->
let actualParamSet = case paramSet of
FixedParamSet s -> s
VariadicParamSet s -> s
maybeAddSet = case setName of
Just name -> Map.insert name arg
Nothing -> id
in maybeAddSet <$> lookupParamSet actualParamSet
where
go env k def = maybe (Left err) return $ Map.lookup k env <|> def
where err = "Could not find " ++ show k

View file

@ -36,6 +36,12 @@ case_function_set_two_arg = constantEqualStr "2" "({ a, b ? 3 }: b - a) { a = 1;
case_function_definition_uses_environment :: Assertion
case_function_definition_uses_environment = constantEqualStr "3" "let f = (let a=1; in x: x+a); in f 2"
case_function_atpattern :: Assertion
case_function_atpattern = constantEqualStr "2" "(({a}@attrs:attrs) {a=2;}).a"
case_function_ellipsis :: Assertion
case_function_ellipsis = constantEqualStr "2" "(({a, ...}@attrs:attrs) {a=0; b=2;}).b"
tests :: TestTree
tests = $testGroupGenerator