[project @ 2000-05-24 07:31:44 by andy]
[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 (hcat (punctuate dot (map text 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 ts ss -> method (modifiers mfs) (typ t) (name n) (parameters as) (throws ts) (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 -> \ts -> \ss -> 
65   mfs <+> t <+> n <+> parens (hsep (punctuate comma as)) <+> ts <+> 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 throws [] = empty
100 throws xs = text "throws" <+> hsep (punctuate comma (map name xs))
101
102 name ns = text ns
103
104 parameters as = map parameter as
105
106 parameter (Parameter mfs t n) = modifiers mfs <+> typ t <+> name n
107
108 typ (PrimType s)  = text s
109 typ (Type n)      = hcat (punctuate dot (map text n))
110 typ (ArrayType t) = typ t <> text "[]"
111
112 statements ss = vcat (map statement ss)
113   
114 statement = \s ->
115   case s of
116     { Skip -> skip
117     ; Return e -> returnStat (expr e)
118     ; Block ss -> vcat [statement s | s <- ss]
119     ; ExprStatement e -> exprStatement (expr e)
120     ; Declaration d -> declStatement (decl d)
121     ; IfThenElse ecs s -> ifthenelse [ (expr e, statement s) | (e,s) <- ecs ] (maybe Nothing (Just .statement) s)
122     ; Switch e as d -> switch (expr e) (arms as) (deflt d)
123     } 
124
125 skip = empty
126   
127 returnStat e = sep [text "return", indent e <> semi]
128
129 exprStatement e = e <> semi
130
131 declStatement d = d
132
133 ifthenelse ((e,s):ecs) ms = sep [text "if", 
134                                 indent (parens e) <+> text "{", 
135                                 indent s, 
136                               thenelse ecs ms]
137
138 thenelse ((e,s):ecs) ms = sep [ text "} else if", 
139                                 indent (parens e) <+> text "{", 
140                                 indent s,
141                                 thenelse ecs ms]
142
143 thenelse [] Nothing  = text "}"
144 thenelse [] (Just s) = sep [text "} else {", indent s, text "}"]
145     
146 switch = \e -> \as -> \d ->
147   text "switch" <+> parens e <+> text "{" 
148   $$ indent (as $$ d)
149   $$ text "}"
150   
151 deflt Nothing   = empty
152 deflt (Just ss) = text "default:" $$ indent (statements ss)  
153     
154 arms [] = empty
155 arms ((e,ss):as) = text "case" <+> expr e <> colon
156                    $$ indent (statements ss)
157                    $$ arms as
158
159 maybeExpr Nothing  = Nothing
160 maybeExpr (Just e) = Just (expr e)
161            
162 expr = \e ->
163  case e of
164    { Var n -> name n
165    ; Literal l -> literal l
166    ; Cast t e -> cast (typ t) e
167    ; Access e n -> expr e <> text "." <> name n
168    ; Assign l r -> assign (expr l) r
169    ; New n es ds -> new (typ n) es (maybeClass ds)
170    ; Call e n es -> call (expr e) (name n) es
171    ; Op e1 o e2 -> op e1 o e2
172    ; InstanceOf e t -> expr e <+> text "instanceof" <+> typ t
173    ; NewArray n es -> newArray (typ n) es
174    }
175    
176 op = \e1 -> \o -> \e2 ->
177   ( if isSimple e1 
178     then expr e1 
179     else parens (expr e1)
180   ) 
181   <+> 
182   text o
183   <+>
184   ( if isSimple e2
185     then expr e2 
186     else parens (expr e2)
187   )
188   
189 assign = \l -> \r ->
190   if isSimple r
191   then l <+> text "=" <+> (expr r)
192   else l <+> text "=" $$ indent (expr r)
193
194 cast = \t -> \e ->
195   if isSimple e
196   then parens (parens t <> expr e)
197   else parens (parens t $$ indent (expr e))
198
199 new n [] (Just ds) = sep [text "new" <+> n <+> text "()" <+> text "{",
200                              indent ds,
201                              text "}"]
202 new n es Nothing = text "new" <+> n <> parens (hsep (punctuate comma (map expr es)))
203
204 newArray n es = text "new" <+> n <> text "[]" <+> braces (hsep (punctuate comma (map expr es)))
205       
206 call e n es = e <> dot <> n <> parens (hsep (punctuate comma (map expr es)))
207
208 literal = \l ->
209   case l of
210     { IntLit i    -> text (show i)
211     ; UIntLit i   -> text (show i)
212     ; CharLit c   -> text (show c)
213     ; UCharLit c  -> text (show c)
214     ; StringLit s -> text (show s)
215     }
216
217 maybeClass Nothing   = Nothing
218 maybeClass (Just ds) = Just (decls ds)
219 \end{code}