CStrings.lhs 3.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
This module deals with printing (a) C string literals and (b) C labels.

\begin{code}
#include "HsVersions.h"

module CStrings(

	cSEP,
	pp_cSEP,

	identToC, modnameToC,
	stringToC, charToC,
	charToEasyHaskell

  ) where

CHK_Ubiq() -- debugging consistency check

import Pretty
import Unpretty( uppChar )
\end{code}


\begin{verbatim}
_ is the main separator

orig		becomes
****		*******
_		Zu
'		Zq (etc for ops ??)
<funny char>	Z[hex-digit][hex-digit]
Prelude<x>	ZP<x>
<std class>	ZC<?>
<std tycon>	ZT<?>
\end{verbatim}

\begin{code}
cSEP    = SLIT("_")	-- official C separator
pp_cSEP = uppChar '_'

identToC    :: FAST_STRING -> Pretty
modnameToC  :: FAST_STRING -> FAST_STRING
stringToC   :: String -> String
charToC, charToEasyHaskell :: Char -> String

-- stringToC: the hassle is what to do w/ strings like "ESC 0"...

stringToC ""  = ""
stringToC [c] = charToC c
stringToC (c:cs)
    -- if we have something "octifiable" in "c", we'd better "octify"
    -- the rest of the string, too.
  = if (c < ' ' || c > '~')
    then (charToC c) ++ (concat (map char_to_C cs))
    else (charToC c) ++ (stringToC cs)
  where
    char_to_C c | c == '\n' = "\\n"	-- use C escapes when we can
		| c == '\a' = "\\a"
		| c == '\b' = "\\b"	-- ToDo: chk some of these...
		| c == '\r' = "\\r"
		| c == '\t' = "\\t"
		| c == '\f' = "\\f"
		| c == '\v' = "\\v"
		| otherwise = '\\' : (octify (ord c))

charToC c = if (c >= ' ' && c <= '~')	-- non-portable...
	    then case c of
		  '\'' -> "\\'"
		  '\\' -> "\\\\"
		  '"'  -> "\\\""
		  '\n' -> "\\n"
		  '\a' -> "\\a"
		  '\b' -> "\\b"
		  '\r' -> "\\r"
		  '\t' -> "\\t"
		  '\f' -> "\\f"
		  '\v' -> "\\v"
		  _    -> [c]
	    else '\\' : (octify (ord c))

-- really: charToSimpleHaskell

charToEasyHaskell c
  = if (c >= 'a' && c <= 'z')
    || (c >= 'A' && c <= 'Z')
    || (c >= '0' && c <= '9')
    then [c]
    else case c of
	  _    -> '\\' : 'o' : (octify (ord c))

octify :: Int -> String
octify n
  = if n < 8 then
	[chr (n + ord '0')]
    else
	octify (n `quot` 8) ++ [chr (n `rem` 8 + ord '0')]

identToC ps
  = let
	str = _UNPK_ ps
    in
    ppBeside
	(case str of
	   's':'t':'d':_ -> -- avoid "stdin", "stdout", and "stderr"...
			    ppChar 'Z'
	   _	         -> ppNil)

	(if (all isAlphanum str) -- we gamble that this test will succeed...
	 then ppPStr ps
	 else ppIntersperse ppNil (map char_to_c str))
  where
    char_to_c 'Z'  = ppPStr SLIT("ZZ")
    char_to_c '&'  = ppPStr SLIT("Za")
    char_to_c '|'  = ppPStr SLIT("Zb")
    char_to_c ':'  = ppPStr SLIT("Zc")
    char_to_c '/'  = ppPStr SLIT("Zd")
    char_to_c '='  = ppPStr SLIT("Ze")
    char_to_c '>'  = ppPStr SLIT("Zg")
    char_to_c '#'  = ppPStr SLIT("Zh")
    char_to_c '<'  = ppPStr SLIT("Zl")
    char_to_c '-'  = ppPStr SLIT("Zm")
    char_to_c '!'  = ppPStr SLIT("Zn")
    char_to_c '.'  = ppPStr SLIT("Zo")
    char_to_c '+'  = ppPStr SLIT("Zp")
    char_to_c '\'' = ppPStr SLIT("Zq")
    char_to_c '*'  = ppPStr SLIT("Zt")
    char_to_c '_'  = ppPStr SLIT("Zu")

    char_to_c c    = if isAlphanum c
		     then ppChar c
		     else ppBeside (ppChar 'Z') (ppInt (ord c))
\end{code}

For \tr{modnameToC}, we really only have to worry about \tr{'}s (quote
chars) in the name.  Rare.
\begin{code}
modnameToC ps
  = let
	str = _UNPK_ ps
    in
    if not (any quote_here str) then
	ps
    else
	_PK_ (concat (map char_to_c str))
  where
    quote_here '\'' = True
    quote_here _    = False

    char_to_c c
      = if isAlphanum c then [c] else 'Z' : (show (ord c))
\end{code}