X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=parse.icl;h=8c4a25e5bea6acf2927026f1b68603a611d47884;hb=e117090bd4eac114541e34d85463d271eded0865;hp=120935f13bbd515161f690b2031d553ce0eeb6ba;hpb=6703509b4b132cee0f4c91dc23795d3d4ab131e8;p=cc1516.git diff --git a/parse.icl b/parse.icl index 120935f..8c4a25e 100644 --- a/parse.icl +++ b/parse.icl @@ -6,6 +6,7 @@ import StdList from StdFunc import const, o import Data.Either import Data.Maybe +import Data.Functor import Control.Monad import Control.Applicative import Data.Func @@ -15,18 +16,20 @@ import GenPrint import yard import lex +import AST parser :: LexerOutput -> ParserOutput parser (Left e) = Left e parser (Right r) = fst $ runParser parseProgram r parseProgram :: Parser Token AST -parseProgram = liftM2 AST (many parseVarDecl) (some parseFunDecl) +parseProgram = AST <$> (many parseVarDecl) <*> (some parseFunDecl) parseFunDecl :: Parser Token FunDecl -parseFunDecl = liftM5 FunDecl - (parseIdent <* satTok BraceOpenToken) - (parseSepList CommaToken parseIdent <* satTok BraceCloseToken) +parseFunDecl = liftM6 FunDecl + (peekPos) + (parseIdent) + (satTok BraceOpenToken *> parseSepList CommaToken parseIdent <* satTok BraceCloseToken) (optional parseFunType <* satTok CBraceOpenToken) (many parseVarDecl) (many parseStmt <* satTok CBraceCloseToken) @@ -45,7 +48,7 @@ parseStmt = parseIfStmt <|> parseWhileStmt <|> parseAssStmt :: Parser Token Stmt parseAssStmt = - liftM2 AssStmt (parseVarDef <* satTok AssignmentToken) parseExpr + AssStmt <$> (parseVarDef <* satTok AssignmentToken) <*> parseExpr parseIfStmt :: Parser Token Stmt parseIfStmt = liftM3 IfStmt @@ -56,29 +59,29 @@ parseStmt = parseIfStmt <|> parseWhileStmt <|> parseWhileStmt :: Parser Token Stmt parseWhileStmt = satTok WhileToken *> - liftM2 WhileStmt (parseBBraces parseExpr) parseBlock + (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 + parseOneLine = pure <$> parseStmt parseFunType :: Parser Token FunType parseFunType = satTok DoubleColonToken *> - (parseInOutType <|> (liftM (FunType []) parseVoidOrType)) + (parseInOutType <|> ((FunType []) <$> parseVoidOrType)) where parseInOutType :: Parser Token FunType - parseInOutType = liftM2 FunType - (some parseType <* satTok ArrowToken) parseVoidOrType + parseInOutType = FunType <$> (some parseType <* satTok ArrowToken) <*> parseVoidOrType parseVoidOrType :: Parser Token (Maybe Type) parseVoidOrType = (satTok VoidToken *> pure Nothing) <|> - (liftM Just parseType) <|> pure Nothing + (Just <$> parseType) <|> pure Nothing parseVarDecl :: Parser Token VarDecl -parseVarDecl = liftM3 VarDecl +parseVarDecl = liftM4 VarDecl + peekPos (parseType <|> trans1 VarToken VarType ) (parseIdent <* satTok AssignmentToken) (parseExpr <* satTok SColonToken) @@ -88,9 +91,9 @@ parseType = trans1 IntTypeToken IntType <|> trans1 CharTypeToken CharType <|> trans1 BoolTypeToken BoolType <|> - (liftM ListType (parseBSqBraces parseType)) <|> - (liftM TupleType (parseTuple parseType)) <|> - (liftM IdType parseIdent) + (ListType <$> (parseBSqBraces parseType)) <|> + (TupleType <$> (parseTuple parseType)) <|> + (IdType <$> parseIdent) parseExpr :: Parser Token Expr parseExpr = //Operators in order of binding strength @@ -110,31 +113,30 @@ parseExpr = //Operators in order of binding strength trans1 PercentToken BiMod) $ parseBasicExpr where parseOpR :: (Parser Token Op2) (Parser Token Expr) -> Parser Token Expr - parseOpR ops prev = prev >>= \e1->optional ( + parseOpR ops prev = peekPos >>= \pos-> prev >>= \e1->optional ( ops >>= \op->parseOpR ops prev >>= \e->pure (op, e) - ) >>= \moe->pure $ maybe e1 (\(op,e2)->Op2Expr e1 op e2) moe + ) >>= \moe->pure $ maybe e1 (\(op,e2)->Op2Expr pos e1 op e2) moe parseOpL :: (Parser Token Op2) (Parser Token Expr) -> Parser Token Expr - parseOpL ops prev = prev >>= \e1->many ( + parseOpL ops prev = peekPos >>= \pos-> prev >>= \e1->many ( ops >>= \op->prev >>= \e->pure (op, e) - ) >>= \moe->foldM (\e->(\(op,e2)->pure $ Op2Expr e op e2)) e1 moe + ) >>= \moe->foldM (\e->(\(op,e2)->pure $ Op2Expr pos e op e2)) e1 moe parseBasicExpr :: Parser Token Expr - parseBasicExpr = - (liftM TupleExpr (parseTuple parseExpr)) <|> - (liftM FunExpr parseFunCall) <|> + parseBasicExpr = peekPos >>= \pos -> + (TupleExpr pos <$> (parseTuple parseExpr)) <|> + (FunExpr pos <$> parseFunCall) <|> parseBBraces parseExpr <|> - trans1 EmptyListToken EmptyListExpr <|> - trans1 TrueToken (BoolExpr True) <|> - trans1 FalseToken (BoolExpr False) <|> - trans2 (NumberToken zero) (\(NumberToken i)->IntExpr i) <|> - trans2 (CharToken zero) (\(CharToken c)->CharExpr c) <|> - (liftM2 Op1Expr parseOp1 parseExpr) <|> - (liftM VarExpr parseVarDef) + trans1 EmptyListToken (EmptyListExpr pos) <|> + trans1 TrueToken (BoolExpr pos True) <|> + trans1 FalseToken (BoolExpr pos False) <|> + trans2 (NumberToken zero) (\(NumberToken i)->IntExpr pos i) <|> + trans2 (CharToken zero) (\(CharToken c)->CharExpr pos c) <|> + (Op1Expr pos <$> parseOp1 <*> parseExpr) <|> + (VarExpr pos <$> parseVarDef) parseFunCall :: Parser Token FunCall -parseFunCall = liftM2 FunCall - parseIdent (parseBBraces $ parseSepList CommaToken parseExpr) +parseFunCall = FunCall <$> parseIdent <*> (parseBBraces $ parseSepList CommaToken parseExpr) parseVarDef :: Parser Token VarDef parseVarDef = liftM2 VarDef @@ -162,17 +164,23 @@ parseTuple p = satTok BraceOpenToken *> (liftM2 (\a->(\b->(a,b))) (p <* satTok CommaToken) p) <* satTok BraceCloseToken +trans :: TokenValue (TokenValue -> a) -> Parser Token (Pos, a) +trans t f = (\(pos,token)->(pos, f token)) <$> satTok t + trans2 :: TokenValue (TokenValue -> a) -> Parser Token a -trans2 t f = liftM (\{token}->f token) $ satTok t +trans2 t f = snd <$> trans t f trans1 :: TokenValue a -> Parser Token a trans1 t r = trans2 t $ const r +peekPos :: Parser Token Pos +peekPos = fst <$> peek + derive gPrint TokenValue derive gEq TokenValue satTok :: TokenValue -> Parser Token Token -satTok t = top >>= \tok=:{line,column,token} -> if (eq t token) - (pure tok) (fail PositionalError line column +satTok t = top >>= \tok=:({line,col},token) -> if (eq t token) + (pure tok) (fail PositionalError line col ("ParseError: Unexpected token: " +++ printToString token)) where eq (IdentToken _) (IdentToken _) = True @@ -188,95 +196,5 @@ parseSepList sep p = parseIdent :: Parser Token String parseIdent = trans2 (IdentToken "") (\(IdentToken e).toString e) -instance toString AST where - toString (AST v f) = concat ( - ["\n":printersperse "\n" v] ++ - ["\n":printersperse "\n" f]) - -class print a :: a -> [String] - -printersperse :: String [a] -> [String] | print a -printersperse i j = intercalate [i] (map print j) - -instance print FunDecl where - print (FunDecl i as t vs ss) = - ["\n", i, " (":printersperse "," as] ++ - [")"] ++ maybe [] (\tt->[" :: ":print tt]) t ++ - ["{\n\t":printersperse "\n\t" vs] ++ - ["\n":printStatements ss 1] ++ ["}\n"] - -printStatements :: [Stmt] Int -> [String] -printStatements [] i = [] -printStatements [s:ss] i = (case s of - (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] ++ - [")":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 [] _ = ["{}\n"] - 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 - -instance print FunType where - print (FunType at rt) = printersperse " " at ++ - [if (isEmpty at) "" "->":maybe ["Void"] print rt] - -instance print VarDecl where - print (VarDecl t i e) = print t ++ [" ":i:"=":print e] ++ [";"] - -instance print Type where - print (TupleType (t1, t2)) = ["(":print t1] ++ [",":print t2] ++ [")"] - print (ListType t) = ["[":print t] ++ ["]"] - print (IdType s) = print s - print IntType = print "Int" - print BoolType = print "Bool" - print CharType = print "Char" - print VarType = print "var" - -instance print String where - print s = [s] - -instance print FieldSelector where - print FieldHd = print "hd" - print FieldTl = print "tl" - print FieldSnd = print "snd" - print FieldFst = print "fst" - -instance print VarDef where - print (VarDef i fs) = printersperse "." [i:flatten $ map print fs] - -instance print FunCall where - print (FunCall i args) = [i,"(":printersperse "," args] ++ [")"] - -instance print Expr where - print (VarExpr vd) = print vd - print (Op2Expr e1 o e2) = ["(":print e1] ++ [" ",case o of - BiPlus = "+"; BiMinus = "-"; BiTimes = "*"; BiDivide = "/" - BiMod = "%"; BiEquals = "="; BiLesser = "<"; BiGreater = ">" - BiLesserEq = "<="; BiGreaterEq = ">="; BiUnEqual = "!="; - BiAnd = "&&"; BiOr = "||"; BiCons = ":" - ," ":print e2] ++ [")"] - print (Op1Expr o e) = ["(",case o of - UnNegation = "!"; UnMinus = "-" - :print e] ++ [")"] - print (IntExpr i) = [toString i] - print (CharExpr c) = ["\'", case c of - '\b' = "\\b"; '\f' = "\\f"; '\n' = "\\n" - '\r' = "\\r"; '\t' = "\\t"; '\v' = "\\v" - c = if (c == toChar 7) "\\a" (toString c) - ,"\'"] - print (BoolExpr b) = [toString b] - print (FunExpr fc) = print fc - print EmptyListExpr = ["[]"] - print (TupleExpr (e1, e2)) = ["(":print e1] ++ [",":print e2] ++ [")"] +//liftM only goes to liftM5 +liftM6 f m1 m2 m3 m4 m5 m6 = f <$> m1 <*> m2 <*> m3 <*> m4 <*> m5 <*> m6 \ No newline at end of file