summaryrefslogtreecommitdiff
path: root/src/Parser
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2024-09-27 20:30:25 +0200
committerRoman Smrž <roman.smrz@seznam.cz>2024-09-27 21:03:29 +0200
commitafd550dc8245e61ab6b148c72cdf133e6b7836d1 (patch)
tree9ceff9a181af372a35c9b906cacca11c7c26b18b /src/Parser
parent213e3523aead4c18b65ac85886203d2508b9b27e (diff)
Default and context-provided values for function arguments
Diffstat (limited to 'src/Parser')
-rw-r--r--src/Parser/Core.hs18
-rw-r--r--src/Parser/Expr.hs4
2 files changed, 15 insertions, 7 deletions
diff --git a/src/Parser/Core.hs b/src/Parser/Core.hs
index ab6079a..f40889a 100644
--- a/src/Parser/Core.hs
+++ b/src/Parser/Core.hs
@@ -134,11 +134,19 @@ unifyExpr off pa expr = if
| Just (Refl :: FunctionType a :~: b) <- eqT
-> do
- case exprArgs expr of
- remaining
- | anull remaining -> return (FunctionEval expr)
- | otherwise -> parseError $ FancyError off $ S.singleton $ ErrorFail $ T.unpack $
- "missing function arguments"
+ let FunctionArguments remaining = exprArgs expr
+ showType ( Nothing, SomeArgumentType atype ) = "`<" <> textExprType atype <> ">'"
+ showType ( Just (ArgumentKeyword kw), SomeArgumentType atype ) = "`" <> kw <> " <" <> textExprType atype <> ">'"
+ err = parseError . FancyError off . S.singleton . ErrorFail . T.unpack
+
+ defaults <- forM (M.toAscList remaining) $ \case
+ arg@(_, SomeArgumentType NoDefault) -> err $ "missing " <> showType arg <> " argument"
+ (kw, SomeArgumentType (ExprDefault def)) -> return (kw, SomeExpr def)
+ (kw, SomeArgumentType atype@ContextDefault) -> do
+ SomeExpr context <- gets testContext
+ context' <- unifyExpr off atype context
+ return (kw, SomeExpr context')
+ return (FunctionEval $ ArgsApp (FunctionArguments $ M.fromAscList defaults) expr)
| Just (Refl :: DynamicType :~: b) <- eqT
, Undefined msg <- expr
diff --git a/src/Parser/Expr.hs b/src/Parser/Expr.hs
index 8ae0f77..4b1a89e 100644
--- a/src/Parser/Expr.hs
+++ b/src/Parser/Expr.hs
@@ -355,9 +355,9 @@ variable = label "variable" $ do
SomeExpr e'@(FunVariable (FunctionArguments argTypes) _ _) -> do
let check poff kw expr = do
case M.lookup kw argTypes of
- Just expected -> do
+ Just (SomeArgumentType (_ :: ArgumentType expected)) -> do
withRecovery registerParseError $ do
- void $ unify poff expected (someExprType expr)
+ void $ unify poff (ExprTypePrim (Proxy @expected)) (someExprType expr)
return expr
Nothing -> do
registerParseError $ FancyError poff $ S.singleton $ ErrorFail $ T.unpack $