[project @ 1997-08-25 22:24:51 by sof]
[ghc-hetmet.git] / ghc / compiler / basicTypes / Literal.lhs
index 1330a3d..738dcf1 100644 (file)
@@ -16,9 +16,10 @@ module Literal (
     ) where
 
 IMP_Ubiq(){-uitous-}
+IMPORT_1_3(Ratio)
 
 -- friends:
-import PrimRep         ( PrimRep(..) ) -- non-abstract
+import PrimRep         ( PrimRep(..), ppPrimRep ) -- non-abstract
 import TysPrim         ( getPrimRepInfo, 
                          addrPrimTy, intPrimTy, floatPrimTy,
                          doublePrimTy, charPrimTy, wordPrimTy )
@@ -27,8 +28,11 @@ import TysPrim               ( getPrimRepInfo,
 import CStrings                ( stringToC, charToC, charToEasyHaskell )
 import TysWiredIn      ( stringTy )
 import Pretty          -- pretty-printing stuff
-import PprStyle                ( PprStyle(..), codeStyle )
-import Util            ( thenCmp, panic )
+import Outputable      ( PprStyle(..), codeStyle, ifaceStyle, Outputable(..) )
+import Util            ( thenCmp, panic, pprPanic, Ord3(..) )
+#if __GLASGOW_HASKELL__ >= 202
+import Type
+#endif
 \end{code}
 
 So-called @Literals@ are {\em either}:
@@ -47,17 +51,24 @@ function applications, etc., etc., has not yet been done.
 data Literal
   = MachChar   Char
   | MachStr    FAST_STRING
+
   | MachAddr   Integer -- whatever this machine thinks is a "pointer"
+
   | MachInt    Integer -- for the numeric types, these are
                Bool    -- True <=> signed (Int#); False <=> unsigned (Word#)
+
   | MachFloat  Rational
   | MachDouble Rational
+
   | MachLitLit  FAST_STRING
                PrimRep
 
-  | NoRepStr       FAST_STRING -- the uncommitted ones
-  | NoRepInteger    Integer  Type{-save what we learned in the typechecker-}
-  | NoRepRational   Rational Type{-ditto-}
+  | NoRepStr       FAST_STRING
+  | NoRepInteger    Integer  Type      -- This Type is always Integer
+  | NoRepRational   Rational Type      -- This Type is always Rational
+                       -- We keep these Types in the literal because Rational isn't
+                       -- (currently) wired in, so we can't conjure up its type out of
+                       -- thin air.    Integer is, so the type here is really redundant.
 
   -- deriving (Eq, Ord): no, don't want to compare Types
   -- The Ord is needed for the FiniteMap used in the lookForConstructor
@@ -159,9 +170,14 @@ literalPrimRep (NoRepStr _)           = panic "literalPrimRep:NoRepString"
 
 The boring old output stuff:
 \begin{code}
-ppCast :: PprStyle -> FAST_STRING -> Pretty
-ppCast PprForC cast = ppPStr cast
-ppCast _       _    = ppNil
+ppCast :: PprStyle -> FAST_STRING -> Doc
+ppCast PprForC cast = ptext cast
+ppCast _       _    = empty
+
+-- MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
+--     exceptions: MachFloat and MachAddr get an initial keyword prefix
+--
+-- NoRep things get an initial keyword prefix (e.g. _integer_ 3)
 
 instance Outputable Literal where
     ppr sty (MachChar ch)
@@ -170,64 +186,54 @@ instance Outputable Literal where
              = case sty of
                  PprForC       -> charToC ch
                  PprForAsm _ _ -> charToC ch
-                 PprUnfolding  -> charToEasyHaskell ch
+                 PprInterface  -> charToEasyHaskell ch
                  _             -> [ch]
        in
-       ppBeside (ppBesides [ppCast sty SLIT("(C_)"), ppChar '\'', ppStr char_encoding, ppChar '\''])
-                (if_ubxd sty)
+       hcat [ppCast sty SLIT("(C_)"), char '\'', text char_encoding, char '\'']
 
     ppr sty (MachStr s)
-      = ppBeside (if codeStyle sty
-                 then ppBesides [ppChar '"', ppStr (stringToC (_UNPK_ s)), ppChar '"']
-                 else ppStr (show (_UNPK_ s)))
-                (if_ubxd sty)
+      | codeStyle sty = hcat [char '"', text (stringToC (_UNPK_ s)), char '"']
+      | otherwise     = text (show (_UNPK_ s))
+
+    ppr sty lit@(NoRepStr s)
+      | codeStyle sty = pprPanic "NoRep in code style" (ppr PprDebug lit)
+      | otherwise     = hcat [ptext SLIT("_string_ "), text (show (_UNPK_ s))]
 
-    ppr sty (MachAddr p) = ppBesides [ppCast sty SLIT("(void*)"), ppInteger p, if_ubxd sty]
     ppr sty (MachInt i signed)
-      | codeStyle sty
-      && ((signed     && (i >= toInteger minInt && i <= toInteger maxInt))
-       || (not signed && (i >= toInteger 0      && i <= toInteger maxInt)))
-      -- ToDo: Think about these ranges!
-      = ppBesides [ppInteger i, if_ubxd sty]
-
-      | not (codeStyle sty) -- we'd prefer the code to the error message
-      = ppBesides [ppInteger i, if_ubxd sty]
-
-      | otherwise
-      = error ("ERROR: Int " ++ show i ++ " out of range [" ++
-               show range_min ++ " .. " ++ show maxInt ++ "]\n")
+      | codeStyle sty && out_of_range
+      = panic ("ERROR: Int " ++ show i ++ " out of range [" ++
+               show range_min ++ " .. " ++ show range_max ++ "]\n")
+
+      | otherwise = integer i
+
       where
        range_min = if signed then minInt else 0
+       range_max = maxInt
+        out_of_range = not (i >= toInteger range_min && i <= toInteger range_max)
 
-    ppr sty (MachFloat f)  = ppBesides [ppCast sty SLIT("(StgFloat)"), ppRational f, if_ubxd sty]
-    ppr sty (MachDouble d) = ppBesides [ppRational d, if_ubxd sty, if_ubxd sty]
+    ppr sty (MachFloat f)  
+       | codeStyle sty = hcat [ppCast sty SLIT("(StgFloat)"), rational f]
+       | otherwise     = hcat [ptext SLIT("_float_ "), rational f]
 
-    ppr sty (NoRepInteger i _)
-      | codeStyle sty  = ppInteger i
-      | ufStyle sty    = ppCat [ppStr "_NOREP_I_", ppInteger i]
-      | otherwise      = ppBesides [ppInteger i, ppChar 'I']
+    ppr sty (MachDouble d) = rational d
 
-    ppr sty (NoRepRational r _)
-      | ufStyle sty    = ppCat [ppStr "_NOREP_R_", ppInteger (numerator r), ppInteger (denominator r)]
-      | codeStyle sty = panic "ppr.ForC.NoRepRational"
-      | otherwise     = ppBesides [ppRational r,  ppChar 'R']
+    ppr sty (MachAddr p) 
+       | codeStyle sty = hcat [ppCast sty SLIT("(void*)"), integer p]
+       | otherwise     = hcat [ptext SLIT("_addr_ "), integer p]
 
-    ppr sty (NoRepStr s)
-      | codeStyle sty = ppBesides [ppStr (show (_UNPK_ s))]
-      | ufStyle   sty = ppCat [ppStr "_NOREP_S_", ppStr (show (_UNPK_ s))]
-      | otherwise     = ppBesides [ppStr (show (_UNPK_ s)), ppChar 'S']
-
-    ppr sty (MachLitLit s k)
-      | codeStyle sty = ppPStr s
-      | ufStyle   sty = ppBesides [ppStr "``", ppPStr s, ppStr "'' _K_ ", ppr sty k]
-      | otherwise     = ppBesides [ppStr "``", ppPStr s, ppStr "''"]
+    ppr sty lit@(NoRepInteger i _)
+      | codeStyle sty  = pprPanic "NoRep in code style" (ppr PprDebug lit)
+      | otherwise      = hsep [ptext SLIT("_integer_ "), integer i]
 
-ufStyle PprUnfolding = True
-ufStyle _           = False
+    ppr sty lit@(NoRepRational r _)
+      | codeStyle sty = pprPanic "NoRep in code style" (ppr PprDebug lit)
+      | otherwise     = hsep [ptext SLIT("_rational_ "), integer (numerator r), integer (denominator r)]
 
-if_ubxd sty = if codeStyle sty then ppNil else ppChar '#'
+    ppr sty (MachLitLit s k)
+      | codeStyle  sty = ptext s
+      | otherwise      = hcat [ptext SLIT("_litlit_ "), ppPrimRep k, char ' ', text (show (_UNPK_ s))]
 
 showLiteral :: PprStyle -> Literal -> String
-
-showLiteral sty lit = ppShow 80 (ppr sty lit)
+showLiteral sty lit = show (ppr sty lit)
 \end{code}
+