Merge branch 'master' of https://github.com/dopefishh/cc1516
authorpimjager <pim@pimjager.nl>
Mon, 29 Feb 2016 13:34:38 +0000 (14:34 +0100)
committerpimjager <pim@pimjager.nl>
Mon, 29 Feb 2016 13:34:38 +0000 (14:34 +0100)
1  2 
src/parse.icl

diff --combined src/parse.icl
@@@ -24,13 -24,14 +24,14 @@@ parser (Right r) = case runParser parse
        (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)
  
@@@ -52,11 -53,10 +53,11 @@@ parseStmt = parseIfStmt <|> parseWhileS
  
                parseIfStmt :: Parser Token Stmt
                parseIfStmt = liftM3 IfStmt
 -                      (satTok IfToken *> parseBBraces parseExpr)
 -                      (parseBlock <|> parseOneLine)
 -                      (liftM (fromMaybe []) 
 -                              (optional (satTok ElseToken *> (parseBlock<|> parseOneLine))))
 +            (satTok IfToken *> parseBBraces parseExpr)
 +            (parseBlock <|> parseOneLine)
 +            (liftM (fromMaybe []) 
 +                (optional (satTok ElseToken *> (parseBlock<|> parseOneLine))))
 +
  
                parseWhileStmt :: Parser Token Stmt
                parseWhileStmt = satTok WhileToken *> 
@@@ -70,7 -70,7 +71,7 @@@
                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 -79,7 +80,7 @@@
  
                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
@@@ -176,9 -176,7 +177,9 @@@ trans1 t r = trans2 t $ const 
  derive gPrint TokenValue
  derive gEq TokenValue
  satTok :: TokenValue -> Parser Token Token
 -satTok t = top >>= \tok=:(pos, tv) -> if (eq t tok) (return tok) (fail <?> (printToString tv, pos))
 +satTok t = top >>= \tok=:(pos, tv) -> if (eq t tok) 
 +                                            (return tok) 
 +                                            (fail <?> (printToString tv+++printToString t, pos))
        where
                eq (IdentToken _) (_, IdentToken _) = True
                eq (NumberToken _) (_, NumberToken _) = True
@@@ -206,7 -204,7 +207,7 @@@ printersperse i j = intercalate [i] (ma
  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"]