1 Anbstract syntax for Java subset that is the target of Mondrian.
2 The syntax has been taken from "The Java Language Specification".
4 (c) Erik Meijer & Arjan van IJzendoorn
8 Major reworking to be usable for the intermeduate (GOO) language
9 for the backend of GHC and to target languauges like Java sucessfully.
14 -- The above warning supression flag is a temporary kludge.
15 -- While working on this module you are encouraged to remove it and fix
16 -- any warnings in the module. See
17 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
24 %************************************************************************
26 \subsection{Java type declararations}
28 %************************************************************************
32 = Package PackageName [Decl]
37 | Field [Modifier] Name (Maybe Expr)
38 | Constructor [Modifier] TypeName [Parameter] [Statement]
39 | Method [Modifier] Name [Parameter] [Exception] [Statement]
41 | Interface [Modifier] TypeName [TypeName] [Decl]
42 | Class [Modifier] TypeName [TypeName] [TypeName] [Decl]
46 = Parameter [Modifier] Name
51 | Return Expr -- This always comes last in a list
52 -- of statements, and it is understood
53 -- you might change this to something
54 -- else (like a variable assignment)
55 -- if this is not top level statements.
57 | ExprStatement Expr -- You are never interested in the result
58 -- of an ExprStatement
59 | Declaration Decl -- variable = inner Field, Class = innerclass
60 | IfThenElse [(Expr,Statement)] (Maybe Statement)
61 | Switch Expr [(Expr, [Statement])] (Maybe [Statement])
70 | InstanceOf Expr Type
71 | Call Expr Name [Expr]
73 | Raise TypeName [Expr]
74 | New Type [Expr] (Maybe [Decl]) -- anonymous innerclass
78 = Public | Protected | Private
80 | Abstract | Final | Native | Synchronized | Transient | Volatile
81 deriving (Show, Eq, Ord)
83 -- A type is used to refer in general to the shape of things,
84 -- or a specific class. Never use a name to refer to a class,
104 type PackageName = String -- A package name
105 -- like "java.awt.Button"
107 type Exception = TypeName -- A class name that must be an exception.
109 type TypeName = String -- a fully qualified type name
110 -- like "java.lang.Object".
111 -- has type "Type <the name>"
113 data Name = Name String Type
114 deriving Show -- A class name or method etc,
115 -- at defintion time,
116 -- this generally not a qualified name.
118 -- The type is shape of the box require
119 -- to store an access to this thing.
120 -- So variables might be Int or Object.
122 -- ** method calls store the returned
123 -- ** type, not a complete arg x result type.
126 -- ... foo1.foo2(...).foo3 ...
127 -- here you want to know the *result*
128 -- after calling foo1, then foo2,
131 instance Eq Name where
132 (Name nm _) == (Name nm' _) = nm == nm'
135 instance Ord Name where
136 (Name nm _) `compare` (Name nm' _) = nm `compare` nm'
140 = IntLit Integer -- unboxed
141 | CharLit Int -- unboxed
142 | StringLit String -- java string
145 addModifier :: Modifier -> Decl -> Decl
146 addModifier = \m -> \d ->
148 { Import n -> Import n
149 ; Field ms n e -> Field (m:ms) n e
150 ; Constructor ms n as ss -> Constructor (m:ms) n as ss
151 ; Method ms n as ts ss -> Method (m:ms) n as ts ss
152 ; Comment ss -> Comment ss
153 ; Interface ms n xs ds -> Interface (m:ms) n xs ds
154 ; Class ms n xs is ds -> Class (m:ms) n xs is ds
157 changeNameType :: Type -> Name -> Name
158 changeNameType ty (Name n _) = Name n ty
160 areSimple :: [Expr] -> Bool
161 areSimple = \es -> all isSimple es
163 isSimple :: Expr -> Bool
166 { Cast t e -> isSimple e
167 ; Access e n -> isSimple e
168 ; Assign l r -> isSimple l && isSimple r
169 ; InstanceOf e t -> isSimple e
170 ; Call e n es -> isSimple e && areSimple es
171 ; Op e1 o e2 -> False
172 ; New n es Nothing -> areSimple es
173 ; New n es (Just ds) -> False