2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[HsLit]{Abstract syntax: source-language literals}
9 #include "HsVersions.h"
13 import Ratio ( Rational )
17 %************************************************************************
19 \subsection[HsLit]{Literals}
21 %************************************************************************
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
44 data HsOverLit -- An overloaded literal
45 = HsIntegral Integer -- Integer-looking literals;
46 | HsFractional Rational -- Frac-looking literals
48 instance Eq HsOverLit where
49 (HsIntegral i1) == (HsIntegral i2) = i1 == i2
50 (HsFractional f1) == (HsFractional f2) = f1 == f2
52 instance Ord HsOverLit where
53 compare (HsIntegral i1) (HsIntegral i2) = i1 `compare` i2
54 compare (HsIntegral _) (HsFractional _) = LT
55 compare (HsFractional f1) (HsFractional f2) = f1 `compare` f2
56 compare (HsFractional f1) (HsIntegral _) = GT
60 instance Outputable HsLit where
61 -- Use "show" because it puts in appropriate escapes
62 ppr (HsChar c) = pprHsChar c
63 ppr (HsCharPrim c) = pprHsChar c <> char '#'
64 ppr (HsString s) = pprHsString s
65 ppr (HsStringPrim s) = pprHsString s <> char '#'
66 ppr (HsInt i) = integer i
67 ppr (HsInteger i) = integer i
68 ppr (HsRat f _) = rational f
69 ppr (HsFloatPrim f) = rational f <> char '#'
70 ppr (HsDoublePrim d) = rational d <> text "##"
71 ppr (HsIntPrim i) = integer i <> char '#'
72 ppr (HsLitLit s _) = hcat [text "``", ptext s, text "''"]
74 instance Outputable HsOverLit where
75 ppr (HsIntegral i) = integer i
76 ppr (HsFractional f) = rational f