[project @ 2001-06-25 08:09:57 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
43 instance Eq HsLit where
44   (HsChar x1)       == (HsChar x2)       = x1==x2
45   (HsCharPrim x1)   == (HsCharPrim x2)   = x1==x2
46   (HsString x1)     == (HsString x2)     = x1==x2
47   (HsStringPrim x1) == (HsStringPrim x2) = x1==x2
48   (HsInt x1)        == (HsInt x2)        = x1==x2
49   (HsIntPrim x1)    == (HsIntPrim x2)    = x1==x2
50   (HsInteger x1)    == (HsInteger x2)    = x1==x2
51   (HsRat x1 _)      == (HsRat x2 _)      = x1==x2
52   (HsFloatPrim x1)  == (HsFloatPrim x2)  = x1==x2
53   (HsDoublePrim x1) == (HsDoublePrim x2) = x1==x2
54   (HsLitLit x1 _)   == (HsLitLit x2 _)   = x1==x2
55   lit1              == lit2              = False
56
57 data HsOverLit          -- An overloaded literal
58   = HsIntegral      Integer             -- Integer-looking literals;
59   | HsFractional    Rational            -- Frac-looking literals
60
61 instance Eq HsOverLit where
62   (HsIntegral i1)   == (HsIntegral i2)   = i1 == i2
63   (HsFractional f1) == (HsFractional f2) = f1 == f2
64
65 instance Ord HsOverLit where
66   compare (HsIntegral i1)   (HsIntegral i2)   = i1 `compare` i2
67   compare (HsIntegral _)    (HsFractional _)  = LT
68   compare (HsFractional f1) (HsFractional f2) = f1 `compare` f2
69   compare (HsFractional f1) (HsIntegral _)    = GT
70 \end{code}
71
72 \begin{code}
73 instance Outputable HsLit where
74         -- Use "show" because it puts in appropriate escapes
75     ppr (HsChar c)       = pprHsChar c
76     ppr (HsCharPrim c)   = pprHsChar c <> char '#'
77     ppr (HsString s)     = pprHsString s
78     ppr (HsStringPrim s) = pprHsString s <> char '#'
79     ppr (HsInt i)        = integer i
80     ppr (HsInteger i)    = integer i
81     ppr (HsRat f _)      = rational f
82     ppr (HsFloatPrim f)  = rational f <> char '#'
83     ppr (HsDoublePrim d) = rational d <> text "##"
84     ppr (HsIntPrim i)    = integer i  <> char '#'
85     ppr (HsLitLit s _)   = hcat [text "``", ptext s, text "''"]
86
87 instance Outputable HsOverLit where
88   ppr (HsIntegral i)   = integer i
89   ppr (HsFractional f) = rational f
90 \end{code}
91
92