X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=src%2Fparse.icl;h=d64efcb97c7111b3854e014200a8dc5808b38c38;hb=43d45105726b3ad3e5cc5165f8c9b066ec8d2790;hp=edc23ac51c1b35e0f0470935d8671c684e1d48e4;hpb=da0449d1b0998b76941699ace4a806022b52523d;p=cc1516.git diff --git a/src/parse.icl b/src/parse.icl index edc23ac..d64efcb 100644 --- a/src/parse.icl +++ b/src/parse.icl @@ -24,13 +24,14 @@ parser (Right r) = case runParser parseProgram r of (Left e, _) = Left $ toString e parseProgram :: Parser Token AST -parseProgram = liftM2 AST (many parseVarDecl) (some parseFunDecl) +parseProgram = liftM2 AST (many parseVarDecl) (some parseFunDecl) + <* satTok EndOfFileToken parseFunDecl :: Parser Token FunDecl parseFunDecl = liftM5 FunDecl (parseIdent <* satTok BraceOpenToken) (parseSepList CommaToken parseIdent <* satTok BraceCloseToken) - (parseFunType <* satTok CBraceOpenToken) + (optional parseFunType <* satTok CBraceOpenToken) (many parseVarDecl) (many parseStmt <* satTok CBraceCloseToken) @@ -70,7 +71,7 @@ parseStmt = parseIfStmt <|> parseWhileStmt <|> parseOneLine = liftM pure parseStmt parseFunType :: Parser Token FunType -parseFunType = satTok DoubleColonToken *> +parseFunType = satTok DoubleColonToken *> (parseInOutType <|> (liftM (FunType []) parseVoidOrType)) where parseInOutType :: Parser Token FunType @@ -79,7 +80,7 @@ parseFunType = satTok DoubleColonToken *> parseVoidOrType :: Parser Token (Maybe Type) parseVoidOrType = (satTok VoidToken *> pure Nothing) <|> - (liftM Just parseType) + (liftM Just parseType) <|> pure Nothing parseVarDecl :: Parser Token VarDecl parseVarDecl = liftM3 VarDecl @@ -206,7 +207,7 @@ printersperse i j = intercalate [i] (map print j) instance print FunDecl where print (FunDecl i as t vs ss) = ["\n", i, " (":printersperse "," as] ++ - [") :: ":print t] ++ + [")"] ++ maybe [] (\tt->[" :: ":print tt]) t ++ ["{\n\t":printersperse "\n\t" vs] ++ ["\n":printStatements ss 1] ++ ["}\n"]