From 67967f029305c663fb3fbb4e0f1a6e375f5d572c Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 6 Sep 2022 12:10:57 +0100 Subject: Group variable declaration --- src/Parser.hs | 59 +++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 12 deletions(-) (limited to 'src/Parser.hs') diff --git a/src/Parser.hs b/src/Parser.hs index 2f60b52..2817208 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -63,6 +63,7 @@ type' = do try typFn <|> typ "type" +-- argument arg :: Parser (String, Type, Bool, SourcePos) arg = do pos <- getPosition @@ -71,6 +72,7 @@ arg = do t <- type' "type" return $ (i, t, True, pos) +-- function definition (common to def and lambda) fdef :: Ident -> Bool -> Bool -> SourcePos -> Parser Expr fdef ident priv anon pos = do args <- parens $ commaSep arg @@ -81,8 +83,13 @@ fdef ident priv anon pos = do rtyp <- type' "return type" return $ rtyp ) - body <- braces $ many statement - return $ Func ident args rtyp body priv anon pos + body <- + braces $ + many $ + try (grVar False) <|> do + x <- statement + pure $ [x] + return $ Func ident args rtyp (concat $ body) priv anon pos function :: Bool -> Parser Expr function priv = do @@ -91,20 +98,42 @@ function priv = do ident <- identifier fdef ident priv False pos -var :: Bool -> Parser Expr -var priv = do - reserved "var" +-- ident: type = value +varWithValue :: Bool -> Parser Expr +varWithValue priv = do (ident, typ, _, pos) <- arg + -- FIXME: this error hint is not being used reservedOp "=" "assignation" value <- expr - reservedOp ";" return $ Var ident typ value priv pos -privateDfn :: Parser Expr -privateDfn = do +-- group variable declaration +grVar :: Bool -> Parser [Expr] +grVar priv = do + reserved "var" + xs <- parens $ commaSep $ varWithValue priv + reservedOp ";" + return $ xs + +-- variable declaration +var :: Bool -> Parser Expr +var priv = do + reserved "var" + x <- varWithValue priv + reservedOp ";" + return $ x + +-- private definition +privateDf :: (Bool -> Parser Expr) -> Parser Expr +privateDf f = do + priv <- optionMaybe $ reserved "private" + f (isJust priv) + +-- private group definition +privateDfn :: (Bool -> Parser [Expr]) -> Parser [Expr] +privateDfn f = do priv <- optionMaybe $ reserved "private" - let isPriv = isJust priv - try (function isPriv) <|> var isPriv + f (isJust priv) lambdaId :: SourcePos -> Ident lambdaId s = @@ -162,8 +191,14 @@ module' = do program :: Parser [Expr] program = do m <- module' - n <- many $ do privateDfn <|> statement - return $ [m] ++ n + n <- + many $ + try (privateDfn grVar) <|> do + x <- + try (privateDf function) <|> try (privateDf var) + <|> statement + pure $ [x] + return $ [m] ++ (concat $ n) parse :: Parser [Expr] parse = program -- cgit v1.2.3