More import tidying and fixing the stage 2 build
[ghc-hetmet.git] / compiler / hsSyn / HsLit.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5 \section[HsLit]{Abstract syntax: source-language literals}
6
7 \begin{code}
8 module HsLit where
9
10 #include "HsVersions.h"
11
12 import {-# SOURCE #-} HsExpr( SyntaxExpr )
13 import Type     ( Type )
14 import Outputable
15 import FastString
16 import Ratio    ( Rational )
17 \end{code}
18
19
20 %************************************************************************
21 %*                                                                      *
22 \subsection[HsLit]{Literals}
23 %*                                                                      *
24 %************************************************************************
25
26
27 \begin{code}
28 data HsLit
29   = HsChar          Char                -- Character
30   | HsCharPrim      Char                -- Unboxed character
31   | HsString        FastString          -- String
32   | HsStringPrim    FastString          -- Packed string
33   | HsInt           Integer             -- Genuinely an Int; arises from TcGenDeriv, 
34                                         --      and from TRANSLATION
35   | HsIntPrim       Integer             -- Unboxed Int
36   | HsInteger       Integer  Type       -- Genuinely an integer; arises only from TRANSLATION
37                                         --      (overloaded literals are done with HsOverLit)
38   | HsRat           Rational Type       -- Genuinely a rational; arises only from TRANSLATION
39                                         --      (overloaded literals are done with HsOverLit)
40   | HsFloatPrim     Rational            -- Unboxed Float
41   | HsDoublePrim    Rational            -- Unboxed Double
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   lit1              == lit2              = False
55
56 data HsOverLit id       -- An overloaded literal
57   = HsIntegral   Integer  (SyntaxExpr id)       -- Integer-looking literals;
58   | HsFractional Rational (SyntaxExpr id)       -- Frac-looking literals
59   -- Before type checking, the SyntaxExpr is 'fromInteger' or 'fromRational'
60   -- After type checking, it is (fromInteger 3) or lit_78; that is,
61   -- the expression that should replace the literal.
62   -- This is unusual, because we're replacing 'fromInteger' with a call 
63   -- to fromInteger.  Reason: it allows commoning up of the fromInteger
64   -- calls, which wouldn't be possible if the desguarar made the application
65
66 -- Comparison operations are needed when grouping literals
67 -- for compiling pattern-matching (module MatchLit)
68 instance Eq (HsOverLit id) where
69   (HsIntegral i1 _)   == (HsIntegral i2 _)   = i1 == i2
70   (HsFractional f1 _) == (HsFractional f2 _) = f1 == f2
71   l1                  == l2                  = False
72
73 instance Ord (HsOverLit id) where
74   compare (HsIntegral i1 _)   (HsIntegral i2 _)   = i1 `compare` i2
75   compare (HsIntegral _ _)    (HsFractional _ _)  = LT
76   compare (HsFractional f1 _) (HsFractional f2 _) = f1 `compare` f2
77   compare (HsFractional f1 _) (HsIntegral _ _)    = GT
78 \end{code}
79
80 \begin{code}
81 instance Outputable HsLit where
82         -- Use "show" because it puts in appropriate escapes
83     ppr (HsChar c)       = pprHsChar c
84     ppr (HsCharPrim c)   = pprHsChar c <> char '#'
85     ppr (HsString s)     = pprHsString s
86     ppr (HsStringPrim s) = pprHsString s <> char '#'
87     ppr (HsInt i)        = integer i
88     ppr (HsInteger i _)  = integer i
89     ppr (HsRat f _)      = rational f
90     ppr (HsFloatPrim f)  = rational f <> char '#'
91     ppr (HsDoublePrim d) = rational d <> text "##"
92     ppr (HsIntPrim i)    = integer i  <> char '#'
93
94 instance Outputable (HsOverLit id) where
95   ppr (HsIntegral i _)   = integer i
96   ppr (HsFractional f _) = rational f
97 \end{code}