[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / compiler / abstractSyn / HsLit.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1994
3 %
4 \section[HsLit]{Abstract syntax: source-language literals}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module HsLit where
10
11 import AbsPrel          ( PrimKind )
12 import Outputable
13 import Pretty
14 import Util
15 \end{code}
16
17 \begin{code}
18 data Literal
19   = CharLit         Char        -- characters
20   | CharPrimLit     Char        -- unboxed char literals
21   | StringLit       FAST_STRING -- strings
22   | StringPrimLit   FAST_STRING -- packed string
23
24   | IntLit          Integer     -- integer-looking literals
25   | FracLit         Rational    -- frac-looking literals
26         -- Up through dict-simplification, IntLit and FracLit simply
27         -- mean the literal was integral- or fractional-looking; i.e.,
28         -- whether it had an explicit decimal-point in it.  *After*
29         -- dict-simplification, they mean (boxed) "Integer" and
30         -- "Rational" [Ratio Integer], respectively.
31
32         -- Dict-simplification tries to replace such lits w/ more
33         -- specific ones, using the unboxed variants that follow...
34   | LitLitLitIn     FAST_STRING -- to pass ``literal literals'' through to C
35                                 -- also: "overloaded" type; but
36                                 -- must resolve to boxed-primitive!
37                                 -- (WDP 94/10)
38   | LitLitLit       FAST_STRING
39                     UniType     -- and now we know the type
40                                 -- Must be a boxed-primitive type
41
42   | IntPrimLit      Integer     -- unboxed Int literals
43 #if __GLASGOW_HASKELL__ <= 22
44   | FloatPrimLit    Double      -- unboxed Float literals
45   | DoublePrimLit   Double      -- unboxed Double literals
46 #else
47   | FloatPrimLit    Rational    -- unboxed Float literals
48   | DoublePrimLit   Rational    -- unboxed Double literals
49 #endif
50 \end{code}
51
52 \begin{code}
53 negLiteral (IntLit  i) = IntLit  (-i)
54 negLiteral (FracLit f) = FracLit (-f)
55 \end{code}
56
57 \begin{code}
58 instance Outputable Literal where
59     ppr sty (CharLit c)         = ppStr (show c)
60     ppr sty (CharPrimLit c)     = ppBeside (ppStr (show c)) (ppChar '#')
61     ppr sty (StringLit s)       = ppStr (show s)
62     ppr sty (StringPrimLit s)   = ppBeside (ppStr (show s)) (ppChar '#')
63     ppr sty (IntLit i)          = ppInteger i
64 #if __GLASGOW_HASKELL__ <= 22
65     ppr sty (FracLit f)         = ppDouble (fromRational f) -- ToDo: better??
66     ppr sty (FloatPrimLit f)    = ppBeside (ppDouble f) (ppChar '#')
67     ppr sty (DoublePrimLit d)   = ppBeside (ppDouble d) (ppStr "##")
68 #else
69     ppr sty (FracLit f)         = ppRational f
70     ppr sty (FloatPrimLit f)    = ppBeside (ppRational f) (ppChar '#')
71     ppr sty (DoublePrimLit d)   = ppBeside (ppRational d) (ppStr "##")
72 #endif
73     ppr sty (IntPrimLit i)      = ppBeside (ppInteger i) (ppChar '#')
74     ppr sty (LitLitLitIn s)     = ppBesides [ppStr "``", ppPStr s, ppStr "''"]
75     ppr sty (LitLitLit s k)     = ppBesides [ppStr "``", ppPStr s, ppStr "''"]
76 \end{code}