inferred funtypes added
[cc1516.git] / src / parse.icl
index cfd4fd1..fc6e740 100644 (file)
@@ -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"]