2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4 \section{Generate Java}
7 module PrintJava( compilationUnit ) where
11 import Char( toLower )
15 indent :: SDoc -> SDoc
19 %************************************************************************
21 \subsection{Pretty printer}
23 %************************************************************************
26 compilationUnit :: CompilationUnit -> SDoc
27 compilationUnit (Package n ds) = package n (decls ds)
29 package = \n -> \ds ->
30 text "package" <+> name n <> text ";"
35 decls (d:ds) = decl d $$ decls ds
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 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)
48 importDecl n = text "import" <+> n <> text ";"
50 field = \mfs -> \t -> \n -> \e ->
52 { Nothing -> mfs <+> t <+> n <> text ";"
53 ; Just e -> lay [mfs <+> t <+> n <+> text "=", indent (expr e <> text ";")]
55 lay | isSimple e = hsep
59 constructor = \mfs -> \n -> \as -> \ss ->
60 mfs <+> n <+> parens (hsep (punctuate comma as)) <+> text "{"
64 method = \mfs -> \t -> \n -> \as -> \ss ->
65 mfs <+> t <+> n <+> parens (hsep (punctuate comma as)) <+> text "{"
71 $$ indent (vcat [ text s | s <- ss])
74 interface = \mfs -> \n -> \xs -> \ms ->
75 mfs <+> n <+> xs <+> text "{"
79 clazz = \mfs -> \n -> \x -> \is -> \ms ->
80 mfs <+> text "class" <+> n <+> x <+> is <+> text "{"
85 text "static" <+> text "{"
89 modifiers mfs = hsep (map modifier mfs)
91 modifier mf = text $ map toLower (show mf)
94 extends xs = text "extends" <+> hsep (punctuate comma (map name xs))
97 implements xs = text "implements" <+> hsep (punctuate comma (map name xs))
101 parameters as = map parameter as
103 parameter (Parameter mfs t n) = modifiers mfs <+> typ t <+> name n
105 typ (PrimType s) = text s
106 typ (Type n) = hcat (punctuate dot (map text n))
107 typ (ArrayType t) = typ t <> text "[]"
109 statements ss = vcat (map statement ss)
114 ; Return e -> returnStat (expr e)
115 ; Block ss -> vcat [statement s | s <- ss]
116 ; ExprStatement e -> exprStatement (expr e)
117 ; Declaration d -> declStatement (decl d)
118 ; IfThenElse ecs s -> ifthenelse [ (expr e, statement s) | (e,s) <- ecs ] (maybe Nothing (Just .statement) s)
119 ; Switch e as d -> switch (expr e) (arms as) (deflt d)
124 returnStat e = sep [text "return", indent e <> semi]
126 exprStatement e = e <> semi
130 ifthenelse ((e,s):ecs) ms = sep [text "if",
131 indent (parens e) <+> text "{",
135 thenelse ((e,s):ecs) ms = sep [ text "} else if",
136 indent (parens e) <+> text "{",
140 thenelse [] Nothing = text "}"
141 thenelse [] (Just s) = sep [text "} else {", indent s, text "}"]
143 switch = \e -> \as -> \d ->
144 text "switch" <+> parens e <+> text "{"
148 deflt Nothing = empty
149 deflt (Just ss) = text "default:" $$ indent (statements ss)
152 arms ((e,ss):as) = text "case" <+> expr e <> colon
153 $$ indent (statements ss)
156 maybeExpr Nothing = Nothing
157 maybeExpr (Just e) = Just (expr e)
162 ; Literal l -> literal l
163 ; Cast t e -> cast (typ t) e
164 ; Access e n -> expr e <> text "." <> name n
165 ; Assign l r -> assign (expr l) r
166 ; New n es ds -> new (typ n) es (maybeClass ds)
167 ; Call e n es -> call (expr e) (name n) es
168 ; Op e1 o e2 -> op e1 o e2
169 ; InstanceOf e t -> expr e <+> text "instanceof" <+> typ t
170 ; NewArray n es -> newArray (typ n) es
173 op = \e1 -> \o -> \e2 ->
176 else parens (expr e1)
183 else parens (expr e2)
188 then l <+> text "=" <+> (expr r)
189 else l <+> text "=" $$ indent (expr r)
193 then parens (parens t <> expr e)
194 else parens (parens t $$ indent (expr e))
196 new n [] (Just ds) = sep [text "new" <+> n <+> text "()" <+> text "{",
199 new n es Nothing = text "new" <+> n <> parens (hsep (punctuate comma (map expr es)))
201 newArray n es = text "new" <+> n <> text "[]" <+> braces (hsep (punctuate comma (map expr es)))
203 call e n es = e <> dot <> n <> parens (hsep (punctuate comma (map expr es)))
207 { IntLit i -> text (show i)
208 ; UIntLit i -> text (show i)
209 ; CharLit c -> text (show c)
210 ; UCharLit c -> text (show c)
211 ; StringLit s -> text (show s)
214 maybeClass Nothing = Nothing
215 maybeClass (Just ds) = Just (decls ds)