5de371b6189347fb50101148a1ac5774dfb29afe
[ghc-hetmet.git] / ghc / compiler / javaGen / Java.lhs
1 Abstract syntax for Java subset that is the target of Mondrian.
2 The syntax has been taken from "The Java Language Specification".
3
4 (c) Erik Meijer & Arjan van IJzendoorn
5
6 November 1999
7
8 \begin{code}
9 module Java where
10
11 \end{code}
12
13 %************************************************************************
14 %*                                                                      *
15 \subsection{Java type declararations}
16 %*                                                                      *
17 %************************************************************************
18
19 \begin{code}
20 data CompilationUnit
21   = Package Name [Decl]
22     deriving (Show)
23     
24 data Decl
25  = Import Name
26  | Field [Modifier] Type Name (Maybe Expr)   
27  | Constructor [Modifier] Name [Parameter] [Statement]
28  | Method [Modifier] Type Name [Parameter] [Statement]
29  | Comment [String]
30  | Interface [Modifier] Name [Name] [Decl]
31  | Class [Modifier] Name [Name] [Name] [Decl]
32    deriving (Show)
33    
34 data Parameter
35  = Parameter [Modifier] Type Name
36    deriving (Show)
37    
38 data Statement
39   = Skip
40   | Return Expr
41   | Block [Statement]
42   | ExprStatement Expr
43   | Declaration Decl -- variable = inner Field, Class = innerclass
44   | IfThenElse [(Expr,Statement)] (Maybe Statement)
45   | Switch Expr [(Expr, [Statement])] (Maybe [Statement])
46     deriving (Show)
47
48 data Expr 
49   = Var Name
50   | Literal Lit
51   | Cast Type Expr
52   | Access Expr Name
53   | Assign Expr Expr
54   | InstanceOf Expr Type
55   | Call Expr Name [Expr]
56   | Op Expr String Expr
57   | New Name [Expr] (Maybe [Decl]) -- anonymous innerclass
58   | NewArray Name [Expr]
59     deriving (Show)
60     
61 data Type 
62   = Type Name
63   | Array Type
64     deriving (Show)
65     
66 data Modifier 
67   = Public | Protected | Private
68   | Static
69   | Abstract | Final | Native | Synchronized | Transient | Volatile
70   deriving (Show, Eq, Ord)
71   
72 type Name = [String]
73
74 data Lit
75   = IntLit Int          -- Boxed
76   | UIntLit Int         -- Unboxed
77   | CharLit Char        -- Boxed
78   | UCharLit Char       -- Unboxed
79   | StringLit String
80   deriving Show
81
82 addModifier :: Modifier -> Decl -> Decl
83 addModifier = \m -> \d ->
84  case d of
85    { Import n -> Import n
86    ; Field ms t n e -> Field (m:ms) t n e  
87    ; Constructor ms n as ss -> Constructor (m:ms) n as ss
88    ; Method ms t n as ss -> Method (m:ms) t n as ss
89    ; Comment ss -> Comment ss
90    ; Interface ms n xs ds -> Interface (m:ms) n xs ds
91    ; Class ms n xs is ds -> Class (m:ms) n xs is ds
92    }
93    
94 areSimple :: [Expr] -> Bool
95 areSimple = \es -> all isSimple es
96
97 isSimple :: Expr -> Bool
98 isSimple = \e ->
99   case e of
100    { Cast t e -> isSimple e
101    ; Access e n -> isSimple e
102    ; Assign l r -> isSimple l && isSimple r
103    ; InstanceOf e t -> isSimple e
104    ; Call e n es -> isSimple e && areSimple es
105    ; Op e1 o e2 -> False
106    ; New n es Nothing -> areSimple es
107    ; New n es (Just ds) -> False
108    ; otherwise -> True
109    }
110 \end{code}