Skip to content

Commit

Permalink
Add wildcard patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
qsctr committed Aug 22, 2018
1 parent e4c0909 commit 13402f7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
8 changes: 8 additions & 0 deletions docs/language-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ def answer

Note that the above function will not work if it is called with any argument other than those three that it pattern matches on, because it lacks a catch-all pattern.

You can use `_` to match any value and discard the result.

```
def answer
"hello" -> "hi"
_ -> "I don't understand"
```

Multi-argument functions are automatically curried, so they can be partially applied.

```
Expand Down
6 changes: 4 additions & 2 deletions src/Language/Dtfpl/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ defAlt = addLoc $ exprBlockMid $

-- | Parse a pattern.
pat :: PParsec p => p (A Pat 'Source)
pat = varPat <|> litPat
pat = varPat <|> litPat <|> wildPat
where varPat = addLoc $ VarPat <$> ident
litPat = addLoc $ LitPat <$> literal
wildPat = addLoc $ wildcard $> WildPat

-- | Given a parser for some node constructor that takes an 'Expr' as argument,
-- run that parser and run the 'expr' parser so that the continuation indent
Expand Down Expand Up @@ -181,11 +182,12 @@ symbol :: PParsec p => String -> p ()
symbol = void . string

-- | Symbol parser.
arrow, comma, equals, lambda :: PParsec p => p ()
arrow, comma, equals, lambda, wildcard :: PParsec p => p ()
arrow = symbol "->"
comma = symbol ","
equals = symbol "="
lambda = symbol "\\"
wildcard = symbol "_"

-- | Characters which cannot be used in identifiers.
{-# ANN reservedChars "HLint: ignore Use String" #-}
Expand Down
3 changes: 0 additions & 3 deletions src/Language/Dtfpl/Simplify/UnDef.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ import Language.Dtfpl.Syntax
-- > 3, 4 -> b
-- > _, _ -> c
--
-- (Note that this isn't actually valid source for now since wildcard patterns
-- aren't supported yet.)
--
-- The first 'VarPat' in each column of the 'DefAlt's will be used as the lambda
-- parameter name for that parameter. If there is no 'VarPat' in that column,
-- a 'GenIdentPart' based on the name of the function will be used.
Expand Down

0 comments on commit 13402f7

Please sign in to comment.