eb0e0f8533b33286bd6fbe8cd682577e52edb26e
[ghc-hetmet.git] / ghc / compiler / javaGen / PrintJava.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
3 %
4 \section{Generate Java}
5
6 \begin{code}
7 module PrintJava( compilationUnit ) where
8
9 import Java
10 import Outputable
11 import Char( toLower )
12 \end{code}
13
14 \begin{code}
15 indent :: SDoc -> SDoc
16 indent = nest 2
17 \end{code}
18   
19 %************************************************************************
20 %*                                                                      *
21 \subsection{Pretty printer}
22 %*                                                                      *
23 %************************************************************************
24
25 \begin{code}
26 compilationUnit :: CompilationUnit -> SDoc
27 compilationUnit (Package n ds) = package n (decls ds)
28
29 package = \n -> \ds ->
30   text "package" <+> name n <> text ";"
31   $$
32   ds
33   
34 decls []     = empty
35 decls (d:ds) = decl d $$ decls ds
36     
37 decl = \d ->
38   case d of
39     { Import n -> importDecl (name n)
40     ; Field mfs t n e -> field (modifiers mfs) (typ t) (name n) e  
41     ; Constructor mfs n as ss -> constructor (modifiers mfs) (name n) (parameters as) (statements ss)
42     ; Method mfs t n as ss -> method (modifiers mfs) (typ t) (name n) (parameters as) (statements ss)
43     ; Comment s -> comment s
44     ; Interface mfs n is ms -> interface (modifiers mfs) (name n) (extends is) (decls ms)
45     ; Class mfs n x is ms -> clazz (modifiers mfs) (name n) (extends x) (implements is) (decls ms)
46     }
47
48 importDecl n = text "import" <+> n <> text ";"
49   
50 field = \mfs -> \t -> \n -> \e ->
51   case e of
52     { Nothing -> mfs <+> t <+> n <> text ";" 
53     ; Just e  -> lay [mfs <+> t <+> n <+> text "=", indent (expr e <> text ";")]
54              where
55                 lay | isSimple e = hsep
56                     | otherwise  = sep
57     }
58
59 constructor = \mfs -> \n -> \as -> \ss ->
60   mfs <+> n <+> parens (hsep (punctuate comma as)) <+> text "{"
61   $$ indent ss 
62   $$ text "}"
63
64 method = \mfs -> \t -> \n -> \as -> \ss -> 
65   mfs <+> t <+> n <+> parens (hsep (punctuate comma as)) <+> text "{" 
66   $$ indent ss 
67   $$ text "}"
68
69 comment = \ss ->
70   text "/**"
71   $$ indent (vcat [ text s | s <- ss])
72   $$ text "**/"
73
74 interface = \mfs -> \n -> \xs -> \ms -> 
75   mfs <+> n <+> xs <+> text "{"
76   $$ indent ms
77   $$ text "}"
78      
79 clazz = \mfs -> \n -> \x -> \is -> \ms ->
80   mfs <+> text "class" <+> n <+> x <+> is <+> text "{" 
81   $$ indent ms 
82   $$ text "}"
83
84 staticblock = \ss ->
85   text "static" <+> text "{"
86   $$ indent ss
87   $$ text "}"
88     
89 modifiers mfs = hsep (map modifier mfs)
90     
91 modifier mf = text $ map toLower (show mf)
92   
93 extends [] = empty
94 extends xs = text "extends" <+> hsep (punctuate comma (map name xs))
95
96 implements [] = empty
97 implements xs = text "implements" <+> hsep (punctuate comma (map name xs))
98
99 name ns = hcat (punctuate dot (map text ns))
100
101 parameters as = map parameter as
102
103 parameter (Parameter mfs t n) = modifiers mfs <+> typ t <+> name n
104
105 typ (Type n)  = name n
106 typ (Array t) = typ t <> text "[]"
107
108 statements ss = vcat (map statement ss)
109   
110 statement = \s ->
111   case s of
112     { Skip -> skip
113     ; Return e -> returnStat (expr e)
114     ; Block ss -> vcat [statement s | s <- ss]
115     ; ExprStatement e -> exprStatement (expr e)
116     ; Declaration d -> declStatement (decl d)
117     ; IfThenElse ecs s -> ifthenelse [ (expr e, statement s) | (e,s) <- ecs ] (maybe Nothing (Just .statement) s)
118     ; Switch e as d -> switch (expr e) (arms as) (deflt d)
119     } 
120
121 skip = empty
122   
123 returnStat e = sep [text "return", indent e <> semi]
124
125 exprStatement e = e <> semi
126
127 declStatement d = d
128
129 ifthenelse ((e,s):ecs) ms = sep [text "if", 
130                                 indent (parens e) <+> text "{", 
131                                 indent s, 
132                               thenelse ecs ms]
133
134 thenelse ((e,s):ecs) ms = sep [ text "} else if", 
135                                 indent (parens e) <+> text "{", 
136                                 indent s,
137                                 thenelse ecs ms]
138
139 thenelse [] Nothing  = text "}"
140 thenelse [] (Just s) = sep [text "} else {", indent s, text "}"]
141     
142 switch = \e -> \as -> \d ->
143   text "switch" <+> parens e <+> text "{" 
144   $$ indent (as $$ d)
145   $$ text "}"
146   
147 deflt Nothing   = empty
148 deflt (Just ss) = text "default:" $$ indent (statements ss)  
149     
150 arms [] = empty
151 arms ((e,ss):as) = text "case" <+> expr e <> colon
152                    $$ indent (statements ss)
153                    $$ arms as
154
155 maybeExpr Nothing  = Nothing
156 maybeExpr (Just e) = Just (expr e)
157            
158 expr = \e ->
159  case e of
160    { Var n -> name n
161    ; Literal l -> literal l
162    ; Cast t e -> cast (typ t) e
163    ; Access e n -> expr e <> text "." <> name n
164    ; Assign l r -> assign (expr l) r
165    ; New n es ds -> new (name n) es (maybeClass ds)
166    ; Call e n es -> call (expr e) (name n) es
167    ; Op e1 o e2 -> op e1 o e2
168    ; InstanceOf e t -> expr e <+> text "instanceof" <+> typ t
169    ; NewArray n es -> newArray (name n) es
170    }
171    
172 op = \e1 -> \o -> \e2 ->
173   ( if isSimple e1 
174     then expr e1 
175     else parens (expr e1)
176   ) 
177   <+> 
178   text o
179   <+>
180   ( if isSimple e2
181     then expr e2 
182     else parens (expr e2)
183   )
184   
185 assign = \l -> \r ->
186   if isSimple r
187   then l <+> text "=" <+> (expr r)
188   else l <+> text "=" $$ indent (expr r)
189
190 cast = \t -> \e ->
191   if isSimple e
192   then parens (parens t <> expr e)
193   else parens (parens t $$ indent (expr e))
194
195 new n [] (Just ds) = sep [text "new" <+> n <+> text "()" <+> text "{",
196                              indent ds,
197                              text "}"]
198 new n es Nothing = text "new" <+> n <> parens (hsep (punctuate comma (map expr es)))
199
200 newArray n es = text "new" <+> n <> text "[]" <+> braces (hsep (punctuate comma (map expr es)))
201       
202 call e n es = e <> dot <> n <> parens (hsep (punctuate comma (map expr es)))
203
204 literal = \l ->
205   case l of
206     { IntLit i    -> text (show i)
207     ; UIntLit i   -> text (show i)
208     ; CharLit c   -> text (show c)
209     ; UCharLit c  -> text (show c)
210     ; StringLit s -> text (show s)
211     }
212
213 maybeClass Nothing   = Nothing
214 maybeClass (Just ds) = Just (decls ds)
215 \end{code}