[project @ 2001-02-20 09:40:43 by simonpj]
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsLit.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 HsLit where
8
9 #include "HsVersions.h"
10
11 import Type     ( Type )        
12 import Outputable
13 import Ratio    ( Rational )
14 \end{code}
15
16
17 %************************************************************************
18 %*                                                                      *
19 \subsection[HsLit]{Literals}
20 %*                                                                      *
21 %************************************************************************
22
23
24 \begin{code}
25 data HsLit
26   = HsChar          Int                 -- Character
27   | HsCharPrim      Int                 -- Unboxed character
28   | HsString        FAST_STRING         -- String
29   | HsStringPrim    FAST_STRING         -- Packed string
30   | HsInt           Integer             -- Genuinely an Int; arises from TcGenDeriv, 
31                                         --      and from TRANSLATION
32   | HsIntPrim       Integer             -- Unboxed Int
33   | HsInteger       Integer             -- Genuinely an integer; arises only from TRANSLATION
34   | HsRat           Rational Type       -- Genuinely a rational; arises only from TRANSLATION
35   | HsFloatPrim     Rational            -- Unboxed Float
36   | HsDoublePrim    Rational            -- Unboxed Double
37   | HsLitLit        FAST_STRING Type    -- to pass ``literal literals'' through to C
38                                         -- also: "overloaded" type; but
39                                         -- must resolve to boxed-primitive!
40         -- The Type in HsLitLit is needed when desuaring;
41         -- before the typechecker it's just an error value
42   deriving( Eq )
43
44 data HsOverLit          -- An overloaded literal
45   = HsIntegral      Integer             -- Integer-looking literals;
46   | HsFractional    Rational            -- Frac-looking literals
47
48 instance Eq HsOverLit where
49   (HsIntegral i1)   == (HsIntegral i2)   = i1 == i2
50   (HsFractional f1) == (HsFractional f2) = f1 == f2
51
52 instance Ord HsOverLit where
53   compare (HsIntegral i1)   (HsIntegral i2)   = i1 `compare` i2
54   compare (HsIntegral _)    (HsFractional _)  = LT
55   compare (HsFractional f1) (HsFractional f2) = f1 `compare` f2
56   compare (HsFractional f1) (HsIntegral _)    = GT
57 \end{code}
58
59 \begin{code}
60 instance Outputable HsLit where
61         -- Use "show" because it puts in appropriate escapes
62     ppr (HsChar c)       = pprHsChar c
63     ppr (HsCharPrim c)   = pprHsChar c <> char '#'
64     ppr (HsString s)     = pprHsString s
65     ppr (HsStringPrim s) = pprHsString s <> char '#'
66     ppr (HsInt i)        = integer i
67     ppr (HsInteger i)    = integer i
68     ppr (HsRat f _)      = rational f
69     ppr (HsFloatPrim f)  = rational f <> char '#'
70     ppr (HsDoublePrim d) = rational d <> text "##"
71     ppr (HsIntPrim i)    = integer i  <> char '#'
72     ppr (HsLitLit s _)   = hcat [text "``", ptext s, text "''"]
73
74 instance Outputable HsOverLit where
75   ppr (HsIntegral i)   = integer i
76   ppr (HsFractional f) = rational f
77 \end{code}
78
79