X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=src%2Fparse.icl;h=fc6e740b946a818a46a582ce3ce4197727ffefb3;hb=cdb033817c0b30f0ff27b0f4aa67b7cf93c36b4d;hp=cfd4fd1a2b974dba0d617a6547bf81d6646d9a82;hpb=40d54a0715006e2baab1704eef15eecc4076668b;p=cc1516.git diff --git a/src/parse.icl b/src/parse.icl index cfd4fd1..fc6e740 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) @@ -69,7 +70,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 @@ -78,7 +79,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 @@ -203,7 +204,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"]