implementation module AST import StdEnv from Data.List import map, intercalate, replicate, flatten, isEmpty from Data.Func import $ from Text import class Text(concat), instance Text String from Data.Maybe import :: Maybe, maybe import GenEq instance toString Pos where toString {line,col} = concat [toString line, ":", toString col, " "] instance toString AST where toString (AST f) = concat ["\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"] instance toString FunDecl where toString fd = concat $ print fd 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 ident args fs) = indent i $ printFunCall ident args fs ++ [";\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 VarDecl where print (VarDecl _ t i e) = maybe ["var"] print t ++ [" ":i:"=":print e] ++ [";"] instance toString VarDecl where toString v = concat (print v) instance toString Type where toString t = concat $ print t 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 VoidType = print "Void" print (FuncType t) = ["(-> ":print t] ++ [")"] print (t1 ->> t2) = ["(":print t1 ++ [" -> ":print t2]] ++ [")"] 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 toString FieldSelector where toString fs = concat $ print fs instance print VarDef where print (VarDef i fs) = printersperse "." [i:printersperse "" fs] instance toString Op2 where toString o = case o of BiPlus = "+"; BiMinus = "-"; BiTimes = "*"; BiDivide = "/" BiMod = "%"; BiEquals = "=="; BiLesser = "<"; BiGreater = ">" BiLesserEq = "<="; BiGreaterEq = ">="; BiUnEqual = "!="; BiAnd = "&&"; BiOr = "||"; BiCons = ":" instance toString Op1 where toString UnNegation = "!" toString UnMinus = "-" instance print Expr where print (VarExpr _ vd) = print vd print (Op2Expr _ e1 o e2) = ["(":print e1] ++ [" ",toString o, " ":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 _ id as fs) = printFunCall id as fs print (EmptyListExpr _) = ["[]"] print (TupleExpr _ (e1, e2)) = ["(":print e1] ++ [",":print e2] ++ [")"] print (LambdaExpr _ args e) = ["\\":args] ++ ["->": print e] instance toString Expr where toString e = concat $ print e printSelectors :: [FieldSelector] -> [String] printSelectors fs = ["."] ++ printersperse "." fs printFunCall :: String [Expr] [FieldSelector] -> [String] printFunCall s args fs = [s, "(":printersperse "," args] ++ [")"] ++ printSelectors fs derive gEq Op2 instance == Op2 where (==) o1 o2 = gEq{|*|} o1 o2 instance zero Pos where zero = {line=0, col=0} derive gEq Op1 instance == Op1 where (==) o1 o2 = gEq{|*|} o1 o2 instance < Op2 where (<) o1 o2 = toString o1 < toString o2 instance < Op1 where (<) o1 o2 = toString o1 < toString o2 derive gEq Type instance == Type where (==) t1 t2 = gEq{|*|} t1 t2