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. --- language.md | 2 +- src/Parser.hs | 39 ++++++++++++++++++++------------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/language.md b/language.md index 2440cc7..ec0a425 100644 --- a/language.md +++ b/language.md @@ -74,7 +74,7 @@ p; # whatever byte is in address 0x8000 (peek) p = 0; # byte at 0x8000 is now 0 (poke) ``` -Variables are exported by default, unless they are defined as private: +Global variables are exported by default, unless they are defined as private: ``` private val local: u8 = 123; ``` 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