empty
parseExpr :: Parser Token Expr
-parseExpr =
+parseExpr = //Operators in order of binding strength
parseOpRight (trans1 ColonToken BiCons) $
parseOpRight (trans1 PipesToken BiOr) $
parseOpRight (trans1 AmpersandsToken BiAnd) $
parseOpLeft (trans1 StarToken BiTimes <|>
trans1 SlashToken BiDivide <|>
trans1 PercentToken BiMod) $
- parseBasicExpr
+ parseBasicExpr
where
parseOpRight :: (Parser Token Op2) (Parser Token Expr) -> Parser Token Expr
parseOpRight ops prev = prev >>= \e1->optional (
parseBasicExpr :: Parser Token Expr
parseBasicExpr =
- (satTok BraceOpenToken *> parseExpr <* satTok BraceCloseToken) <|>
(satTok BraceOpenToken *> parseExpr <* satTok CommaToken
>>= \e1->parseExpr <* satTok BraceCloseToken
>>= \e2->pure $ TupleExpr e1 e2) <|>
+ (parseIdent <* satTok BraceOpenToken
+ >>= \i->parseSepList CommaToken parseExpr <* satTok BraceCloseToken
+ >>= \es->pure $ FunExpr i es) <|>
+ (satTok BraceOpenToken *> parseExpr <* satTok BraceCloseToken) <|>
trans1 EmptyListToken EmptyListExpr <|>
trans2 TrueToken (const $ BoolExpr True) <|>
trans2 FalseToken (const $ BoolExpr False) <|>
trans2 (NumberToken zero) (\(NumberToken i)->IntExpr i) <|>
trans2 (CharToken zero) (\(CharToken c)->CharExpr c) <|>
(parseOp1 >>= \o->parseExpr >>= \e.pure $ Op1Expr o e) <|>
- (parseIdent <* satTok BraceOpenToken
- >>= \i->parseActArgs <* satTok BraceCloseToken
- >>= \es->pure $ FunExpr i es) <|>
- (parseIdent >>= \i. parseFieldSelector >>= \f.pure $ VarExpr i f)
-
-parseActArgs :: Parser Token [Expr]
-parseActArgs =
- (some (parseExpr <* satTok CommaToken) >>= \es->parseExpr
- >>= \e.pure [e:es]) <|>
- (parseExpr >>= \e->pure [e]) <|>
- empty
+ (parseIdent >>= \i->parseFieldSelector >>= \f.pure $ VarExpr i f)
parseOp1 :: Parser Token Op1
parseOp1 = trans1 DashToken UnMinus <|>
satTok :: TokenValue -> Parser Token Token
satTok t = satisfy ((===) t)
+parseSepList :: TokenValue (Parser Token a) -> Parser Token [a]
+parseSepList sep p =
+ (some (p <* satTok sep) >>= \es->p >>= \e.pure $ reverse [e:es]) <|>
+ (p >>= \e->pure [e]) <|> pure []
+
parseIdent :: Parser Token String
parseIdent = trans2 (IdentToken []) (\(IdentToken e).toString e)
instance toString AST where
- toString (AST v f) = concat (print v ++ ["\n":print f])
+ toString (AST v f) = concat (
+ ["\n":strJoin "\n" (map print v)] ++
+ ["\n":strJoin "\n" (map print f)])
class print a :: a -> [String]
-instance print [a] | print a where
- print [] = ["\n"]
- print [v:vs] = print v ++ ["\n":print vs]
+instance print FunDecl where
+ print (FunDecl i as t vs ss) =
+ ["\n", i, " (":strJoin "," (map (\i->[i]) as)] ++
+ [") :: ":print t] ++
+ ["{\n\t":strJoin "\n\t" (map print vs)] ++
+ ["\n":printStatements ss 1] ++ ["\n}"]
+
+printStatements :: [Stmt] Int -> [String]
+printStatements _ _ = []
+
+//TODO
+instance print FunType where
+ print _ = []
+
+//TODO
+instance print Stmt where
+ print _ = []
instance print VarDecl where
print (VarDecl i t e) = print t ++ [" ":i:"=":print e] ++ [";"]
-instance print FunDecl where
- print _ = ["Function printing not yet implemented"]
instance print Type where
print (TupleType t1 t2) = ["(":print t1] ++ [", ":print t2] ++ [")"]
print CharType = ["Char"]
print VarType = ["var"]
+strJoin :: String [[String]] -> [String]
+strJoin _ [] = []
+strJoin j [s:rest] = s ++ flatten [[j:ss]\\ss<-rest]
+
instance print Expr where
print (VarExpr i Nothing) = [i]
print (VarExpr i (Just mf)) = [i, case mf of
FieldHd = ".hd"; FieldTl = ".tl"
FieldSnd = ".snd"; FieldFst = ".fst"]
- print (Op2Expr e1 o e2) = print e1 ++ [case o of
+ 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
+ :print e2] ++ [")"]
+ print (Op1Expr o e) = ["(",case o of
UnNegation = "!"; UnMinus = "-"
- :print e]
+ :print e] ++ [")"]
print (IntExpr i) = [toString i]
print (CharExpr c) = ["\'", case c of
'\b' = "\\b"; '\f' = "\\f"; '\n' = "\\n"
c = if (c == toChar 7) "\\a" (toString c)
,"\'"]
print (BoolExpr b) = [toString b]
- print (FunExpr i es) = pe ++ flatten [[",":x]\\x<-tl pes]
- where [pe:pes] = map print es
+ print (FunExpr i es) = [i,"(":strJoin "," (map print es)] ++ [")"]
print EmptyListExpr = ["[]"]
print (TupleExpr e1 e2) = ["(":print e1] ++ [",":print e2] ++ [")"]