2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 \section[HsLit]{Abstract syntax: source-language literals}
10 #include "HsVersions.h"
12 import {-# SOURCE #-} HsExpr( SyntaxExpr )
16 import Ratio ( Rational )
20 %************************************************************************
22 \subsection[HsLit]{Literals}
24 %************************************************************************
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
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
56 data HsOverLit id -- An overloaded literal
57 = HsIntegral Integer (SyntaxExpr id) -- Integer-looking literals;
58 | HsFractional Rational (SyntaxExpr id) -- Frac-looking literals
59 | HsIsString FastString (SyntaxExpr id) -- String-looking literals
60 -- Before type checking, the SyntaxExpr is 'fromInteger' or 'fromRational'
61 -- After type checking, it is (fromInteger 3) or lit_78; that is,
62 -- the expression that should replace the literal.
63 -- This is unusual, because we're replacing 'fromInteger' with a call
64 -- to fromInteger. Reason: it allows commoning up of the fromInteger
65 -- calls, which wouldn't be possible if the desguarar made the application
67 -- Comparison operations are needed when grouping literals
68 -- for compiling pattern-matching (module MatchLit)
69 instance Eq (HsOverLit id) where
70 (HsIntegral i1 _) == (HsIntegral i2 _) = i1 == i2
71 (HsFractional f1 _) == (HsFractional f2 _) = f1 == f2
72 (HsIsString s1 _) == (HsIsString s2 _) = s1 == s2
75 instance Ord (HsOverLit id) where
76 compare (HsIntegral i1 _) (HsIntegral i2 _) = i1 `compare` i2
77 compare (HsIntegral _ _) (HsFractional _ _) = LT
78 compare (HsIntegral _ _) (HsIsString _ _) = LT
79 compare (HsFractional f1 _) (HsFractional f2 _) = f1 `compare` f2
80 compare (HsFractional f1 _) (HsIntegral _ _) = GT
81 compare (HsFractional f1 _) (HsIsString _ _) = LT
82 compare (HsIsString s1 _) (HsIsString s2 _) = s1 `compare` s2
83 compare (HsIsString s1 _) (HsIntegral _ _) = GT
84 compare (HsIsString s1 _) (HsFractional _ _) = GT
88 instance Outputable HsLit where
89 -- Use "show" because it puts in appropriate escapes
90 ppr (HsChar c) = pprHsChar c
91 ppr (HsCharPrim c) = pprHsChar c <> char '#'
92 ppr (HsString s) = pprHsString s
93 ppr (HsStringPrim s) = pprHsString s <> char '#'
94 ppr (HsInt i) = integer i
95 ppr (HsInteger i _) = integer i
96 ppr (HsRat f _) = rational f
97 ppr (HsFloatPrim f) = rational f <> char '#'
98 ppr (HsDoublePrim d) = rational d <> text "##"
99 ppr (HsIntPrim i) = integer i <> char '#'
101 instance Outputable (HsOverLit id) where
102 ppr (HsIntegral i _) = integer i
103 ppr (HsFractional f _) = rational f
104 ppr (HsIsString s _) = pprHsString s