2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[HsLit]{Abstract syntax: source-language literals}
9 #include "HsVersions.h"
13 import HsTypes ( PostTcType )
15 import Ratio ( Rational )
19 %************************************************************************
21 \subsection[HsLit]{Literals}
23 %************************************************************************
28 = HsChar Int -- Character
29 | HsCharPrim Int -- Unboxed character
30 | HsString FAST_STRING -- String
31 | HsStringPrim FAST_STRING -- Packed string
32 | HsInt Integer -- Genuinely an Int; arises from TcGenDeriv,
33 -- and from TRANSLATION
34 | HsIntPrim Integer -- Unboxed Int
35 | HsInteger Integer -- Genuinely an integer; arises only from TRANSLATION
36 | HsRat Rational Type -- Genuinely a rational; arises only from TRANSLATION
37 | HsFloatPrim Rational -- Unboxed Float
38 | HsDoublePrim Rational -- Unboxed Double
39 | HsLitLit FAST_STRING PostTcType -- to pass ``literal literals'' through to C
40 -- also: "overloaded" type; but
41 -- must resolve to boxed-primitive!
42 -- The Type in HsLitLit is needed when desuaring;
43 -- before the typechecker it's just an error value
45 instance Eq HsLit where
46 (HsChar x1) == (HsChar x2) = x1==x2
47 (HsCharPrim x1) == (HsCharPrim x2) = x1==x2
48 (HsString x1) == (HsString x2) = x1==x2
49 (HsStringPrim x1) == (HsStringPrim x2) = x1==x2
50 (HsInt x1) == (HsInt x2) = x1==x2
51 (HsIntPrim x1) == (HsIntPrim x2) = x1==x2
52 (HsInteger x1) == (HsInteger x2) = x1==x2
53 (HsRat x1 _) == (HsRat x2 _) = x1==x2
54 (HsFloatPrim x1) == (HsFloatPrim x2) = x1==x2
55 (HsDoublePrim x1) == (HsDoublePrim x2) = x1==x2
56 (HsLitLit x1 _) == (HsLitLit x2 _) = x1==x2
59 data HsOverLit -- An overloaded literal
60 = HsIntegral Integer Name -- Integer-looking literals;
61 -- The name is fromInteger
62 | HsFractional Rational Name -- Frac-looking literals
63 -- The name is fromRational
65 instance Eq HsOverLit where
66 (HsIntegral i1 _) == (HsIntegral i2 _) = i1 == i2
67 (HsFractional f1 _) == (HsFractional f2 _) = f1 == f2
69 instance Ord HsOverLit where
70 compare (HsIntegral i1 _) (HsIntegral i2 _) = i1 `compare` i2
71 compare (HsIntegral _ _) (HsFractional _ _) = LT
72 compare (HsFractional f1 _) (HsFractional f2 _) = f1 `compare` f2
73 compare (HsFractional f1 _) (HsIntegral _ _) = GT
77 instance Outputable HsLit where
78 -- Use "show" because it puts in appropriate escapes
79 ppr (HsChar c) = pprHsChar c
80 ppr (HsCharPrim c) = pprHsChar c <> char '#'
81 ppr (HsString s) = pprHsString s
82 ppr (HsStringPrim s) = pprHsString s <> char '#'
83 ppr (HsInt i) = integer i
84 ppr (HsInteger i) = integer i
85 ppr (HsRat f _) = rational f
86 ppr (HsFloatPrim f) = rational f <> char '#'
87 ppr (HsDoublePrim d) = rational d <> text "##"
88 ppr (HsIntPrim i) = integer i <> char '#'
89 ppr (HsLitLit s _) = hcat [text "``", ptext s, text "''"]
91 instance Outputable HsOverLit where
92 ppr (HsIntegral i _) = integer i
93 ppr (HsFractional f _) = rational f