From 1942efe3c45c87ef4f1c73d6078ff6487b820b53 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Fri, 2 Sep 2022 16:57:07 +0100 Subject: Private only makes sense for global variables Also make error reporting slightly better on statements. --- src/Parser.hs | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) (limited to 'src/Parser.hs') diff --git a/src/Parser.hs b/src/Parser.hs index f3fd03b..2f60b52 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -84,13 +84,27 @@ fdef ident priv anon pos = do body <- braces $ many statement return $ Func ident args rtyp body priv anon pos -function :: Parser Expr -function = do +function :: Bool -> Parser Expr +function priv = do pos <- getPosition - priv <- optionMaybe $ reserved "private" reserved "def" ident <- identifier - fdef ident (isJust priv) False pos + fdef ident priv False pos + +var :: Bool -> Parser Expr +var priv = do + reserved "var" + (ident, typ, _, pos) <- arg + reservedOp "=" "assignation" + value <- expr + reservedOp ";" + return $ Var ident typ value priv pos + +privateDfn :: Parser Expr +privateDfn = do + priv <- optionMaybe $ reserved "private" + let isPriv = isJust priv + try (function isPriv) <|> var isPriv lambdaId :: SourcePos -> Ident lambdaId s = @@ -116,16 +130,6 @@ call = do args <- parens $ commaSep expr return $ Call ident args pos -var :: Parser Expr -var = do - priv <- optionMaybe $ reserved "private" - reserved "var" - (ident, typ, _, pos) <- arg - reservedOp "=" "assignation" - value <- expr - reservedOp ";" - return $ Var ident typ value (isJust priv) pos - factor :: Parser Expr factor = number @@ -145,6 +149,7 @@ exprStmt = do statement :: Parser Expr statement = do try exprStmt + <|> var False <|> return' module' :: Parser Expr @@ -157,11 +162,7 @@ module' = do program :: Parser [Expr] program = do m <- module' - n <- - many $ do - try function - <|> var - <|> statement "statement" + n <- many $ do privateDfn <|> statement return $ [m] ++ n parse :: Parser [Expr] -- cgit v1.2.3