2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[HsLit]{Abstract syntax: source-language literals}
9 #include "HsVersions.h"
12 import HsTypes ( SyntaxName, PostTcType )
15 import Ratio ( Rational )
19 %************************************************************************
21 \subsection[HsLit]{Literals}
23 %************************************************************************
28 = HsChar Int -- Character
29 | HsCharPrim Int -- 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 -- 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 | HsLitLit FastString PostTcType -- to pass ``literal literals'' through to C
42 -- also: "overloaded" type; but
43 -- must resolve to boxed-primitive!
44 -- The Type in HsLitLit is needed when desuaring;
45 -- before the typechecker it's just an error value
47 instance Eq HsLit where
48 (HsChar x1) == (HsChar x2) = x1==x2
49 (HsCharPrim x1) == (HsCharPrim x2) = x1==x2
50 (HsString x1) == (HsString x2) = x1==x2
51 (HsStringPrim x1) == (HsStringPrim x2) = x1==x2
52 (HsInt x1) == (HsInt x2) = x1==x2
53 (HsIntPrim x1) == (HsIntPrim x2) = x1==x2
54 (HsInteger x1) == (HsInteger x2) = x1==x2
55 (HsRat x1 _) == (HsRat x2 _) = x1==x2
56 (HsFloatPrim x1) == (HsFloatPrim x2) = x1==x2
57 (HsDoublePrim x1) == (HsDoublePrim x2) = x1==x2
58 (HsLitLit x1 _) == (HsLitLit x2 _) = x1==x2
61 data HsOverLit -- An overloaded literal
62 = HsIntegral Integer SyntaxName -- Integer-looking literals;
63 -- The name is fromInteger
64 | HsFractional Rational SyntaxName -- Frac-looking literals
65 -- The name is fromRational
67 instance Eq HsOverLit where
68 (HsIntegral i1 _) == (HsIntegral i2 _) = i1 == i2
69 (HsFractional f1 _) == (HsFractional f2 _) = f1 == f2
71 instance Ord HsOverLit where
72 compare (HsIntegral i1 _) (HsIntegral i2 _) = i1 `compare` i2
73 compare (HsIntegral _ _) (HsFractional _ _) = LT
74 compare (HsFractional f1 _) (HsFractional f2 _) = f1 `compare` f2
75 compare (HsFractional f1 _) (HsIntegral _ _) = GT
79 instance Outputable HsLit where
80 -- Use "show" because it puts in appropriate escapes
81 ppr (HsChar c) = pprHsChar c
82 ppr (HsCharPrim c) = pprHsChar c <> char '#'
83 ppr (HsString s) = pprHsString s
84 ppr (HsStringPrim s) = pprHsString s <> char '#'
85 ppr (HsInt i) = integer i
86 ppr (HsInteger i) = integer i
87 ppr (HsRat f _) = rational f
88 ppr (HsFloatPrim f) = rational f <> char '#'
89 ppr (HsDoublePrim d) = rational d <> text "##"
90 ppr (HsIntPrim i) = integer i <> char '#'
91 ppr (HsLitLit s _) = hcat [text "``", ftext s, text "''"]
93 instance Outputable HsOverLit where
94 ppr (HsIntegral i _) = integer i
95 ppr (HsFractional f _) = rational f