[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsBasic.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[HsLit]{Abstract syntax: source-language literals}
5
6 \begin{code}
7 module HsBasic where
8
9 #include "HsVersions.h"
10
11 import Outputable
12 import Ratio    ( Rational )
13 \end{code}
14
15 %************************************************************************
16 %*                                                                      *
17 \subsection[HsLit]{Literals}
18 %*                                                                      *
19 %************************************************************************
20
21
22 \begin{code}
23 data HsLit
24   = HsChar          Char        -- characters
25   | HsCharPrim      Char        -- unboxed char literals
26   | HsString        FAST_STRING -- strings
27   | HsStringPrim    FAST_STRING -- packed string
28
29   | HsInt           Integer     -- integer-looking literals
30   | HsFrac          Rational    -- frac-looking literals
31         -- Up through dict-simplification, HsInt and HsFrac simply
32         -- mean the literal was integral- or fractional-looking; i.e.,
33         -- whether it had an explicit decimal-point in it.  *After*
34         -- dict-simplification, they mean (boxed) "Integer" and
35         -- "Rational" [Ratio Integer], respectively.
36
37         -- Dict-simplification tries to replace such lits w/ more
38         -- specific ones, using the unboxed variants that follow...
39   | HsIntPrim       Integer     -- unboxed Int literals
40   | HsFloatPrim     Rational    -- unboxed Float literals
41   | HsDoublePrim    Rational    -- unboxed Double literals
42
43   | HsLitLit        FAST_STRING -- to pass ``literal literals'' through to C
44                                 -- also: "overloaded" type; but
45                                 -- must resolve to boxed-primitive!
46                                 -- (WDP 94/10)
47         deriving Eq
48 \end{code}
49
50 ToDo: an improved Eq instance JJQC 30-Nov-1997
51
52 \begin{code}
53 negLiteral (HsInt  i) = HsInt  (-i)
54 negLiteral (HsFrac f) = HsFrac (-f)
55 \end{code}
56
57 \begin{code}
58 instance Outputable HsLit where
59         -- Use "show" because it puts in appropriate escapes
60     ppr (HsChar c)       = text (show c)
61     ppr (HsCharPrim c)   = text (show c) <> char '#'
62     ppr (HsStringPrim s) = pprFSAsString s <> char '#'
63     ppr (HsString s)     = pprFSAsString s
64     ppr (HsInt i)        = integer i
65     ppr (HsFrac f)       = rational f
66     ppr (HsFloatPrim f)  = rational f <> char '#'
67     ppr (HsDoublePrim d) = rational d <> text "##"
68     ppr (HsIntPrim i)    = integer i  <> char '#'
69     ppr (HsLitLit s)     = hcat [text "``", ptext s, text "''"]
70 \end{code}
71
72