import StdList
from StdFunc import const, o
import Data.Either
-import Data.Functor
import Data.Maybe
import Control.Monad
import Control.Applicative
parser (Right r) = fst $ runParser parseProgram r
parseProgram :: Parser Token AST
-parseProgram = liftM2 AST (many parseVarDecl) (some parseFunDecl)
+parseProgram = liftM2 AST (many parseVarDecl) (some parseFunDecl)
parseFunDecl :: Parser Token FunDecl
parseFunDecl = liftM5 FunDecl
satTok ReturnToken *> liftM ReturnStmt (optional parseExpr)
parseAssStmt :: Parser Token Stmt
- parseAssStmt =
+ parseAssStmt =
liftM2 AssStmt (parseVarDef <* satTok AssignmentToken) parseExpr
parseIfStmt :: Parser Token Stmt
parseIfStmt = liftM3 IfStmt
(satTok IfToken *> parseBBraces parseExpr)
(parseBlock <|> parseOneLine)
- (liftM (fromMaybe [])
+ (liftM (fromMaybe [])
(optional (satTok ElseToken *> (parseBlock<|> parseOneLine))))
-
parseWhileStmt :: Parser Token Stmt
- parseWhileStmt = satTok WhileToken *>
+ parseWhileStmt = satTok WhileToken *>
liftM2 WhileStmt (parseBBraces parseExpr) parseBlock
parseBlock :: Parser Token [Stmt]
parseOneLine = liftM pure parseStmt
parseFunType :: Parser Token FunType
-parseFunType = satTok DoubleColonToken *>
+parseFunType = satTok DoubleColonToken *>
(parseInOutType <|> (liftM (FunType []) parseVoidOrType))
where
parseInOutType :: Parser Token FunType
- parseInOutType = liftM2 FunType
+ parseInOutType = liftM2 FunType
(some parseType <* satTok ArrowToken) parseVoidOrType
parseVoidOrType :: Parser Token (Maybe Type)
- parseVoidOrType = (satTok VoidToken *> pure Nothing) <|>
+ parseVoidOrType = (satTok VoidToken *> pure Nothing) <|>
(liftM Just parseType) <|> pure Nothing
parseVarDecl :: Parser Token VarDecl
(parseExpr <* satTok SColonToken)
parseType :: Parser Token Type
-parseType =
+parseType =
trans1 IntTypeToken IntType <|>
trans1 CharTypeToken CharType <|>
trans1 BoolTypeToken BoolType <|>
) >>= \moe->foldM (\e->(\(op,e2)->pure $ Op2Expr e op e2)) e1 moe
parseBasicExpr :: Parser Token Expr
- parseBasicExpr =
+ parseBasicExpr =
(liftM TupleExpr (parseTuple parseExpr)) <|>
(liftM FunExpr parseFunCall) <|>
parseBBraces parseExpr <|>
parseVarDef :: Parser Token VarDef
parseVarDef = liftM2 VarDef
- parseIdent
+ parseIdent
(many (satTok DotToken *> (
(parseIdent >>= (\i.if (i == "hd") (pure FieldHd) empty)) <|>
(parseIdent >>= \i.if (i == "tl") (pure FieldTl) empty) <|>
parseBSqBraces p = satTok SquareOpenToken *> p <* satTok SquareCloseToken
parseTuple :: (Parser Token a) -> Parser Token (a, a)
-parseTuple p = satTok BraceOpenToken *>
- (liftM2 (\a->(\b->(a,b))) (p <* satTok CommaToken) p)
+parseTuple p = satTok BraceOpenToken *>
+ (liftM2 (\a->(\b->(a,b))) (p <* satTok CommaToken) p)
<* satTok BraceCloseToken
trans2 :: TokenValue (TokenValue -> a) -> Parser Token a
derive gPrint TokenValue
derive gEq TokenValue
satTok :: TokenValue -> Parser Token Token
-satTok t = top >>= \tok=:{line,column,token} -> if (eq t token)
- (return tok) (fail <?> PositionalError line column ("ParseError: Unexpected token: " +++ printToString token))
+satTok t = top >>= \tok=:{line,column,token} -> if (eq t token)
+ (pure tok) (fail <?> PositionalError line column
+ ("ParseError: Unexpected token: " +++ printToString token))
where
eq (IdentToken _) (IdentToken _) = True
eq (NumberToken _) (NumberToken _) = True
eq x y = gEq {|*|} x y
parseSepList :: TokenValue (Parser Token a) -> Parser Token [a]
-parseSepList sep p =
+parseSepList sep p =
(liftM2 (\es->(\e->reverse [e:es])) (some (p <* satTok sep)) p) <|>
(liftM pure p) <|> pure empty
printStatements [] i = []
printStatements [s:ss] i = (case s of
(IfStmt b thens elses) = indent i ["if (":print b] ++ [")"] ++
- printCodeBlock thens i ++
+ printCodeBlock thens i ++
indent i ["else ":printCodeBlock elses i] ++ ["\n"]
- (WhileStmt b dos) = indent i ["while (":print b] ++
+ (WhileStmt b dos) = indent i ["while (":print b] ++
[")":printCodeBlock dos i]
(AssStmt vardef val) =
indent i $ print vardef ++ ["=":print val] ++ [";\n"]
instance print String where
print s = [s]
-instance print FieldSelector where
+instance print FieldSelector where
print FieldHd = print "hd"
print FieldTl = print "tl"
print FieldSnd = print "snd"