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 data Pattern = VarP String
| IntP Integer | IntP Integer
| StrP String
| UnitP | UnitP
| ConsP Pattern Pattern | ConsP Pattern Pattern
| TupleP [Pattern] | TupleP [Pattern]

View File

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

View File

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