29fcce384989aef5934308ad4faf53bd396d530d
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsBasic.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 HsBasic where
10
11 IMP_Ubiq(){-uitous-}
12
13 IMPORT_1_3(Ratio(Rational))
14
15 import Pretty
16 import Outputable
17 \end{code}
18
19 %************************************************************************
20 %*                                                                      *
21 \subsection[HsLit]{Literals}
22 %*                                                                      *
23 %************************************************************************
24
25
26 \begin{code}
27 data HsLit
28   = HsChar          Char        -- characters
29   | HsCharPrim      Char        -- unboxed char literals
30   | HsString        FAST_STRING -- strings
31   | HsStringPrim    FAST_STRING -- packed string
32
33   | HsInt           Integer     -- integer-looking literals
34   | HsFrac          Rational    -- frac-looking literals
35         -- Up through dict-simplification, HsInt and HsFrac simply
36         -- mean the literal was integral- or fractional-looking; i.e.,
37         -- whether it had an explicit decimal-point in it.  *After*
38         -- dict-simplification, they mean (boxed) "Integer" and
39         -- "Rational" [Ratio Integer], respectively.
40
41         -- Dict-simplification tries to replace such lits w/ more
42         -- specific ones, using the unboxed variants that follow...
43   | HsIntPrim       Integer     -- unboxed Int literals
44   | HsFloatPrim     Rational    -- unboxed Float literals
45   | HsDoublePrim    Rational    -- unboxed Double literals
46
47   | HsLitLit        FAST_STRING -- to pass ``literal literals'' through to C
48                                 -- also: "overloaded" type; but
49                                 -- must resolve to boxed-primitive!
50                                 -- (WDP 94/10)
51 \end{code}
52
53 \begin{code}
54 negLiteral (HsInt  i) = HsInt  (-i)
55 negLiteral (HsFrac f) = HsFrac (-f)
56 \end{code}
57
58 \begin{code}
59 instance Outputable HsLit where
60     ppr sty (HsChar c)          = text (show c)
61     ppr sty (HsCharPrim c)      = (<>) (text (show c)) (char '#')
62     ppr sty (HsString s)        = text (show s)
63     ppr sty (HsStringPrim s)    = (<>) (text (show s)) (char '#')
64     ppr sty (HsInt i)           = integer i
65     ppr sty (HsFrac f)          = rational f
66     ppr sty (HsFloatPrim f)     = (<>) (rational f) (char '#')
67     ppr sty (HsDoublePrim d)    = (<>) (rational d) (text "##")
68     ppr sty (HsIntPrim i)       = (<>) (integer i) (char '#')
69     ppr sty (HsLitLit s)        = hcat [text "``", ptext s, text "''"]
70 \end{code}
71
72