diff options
author | Tom Smeding <tom.smeding@gmail.com> | 2020-07-24 21:53:41 +0200 |
---|---|---|
committer | Tom Smeding <tom.smeding@gmail.com> | 2020-07-24 21:53:41 +0200 |
commit | b5b044ceb4c178a656c0ddc27adec4a719b35893 (patch) | |
tree | 17d8dfbd905b82cd325068a3353a31e999f75cf7 /parser/CC | |
parent | 50fe19860cf143de939671926118ba0cf8c9f35c (diff) |
Support tuples
Diffstat (limited to 'parser/CC')
-rw-r--r-- | parser/CC/Parser.hs | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/parser/CC/Parser.hs b/parser/CC/Parser.hs index 856a7b9..a097f98 100644 --- a/parser/CC/Parser.hs +++ b/parser/CC/Parser.hs @@ -51,7 +51,16 @@ pType :: Parser Type pType = chainr1 pTypeAtom (symbol "->" >> return TFun) pTypeAtom :: Parser Type -pTypeAtom = (wordToken "Int" >> return TInt) <|> between (token "(") (token ")") pType +pTypeAtom = (wordToken "Int" >> return TInt) <|> pParenType + +pParenType :: Parser Type +pParenType = do + token "(" + tys <- pType `sepBy` token "," + token ")" + case tys of + [ty] -> return ty + _ -> return (TTup tys) pExpr :: Parser Expr pExpr = label (pCall <|> pLam) "expression" @@ -84,7 +93,18 @@ pExprAtom :: Parser Expr pExprAtom = choice [ uncurry (flip Int) <$> pInt , uncurry (flip Var) <$> pName - , between (token "(") (token ")") pExpr ] + , pParenExpr ] + +pParenExpr :: Parser Expr +pParenExpr = do + p1 <- getPosition + token "(" + exprs <- pExpr `sepBy` token "," + token ")" + p2 <- getPosition + case exprs of + [expr] -> return expr + _ -> return (Tup (SourceRange p1 p2) exprs) pInt :: Parser (Int, SourceRange) pInt = try (whitespace >> pInt0) |