[project @ 1996-06-05 06:44:31 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 IMP_Ubiq(){-uitous-}
12 IMPORT_1_3(Ratio(Rational))
13
14 import Pretty
15 \end{code}
16
17 \begin{code}
18 data HsLit
19   = HsChar          Char        -- characters
20   | HsCharPrim      Char        -- unboxed char literals
21   | HsString        FAST_STRING -- strings
22   | HsStringPrim    FAST_STRING -- packed string
23
24   | HsInt           Integer     -- integer-looking literals
25   | HsFrac          Rational    -- frac-looking literals
26         -- Up through dict-simplification, HsInt and HsFrac 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   | HsIntPrim       Integer     -- unboxed Int literals
35   | HsFloatPrim     Rational    -- unboxed Float literals
36   | HsDoublePrim    Rational    -- unboxed Double literals
37
38   | HsLitLit        FAST_STRING -- to pass ``literal literals'' through to C
39                                 -- also: "overloaded" type; but
40                                 -- must resolve to boxed-primitive!
41                                 -- (WDP 94/10)
42 \end{code}
43
44 \begin{code}
45 negLiteral (HsInt  i) = HsInt  (-i)
46 negLiteral (HsFrac f) = HsFrac (-f)
47 \end{code}
48
49 \begin{code}
50 instance Outputable HsLit where
51     ppr sty (HsChar c)          = ppStr (show c)
52     ppr sty (HsCharPrim c)      = ppBeside (ppStr (show c)) (ppChar '#')
53     ppr sty (HsString s)        = ppStr (show s)
54     ppr sty (HsStringPrim s)    = ppBeside (ppStr (show s)) (ppChar '#')
55     ppr sty (HsInt i)           = ppInteger i
56     ppr sty (HsFrac f)          = ppRational f
57     ppr sty (HsFloatPrim f)     = ppBeside (ppRational f) (ppChar '#')
58     ppr sty (HsDoublePrim d)    = ppBeside (ppRational d) (ppStr "##")
59     ppr sty (HsIntPrim i)       = ppBeside (ppInteger i) (ppChar '#')
60     ppr sty (HsLitLit s)        = ppBesides [ppStr "``", ppPStr s, ppStr "''"]
61 \end{code}