+
+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] ++
+ [") :: ":print t] ++
+ ["{":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] ++ [")"] ++
+ 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 [] _ = ["{}"]
+ printCodeBlock [x] i = ["\n":printStatements [x] (i+1)]
+ printCodeBlock x i =
+ ["{\n":printStatements x (i+1)] ++ indent i ["}"]
+
+ 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] ++ [";"]
+
+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] ++ [")"]