(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)
parseOneLine = liftM pure parseStmt
parseFunType :: Parser Token FunType
-parseFunType = satTok DoubleColonToken *>
+parseFunType = satTok DoubleColonToken *>
(parseInOutType <|> (liftM (FunType []) parseVoidOrType))
where
parseInOutType :: Parser Token FunType
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
(parseIdent >>= \i.if (i == "snd") (pure FieldSnd) empty))))
parseOp1 :: Parser Token Op1
-parseOp1 = trans1 DashToken UnMinus <|>
- trans1 ExclamationToken UnNegation
+parseOp1 = trans1 DashToken UnMinus <|> trans1 ExclamationToken UnNegation
parseBBraces :: (Parser Token a) -> Parser Token a
parseBBraces p = satTok BraceOpenToken *> p <* satTok BraceCloseToken
<* satTok BraceCloseToken
trans2 :: TokenValue (TokenValue -> a) -> Parser Token a
-trans2 t f = satTok t >>= \(_, r).pure (f r)
+trans2 t f = liftM (f o snd) $ satTok t
trans1 :: TokenValue a -> Parser Token a
trans1 t r = trans2 t $ const r
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"]
) ++ printStatements ss i
where
printCodeBlock :: [Stmt] Int -> [String]
- printCodeBlock [] _ = ["{}"]
+ printCodeBlock [] _ = ["{}\n"]
printCodeBlock [x] i = ["\n":printStatements [x] (i+1)]
printCodeBlock x i =
["{\n":printStatements x (i+1)] ++ indent i ["}\n"]