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" <+> packagename n <> text ";"
35 decls (d:ds) = decl d $$ decls ds
39 { Import n -> importDecl (packagename n)
40 ; Field mfs n e -> field (modifiers mfs) (nameTy n) (name n) e
41 ; Constructor mfs n as ss -> constructor (modifiers mfs) (typename n) (parameters as) (statements ss)
42 ; Method mfs n as ts ss -> method (modifiers mfs) (nameTy n) (name n) (parameters as) (throws ts) (statements ss)
43 ; Comment s -> comment s
44 ; Interface mfs n is ms -> interface (modifiers mfs) (typename n) (extends is) (decls ms)
45 ; Class mfs n x is ms -> clazz (modifiers mfs) (typename 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 -> \ts -> \ss ->
65 mfs <+> t <+> n <+> parens (hsep (punctuate comma as)) <+> ts <+> 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 "{"
84 modifiers mfs = hsep (map modifier mfs)
86 modifier mf = text $ map toLower (show mf)
89 extends xs = text "extends" <+> hsep (punctuate comma (map typename xs))
92 implements xs = text "implements" <+> hsep (punctuate comma (map typename xs))
95 throws xs = text "throws" <+> hsep (punctuate comma (map typename xs))
97 name (Name n t) = text n
99 nameTy (Name n t) = typ t
102 packagename n = text n
104 parameters as = map parameter as
106 parameter (Parameter mfs n) = modifiers mfs <+> nameTy n <+> name n
108 typ (PrimType s) = primtype s
109 typ (Type n) = typename n
110 typ (ArrayType t) = typ t <> text "[]"
112 primtype PrimInt = text "int"
113 primtype PrimBoolean = text "boolean"
114 primtype PrimChar = text "char"
115 primtype PrimLong = text "long"
116 primtype PrimFloat = text "float"
117 primtype PrimDouble = text "double"
118 primtype PrimByte = text "byte"
119 primtype PrimVoid = text "void"
121 statements ss = vcat (map statement ss)
126 ; Return e -> returnStat (expr e)
127 ; Block ss -> vcat [statement s | s <- ss]
128 ; ExprStatement e -> exprStatement (expr e)
129 ; Declaration d -> declStatement (decl d)
130 ; IfThenElse ecs s -> ifthenelse [ (expr e, statement s) | (e,s) <- ecs ] (maybe Nothing (Just .statement) s)
131 ; Switch e as d -> switch (expr e) (arms as) (deflt d)
136 returnStat e = sep [text "return", indent e <> semi]
138 exprStatement e = e <> semi
142 ifthenelse ((e,s):ecs) ms = sep [ text "if" <+> parens e <+> text "{",
146 thenelse ((e,s):ecs) ms = sep [ text "} else if" <+> parens e <+> text "{",
150 thenelse [] Nothing = text "}"
151 thenelse [] (Just s) = sep [text "} else {", indent s, text "}"]
153 switch = \e -> \as -> \d ->
154 text "switch" <+> parens e <+> text "{"
158 deflt Nothing = empty
159 deflt (Just ss) = text "default:" $$ indent (statements ss)
162 arms ((e,ss):as) = text "case" <+> expr e <> colon
163 $$ indent (statements ss)
166 maybeExpr Nothing = Nothing
167 maybeExpr (Just e) = Just (expr e)
172 ; Literal l -> literal l
173 ; Cast t e -> cast (typ t) e
174 ; Access e n -> expr e <> text "." <> name n
175 ; Assign l r -> assign (expr l) r
176 ; New n es ds -> new (typ n) es (maybeClass ds)
177 ; Raise n es -> text "raise" <+> text n
178 <+> parens (hsep (punctuate comma (map expr es)))
179 ; Call e n es -> call (expr e) (name n) es
180 ; Op e1 o e2 -> op e1 o e2
181 ; InstanceOf e t -> expr e <+> text "instanceof" <+> typ t
184 op = \e1 -> \o -> \e2 ->
187 else parens (expr e1)
194 else parens (expr e2)
199 then l <+> text "=" <+> (expr r)
200 else l <+> text "=" $$ indent (expr r)
204 then parens (parens t <> expr e)
205 else parens (parens t $$ indent (expr e))
207 new n [] (Just ds) = sep [text "new" <+> n <+> text "()" <+> text "{",
210 new n es Nothing = text "new" <+> n <> parens (hsep (punctuate comma (map expr es)))
213 call e n es = e <> dot <> n <> parens (hsep (punctuate comma (map expr es)))
217 { IntLit i -> text (show i)
218 ; CharLit c -> text "(char)" <+> text (show c)
219 ; StringLit s -> text ("\"" ++ s ++ "\"") -- strings are already printable
222 maybeClass Nothing = Nothing
223 maybeClass (Just ds) = Just (decls ds)