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