[project @ 1996-03-19 08:58:34 by partain]
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsLit.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
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 Ubiq{-uitous-}
12
13 import Pretty
14 \end{code}
15
16 \begin{code}
17 data HsLit
18   = HsChar          Char        -- characters
19   | HsCharPrim      Char        -- unboxed char literals
20   | HsString        FAST_STRING -- strings
21   | HsStringPrim    FAST_STRING -- packed string
22
23   | HsInt           Integer     -- integer-looking literals
24   | HsFrac          Rational    -- frac-looking literals
25         -- Up through dict-simplification, HsInt and HsFrac simply
26         -- mean the literal was integral- or fractional-looking; i.e.,
27         -- whether it had an explicit decimal-point in it.  *After*
28         -- dict-simplification, they mean (boxed) "Integer" and
29         -- "Rational" [Ratio Integer], respectively.
30
31         -- Dict-simplification tries to replace such lits w/ more
32         -- specific ones, using the unboxed variants that follow...
33   | HsIntPrim       Integer     -- unboxed Int literals
34   | HsFloatPrim     Rational    -- unboxed Float literals
35   | HsDoublePrim    Rational    -- unboxed Double literals
36
37   | HsLitLit        FAST_STRING -- to pass ``literal literals'' through to C
38                                 -- also: "overloaded" type; but
39                                 -- must resolve to boxed-primitive!
40                                 -- (WDP 94/10)
41 \end{code}
42
43 \begin{code}
44 negLiteral (HsInt  i) = HsInt  (-i)
45 negLiteral (HsFrac f) = HsFrac (-f)
46 \end{code}
47
48 \begin{code}
49 instance Outputable HsLit where
50     ppr sty (HsChar c)          = ppStr (show c)
51     ppr sty (HsCharPrim c)      = ppBeside (ppStr (show c)) (ppChar '#')
52     ppr sty (HsString s)        = ppStr (show s)
53     ppr sty (HsStringPrim s)    = ppBeside (ppStr (show s)) (ppChar '#')
54     ppr sty (HsInt i)           = ppInteger i
55     ppr sty (HsFrac f)          = ppRational f
56     ppr sty (HsFloatPrim f)     = ppBeside (ppRational f) (ppChar '#')
57     ppr sty (HsDoublePrim d)    = ppBeside (ppRational d) (ppStr "##")
58     ppr sty (HsIntPrim i)       = ppBeside (ppInteger i) (ppChar '#')
59     ppr sty (HsLitLit s)        = ppBesides [ppStr "``", ppPStr s, ppStr "''"]
60 \end{code}