fix: deal with literals in match (#1146)

This commit is contained in:
Veit Heller 2021-01-26 06:18:32 +01:00 committed by GitHub
parent 8c1999d656
commit 07f6330bf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 1 deletions

View File

@ -132,7 +132,7 @@ expand eval ctx xobj =
| even (length rest) ->
do
(ctx', expandedExpr) <- expand eval ctx expr
(newCtx, expandedPairs) <- foldlM successiveExpandLR (ctx', Right []) (pairwise rest)
(newCtx, expandedPairs) <- foldlM successiveExpandLRMatch (ctx', Right []) (pairwise rest)
pure
( newCtx,
do
@ -274,6 +274,17 @@ expand eval ctx xobj =
case expandedR of
Right v -> pure (newCtx, Right (lst ++ [[l, v]]))
Left err -> pure (newCtx, Left err)
successiveExpandLRMatch a (l, r) =
case traverseExpandLiteral l of
Left (err, x) -> pure (evalError ctx err (xobjInfo x))
_ -> successiveExpandLR a (l, r)
traverseExpandLiteral (XObj (Lst objs) _ _) =
case mapM traverseExpandLiteral objs of
Left e -> Left e
_ -> Right ()
traverseExpandLiteral (XObj (Sym _ _) _ _) = Right ()
traverseExpandLiteral other =
Left ("I cant use `" ++ pretty other ++ "` in match, only lists and symbols are allowed", other)
-- | Replace all the infoIdentifier:s on all nested XObj:s
setNewIdentifiers :: XObj -> XObj

View File

@ -0,0 +1 @@
match-literals.carp:5:18 I cant use `0` in match, only lists and symbols are allowed

View File

@ -0,0 +1,6 @@
(Project.config "file-path-print-length" "short")
(defn test [m]
(match m
(Maybe.Just 0) (println* "hi")
_ (println* "bye")))