add string patterns

This commit is contained in:
darkf 2013-10-23 15:31:37 -07:00
parent 0dcc1707ee
commit 47d43351e2
3 changed files with 10 additions and 2 deletions

1
ast.hs
View File

@ -31,6 +31,7 @@ data AST = Add AST AST
data Pattern = VarP String
| IntP Integer
| StrP String
| UnitP
| ConsP Pattern Pattern
| TupleP [Pattern]

View File

@ -199,6 +199,11 @@ patternBindings (IntP n) _ = Nothing
patternBindings UnitP UnitV = Just M.empty
patternBindings UnitP _ = Nothing
patternBindings (StrP x) (StrV y)
| x == y = Just M.empty
| otherwise = Nothing
patternBindings (StrP _) _ = Nothing
-- cons on strings
patternBindings (ConsP x (ListP [])) (StrV (y:[])) = patternBindings x (StrV [y])
patternBindings (ConsP xp xsp) (StrV (x:xs)) =
@ -217,7 +222,7 @@ patternBindings (ConsP xp xsp) (ListV (x:xs)) =
patternBindings (ConsP _ _) _ = Nothing
-- lists
patternBindings (ListP []) (ListV (x:xs)) = Nothing -- not enough patterns
patternBindings (ListP []) (ListV (x:_)) = Nothing -- not enough patterns
patternBindings (ListP (_:_)) (ListV []) = Nothing -- not enough values
patternBindings (ListP []) (ListV []) = Just M.empty -- base case
patternBindings (ListP (x:xs)) (ListV (y:ys)) =

View File

@ -85,10 +85,11 @@ emptyTuple cons = do
intPattern = fmap IntP integer
varPattern = fmap VarP identifier
stringPattern = fmap StrP stringLiteral
listPattern = listSeq pattern ListP
consPattern = do
x <- intPattern <|> varPattern
x <- intPattern <|> varPattern <|> stringPattern
symbol "::"
y <- pattern
return $ ConsP x y
@ -99,6 +100,7 @@ pattern = try consPattern
<|> listPattern
<|> varPattern
<|> intPattern
<|> stringPattern
patterns = sepBy pattern (symbol ",")