Parser aangepast naar leipe arrowtypes zonder arity
[cc1516.git] / AST.icl
1 implementation module AST
2
3 import StdEnv
4
5 from Data.List import map, intercalate, replicate, flatten, isEmpty
6 from Data.Func import $
7 from Text import class Text(concat), instance Text String
8 from Data.Maybe import :: Maybe, maybe
9
10 instance toString AST where
11 toString (AST v f) = concat (
12 ["\n":printersperse "\n" v] ++
13 ["\n":printersperse "\n" f])
14
15 class print a :: a -> [String]
16
17 printersperse :: String [a] -> [String] | print a
18 printersperse i j = intercalate [i] (map print j)
19
20 instance print FunDecl where
21 print (FunDecl _ i as t vs ss) =
22 ["\n", i, " (":printersperse "," as] ++
23 [")"] ++ maybe [] (\tt->[" :: ":print tt]) t ++
24 ["{\n\t":printersperse "\n\t" vs] ++
25 ["\n":printStatements ss 1] ++ ["}\n"]
26
27 printStatements :: [Stmt] Int -> [String]
28 printStatements [] i = []
29 printStatements [s:ss] i = (case s of
30 (IfStmt b thens elses) = indent i ["if (":print b] ++ [")"] ++
31 printCodeBlock thens i ++
32 indent i ["else ":printCodeBlock elses i] ++ ["\n"]
33 (WhileStmt b dos) = indent i ["while (":print b] ++
34 [")":printCodeBlock dos i]
35 (AssStmt vardef val) =
36 indent i $ print vardef ++ ["=":print val] ++ [";\n"]
37 (FunStmt fc) = indent i $ print fc ++ [";\n"]
38 (ReturnStmt me) = indent i ["return ":maybe [""] print me] ++ [";\n"]
39 ) ++ printStatements ss i
40 where
41 printCodeBlock :: [Stmt] Int -> [String]
42 printCodeBlock [] _ = ["{}\n"]
43 printCodeBlock [x] i = ["\n":printStatements [x] (i+1)]
44 printCodeBlock x i =
45 ["{\n":printStatements x (i+1)] ++ indent i ["}\n"]
46
47 indent :: Int [String] -> [String]
48 indent i rest = replicate i "\t" ++ rest
49
50 instance print VarDecl where
51 print (VarDecl _ t i e) = print t ++ [" ":i:"=":print e] ++ [";"]
52
53 instance print Type where
54 print (TupleType (t1, t2)) = ["(":print t1] ++ [",":print t2] ++ [")"]
55 print (ListType t) = ["[":print t] ++ ["]"]
56 print (IdType s) = print s
57 print IntType = print "Int"
58 print BoolType = print "Bool"
59 print CharType = print "Char"
60 print VarType = print "var"
61 print VoidType = print "Void"
62 print (t1 ->> t2) = print t1 ++ [" -> ":print t2]
63
64 instance print String where
65 print s = [s]
66
67 instance print FieldSelector where
68 print FieldHd = print "hd"
69 print FieldTl = print "tl"
70 print FieldSnd = print "snd"
71 print FieldFst = print "fst"
72
73 instance print VarDef where
74 print (VarDef i fs) = printersperse "." [i:flatten $ map print fs]
75
76 instance print FunCall where
77 print (FunCall i args) = [i,"(":printersperse "," args] ++ [")"]
78
79 instance print Expr where
80 print (VarExpr _ vd) = print vd
81 print (Op2Expr _ e1 o e2) = ["(":print e1] ++ [" ",case o of
82 BiPlus = "+"; BiMinus = "-"; BiTimes = "*"; BiDivide = "/"
83 BiMod = "%"; BiEquals = "=="; BiLesser = "<"; BiGreater = ">"
84 BiLesserEq = "<="; BiGreaterEq = ">="; BiUnEqual = "!=";
85 BiAnd = "&&"; BiOr = "||"; BiCons = ":"
86 ," ":print e2] ++ [")"]
87 print (Op1Expr _ o e) = ["(",case o of
88 UnNegation = "!"; UnMinus = "-"
89 :print e] ++ [")"]
90 print (IntExpr _ i) = [toString i]
91 print (CharExpr _ c) = ["\'", case c of
92 '\b' = "\\b"; '\f' = "\\f"; '\n' = "\\n"
93 '\r' = "\\r"; '\t' = "\\t"; '\v' = "\\v"
94 c = if (c == toChar 7) "\\a" (toString c)
95 ,"\'"]
96 print (BoolExpr _ b) = [toString b]
97 print (FunExpr _ fc) = print fc
98 print (EmptyListExpr _) = ["[]"]
99 print (TupleExpr _ (e1, e2)) = ["(":print e1] ++ [",":print e2] ++ [")"]