diff options
author | Juan J. Martinez <jjm@usebox.net> | 2022-09-02 16:57:07 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2022-09-02 16:57:07 +0100 |
commit | 1942efe3c45c87ef4f1c73d6078ff6487b820b53 (patch) | |
tree | 662458842355e034d1f188361a001a6c161390a4 /src | |
parent | f66874f1f5066e57ef5761cd7c87b5d498fd89b6 (diff) | |
download | micro-lang-hs-1942efe3c45c87ef4f1c73d6078ff6487b820b53.tar.gz micro-lang-hs-1942efe3c45c87ef4f1c73d6078ff6487b820b53.zip |
Private only makes sense for global variables
Also make error reporting slightly better on statements.
Diffstat (limited to 'src')
-rw-r--r-- | src/Parser.hs | 39 |
1 files changed, 20 insertions, 19 deletions
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] |