X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=src%2Fparse.icl;h=f57c5e536c22934cf438c374ed1907b436c7d63d;hb=3944a17b55128c2b75bf1948bbf0561a4b6ca783;hp=d04b0f94c28b169bf5aa9f0f6e7b7db628330fad;hpb=3faf0ef4123af219f9f10560b5fe1aa041a37c4a;p=cc1516.git diff --git a/src/parse.icl b/src/parse.icl index d04b0f9..f57c5e5 100644 --- a/src/parse.icl +++ b/src/parse.icl @@ -3,15 +3,16 @@ implementation module parse import StdString import StdTuple import StdList -from StdFunc import const +from StdFunc import const, o import Data.Either import Data.Functor import Data.Maybe import Control.Monad import Control.Applicative import Data.Func -from Data.List import intercalate, replicate +from Data.List import intercalate, replicate, instance Functor [] from Text import class Text(concat), instance Text String +import GenPrint import yard import lex @@ -23,46 +24,49 @@ parser (Right r) = case runParser parseProgram r of (Left e, _) = Left $ toString e parseProgram :: Parser Token AST -parseProgram = many parseVarDecl - >>= \vd->some parseFunDecl - >>= \fd->pure $ AST vd fd +parseProgram = liftM2 AST (many parseVarDecl) (some parseFunDecl) parseFunDecl :: Parser Token FunDecl -parseFunDecl = parseIdent <* satTok BraceOpenToken - >>= \ident->parseSepList CommaToken parseIdent <* satTok BraceCloseToken - >>= \args->parseFunType <* satTok CBraceOpenToken - >>= \funtype->many parseVarDecl - >>= \vardecls->many parseStmt - <* satTok CBraceCloseToken - >>= \stmts->pure $ FunDecl ident args funtype vardecls stmts +parseFunDecl = liftM5 FunDecl + (parseIdent <* satTok BraceOpenToken) + (parseSepList CommaToken parseIdent <* satTok BraceCloseToken) + (parseFunType <* satTok CBraceOpenToken) + (many parseVarDecl) + (many parseStmt <* satTok CBraceCloseToken) parseStmt :: Parser Token Stmt -parseStmt = parseIfStmt <|> - parseWhileStmt <|> - (parseAssStmt <* satTok SColonToken) <|> - (parseReturnStmt <* satTok SColonToken) <|> - (parseFunCall <* satTok SColonToken >>= \fc->pure $ FunStmt fc) +parseStmt = parseIfStmt <|> parseWhileStmt <|> + parseSColon parseAssStmt <|> parseSColon parseReturnStmt <|> + (liftM FunStmt (parseSColon parseFunCall)) where + parseSColon :: (Parser Token a) -> Parser Token a + parseSColon p = p <* satTok SColonToken + parseReturnStmt :: Parser Token Stmt - parseReturnStmt = satTok ReturnToken - *> optional parseExpr >>= \me->pure $ ReturnStmt me + parseReturnStmt = + satTok ReturnToken *> liftM ReturnStmt (optional parseExpr) parseAssStmt :: Parser Token Stmt - parseAssStmt = parseVarDef <* satTok AssignmentToken - >>= \var-> parseExpr >>= \expr->pure $ AssStmt var expr + parseAssStmt = + liftM2 AssStmt (parseVarDef <* satTok AssignmentToken) parseExpr parseIfStmt :: Parser Token Stmt - parseIfStmt = satTok IfToken - *> parseBBraces parseExpr - >>= \pred->parseBCBraces (many parseStmt) - >>= \thens->optional ( - satTok ElseToken *> parseBCBraces (many parseStmt) - )>>= \elses->pure $ IfStmt pred thens (fromMaybe [] elses) + parseIfStmt = liftM3 IfStmt + (satTok IfToken *> parseBBraces parseExpr) + (parseBlock <|> parseOneLine) + (liftM (fromMaybe []) + (optional (satTok ElseToken *> (parseBlock<|> parseOneLine)))) parseWhileStmt :: Parser Token Stmt - parseWhileStmt = satTok WhileToken *> parseBBraces parseExpr - >>= \pred->parseBCBraces (many parseStmt) - >>= \body->pure $ WhileStmt pred body + parseWhileStmt = satTok WhileToken *> + liftM2 WhileStmt (parseBBraces parseExpr) parseBlock + + parseBlock :: Parser Token [Stmt] + parseBlock = parseBCBraces (many parseStmt) + + parseOneLine :: Parser Token [Stmt] + //first pure makes singleton list from the statement + parseOneLine = liftM pure parseStmt parseBBraces :: (Parser Token a) -> Parser Token a parseBBraces p = satTok BraceOpenToken *> p <* satTok BraceCloseToken @@ -70,39 +74,39 @@ parseBBraces p = satTok BraceOpenToken *> p <* satTok BraceCloseToken parseBCBraces :: (Parser Token a) -> Parser Token a parseBCBraces p = satTok CBraceOpenToken *> p <* satTok CBraceCloseToken +parseBSqBraces :: (Parser Token a) -> Parser Token a +parseBSqBraces p = satTok SquareOpenToken *> p <* satTok SquareCloseToken + parseFunType :: Parser Token FunType -parseFunType = satTok DoubleColonToken *> - (parseInOutType <|> (parseVoidOrType >>= \t->pure $ FunType [] t)) +parseFunType = satTok DoubleColonToken *> + (parseInOutType <|> (liftM (FunType []) parseVoidOrType)) where parseInOutType :: Parser Token FunType - parseInOutType = some parseType <* satTok ArrowToken - >>= \intypes-> parseVoidOrType - >>= \outtypes->pure $ FunType intypes outtypes + parseInOutType = liftM2 FunType + (some parseType <* satTok ArrowToken) parseVoidOrType parseVoidOrType :: Parser Token (Maybe Type) parseVoidOrType = (satTok VoidToken *> pure Nothing) <|> - (parseType >>= \type->pure $ Just type) + (liftM Just parseType) parseVarDecl :: Parser Token VarDecl -parseVarDecl = +parseVarDecl = liftM3 VarDecl (parseType <|> trans1 VarToken VarType ) - >>= \t->parseIdent <* satTok AssignmentToken - >>= \i->parseExpr <* satTok SColonToken - >>= \e->pure $ VarDecl i t e + (parseIdent <* satTok AssignmentToken) + (parseExpr <* satTok SColonToken) parseType :: Parser Token Type parseType = trans1 IntTypeToken IntType <|> trans1 CharTypeToken CharType <|> trans1 BoolTypeToken BoolType <|> - (satTok SquareOpenToken *> parseType <* satTok SquareCloseToken - >>= \t.pure $ ListType t) <|> - (satTok BraceOpenToken *> parseType <* satTok CommaToken - >>= \t1->parseType <* satTok BraceCloseToken - >>= \t2->pure $ TupleType t1 t2) <|> - (parseIdent >>= \e.pure $ IdType e) <|> - empty + (liftM ListType (parseBSqBraces parseType)) <|> + (liftM2 TupleType + (satTok BraceOpenToken *> parseType <* satTok CommaToken) + (parseType <* satTok BraceCloseToken)) <|> + (liftM IdType parseIdent) +//TODO hieronder omzetten naar liftm notatie parseExpr :: Parser Token Expr parseExpr = //Operators in order of binding strength parseOpR (trans1 ColonToken BiCons) $ @@ -169,8 +173,15 @@ trans2 t f = satTok t >>= \(_, r).pure (f r) trans1 :: TokenValue a -> Parser Token a trans1 t r = trans2 t $ const r +derive gPrint TokenValue +derive gEq TokenValue satTok :: TokenValue -> Parser Token Token -satTok t = satisfy ((===) t) +satTok t = top >>= \tok=:(pos, tv) -> if (eq t tok) (return tok) (fail (printToString tv, pos)) + where + eq (IdentToken _) (_, IdentToken _) = True + eq (NumberToken _) (_, NumberToken _) = True + eq (CharToken _) (_, CharToken _) = True + eq x (_, y) = gEq {|*|} x y parseSepList :: TokenValue (Parser Token a) -> Parser Token [a] parseSepList sep p = @@ -194,33 +205,38 @@ instance print FunDecl where print (FunDecl i as t vs ss) = ["\n", i, " (":printersperse "," as] ++ [") :: ":print t] ++ - ["{\n\t":printersperse "\n\t" vs] ++ - ["\n":printStatements ss 1] ++ ["\n}"] + ["{":printersperse "\n\t" vs] ++ + ["\n":printStatements ss 1] ++ ["}"] printStatements :: [Stmt] Int -> [String] printStatements [] i = [] printStatements [s:ss] i = (case s of - (IfStmt b thens elses) = indent i ["if (":print b] ++ ["){\n"] ++ - printStatements thens (i+1) ++ - indent i ["} else {\n":printStatements elses (i+1)] ++ - indent i ["}\n"] + (IfStmt b thens elses) = indent i ["if (":print b] ++ [")"] ++ + printCodeBlock thens i ++ + indent i ["else ":printCodeBlock elses i] ++ ["\n"] (WhileStmt b dos) = indent i ["while (":print b] ++ - ["){\n":printStatements dos (i+1)] ++ indent i ["}\n"] + [")":printCodeBlock dos i] (AssStmt vardef val) = indent i $ print vardef ++ ["=":print val] ++ [";\n"] (FunStmt fc) = indent i $ print fc ++ [";\n"] (ReturnStmt me) = indent i ["return ":maybe [""] print me] ++ [";\n"] ) ++ printStatements ss i + where + printCodeBlock :: [Stmt] Int -> [String] + printCodeBlock [] _ = ["{}"] + printCodeBlock [x] i = ["\n":printStatements [x] (i+1)] + printCodeBlock x i = + ["{\n":printStatements x (i+1)] ++ indent i ["}\n"] -indent :: Int [String] -> [String] -indent i rest = replicate i "\t" ++ rest + indent :: Int [String] -> [String] + indent i rest = replicate i "\t" ++ rest instance print FunType where print (FunType at rt) = printersperse " " at ++ [if (isEmpty at) "" "->":maybe ["Void"] print rt] instance print VarDecl where - print (VarDecl i t e) = print t ++ [" ":i:"=":print e] ++ [";"] + print (VarDecl t i e) = print t ++ [" ":i:"=":print e] ++ [";"] instance print Type where print (TupleType t1 t2) = ["(":print t1] ++ [",":print t2] ++ [")"]