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