on master help sign in
Merge pull request #669 from Melvar/showstring

Change Show instances for Char and String to match their literals

commit4f1b6042951a833862a28833a66982650f48325b authorEdwin Brady <edwin.brady@gmail.com> dateMon 25 Nov 2013, 11:10 (13 years ago) parent7514bd0ea3b20a
15 files changed+59−23
idris.cabal+1−1 libs/prelude/Prelude.idr+39−2 src/IRTS/CodegenC.hs+1−1 src/IRTS/Java/JTypes.hs+1 test/reg005/reg005.idr+1−1 test/reg024/reg024.idr+1−1 test/reg025/reg025.idr+2−2 test/test003/expected+3−3 test/test005/expected+2−2 test/test008/test008.idr+1−1 test/test011/expected+3−3 test/test020/expected+1−1 test/test021/expected+1−3 test/test028/test028.idr+1−1 test/test034/expected+1−1
idris.cabal+1−1
@@ -395,7 +395,7 @@ Executable idris
395395 , directory >= 1.2
396396 , filepath
397397 , haskeline >= 0.7
398− , language-java >= 0.2.2
398+ , language-java >= 0.2.6
399399 , mtl
400400 --, parsec >= 3
401401 , parsers == 0.9
libs/prelude/Prelude.idr+39−2
@@ -40,11 +40,48 @@ instance Show Integer where
4040instance Show Float where
4141 show = prim__floatToStr
4242
43+asciiTab : Vect 32 String
44+asciiTab = ["NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
45+ "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
46+ "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
47+ "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US"]
48+
49+firstCharIs : (Char -> Bool) -> String -> Bool
50+firstCharIs p s with (strM s)
51+ firstCharIs p "" | StrNil = False
52+ firstCharIs p (strCons c cs) | StrCons c cs = p c
53+
54+protectEsc : (Char -> Bool) -> String -> String -> String
55+protectEsc p f s = f ++ (if firstCharIs p s then "\\&" else "") ++ s
56+
57+showLitChar : Char -> String -> String
58+showLitChar '\a' = ("\\a" ++)
59+showLitChar '\b' = ("\\b" ++)
60+showLitChar '\f' = ("\\f" ++)
61+showLitChar '\n' = ("\\n" ++)
62+showLitChar '\r' = ("\\r" ++)
63+showLitChar '\t' = ("\\t" ++)
64+showLitChar '\v' = ("\\v" ++)
65+showLitChar '\SO' = protectEsc (== 'H') "\\SO"
66+showLitChar '\DEL' = ("\\DEL" ++)
67+showLitChar '\\' = ("\\\\" ++)
68+showLitChar c = case (integerToFin (cast (cast {to=Int} c)) 32) of
69+ Just k => strCons '\\' . ((index k asciiTab) ++)
70+ Nothing => if (c > '\DEL')
71+ then strCons '\\' . protectEsc isDigit (show (cast {to=Int} c))
72+ else strCons c
73+
74+showLitString : List Char -> String -> String
75+showLitString [] = id
76+showLitString ('"'::cs) = ("\\\"" ++) . showLitString cs
77+showLitString (c ::cs) = showLitChar c . showLitString cs
78+
4379instance Show Char where
44− show x = strCons x ""
80+ show '\'' = "'\\''"
81+ show c = strCons '\'' (showLitChar c "'")
4582
4683instance Show String where
47− show = id
84+ show cs = strCons '"' (showLitString (cast cs) "\"")
4885
4986instance Show Nat where
5087 show n = show (the Integer (cast n))
src/IRTS/CodegenC.hs+1−1
@@ -201,7 +201,7 @@ bcc i (CONSTCASE r code def)
201201 indent i ++ "if (GETINT(" ++ v ++ ") == " ++ show b ++ ") {\n"
202202 ++ concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
203203 iCase v (Ch b, bc) =
204− indent i ++ "if (GETINT(" ++ v ++ ") == " ++ show b ++ ") {\n"
204+ indent i ++ "if (GETINT(" ++ v ++ ") == " ++ show (fromEnum b) ++ ") {\n"
205205 ++ concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
206206
207207 showCase i (t, bc) = indent i ++ "case " ++ show t ++ ":\n"
src/IRTS/Java/JTypes.hs+1
@@ -218,6 +218,7 @@ opName x
218218 | (LTrunc _ to) <- x = "LTrunc" ++ (suffixFor to)
219219 | (LFloatInt to) <- x = "LFloatInt" ++ (suffixFor to)
220220 | (LStrInt to) <- x = "LStrInt" ++ (suffixFor to)
221+ | (LChInt to) <- x = "LChInt" ++ (suffixFor to)
221222 | otherwise = takeWhile ((/=) ' ') $ show x
222223 where
223224 suffixFor (ITFixed nt) = show nt
test/reg005/reg005.idr+1−1
@@ -30,7 +30,7 @@ compress xs with (rle xs)
3030 compress Nil | REnd = ""
3131 compress (rep (S n) x ++ xs) | RChar _ _ rs
3232 = let ni : Integer = cast (S n) in
33− show ni ++ show x ++ compress xs
33+ show ni ++ strCons x (compress xs)
3434
3535compressString : String -> String
3636compressString xs = compress (fromList (unpack xs))
test/reg024/reg024.idr+1−1
@@ -28,4 +28,4 @@ test : String
2828test = printf "The %s is %d" "answer" 42
2929
3030main : IO ()
31−main = print test
31+main = putStrLn test
test/reg025/reg025.idr+2−2
@@ -70,6 +70,6 @@ emptyCell (MkBoard rs) =
7070main : IO ()
7171main =
7272 case emptyCell (emptyBoard {n=0}) of
73− Left _ => print "l"
74− Right _ => print "r"
73+ Left _ => putStrLn "l"
74+ Right _ => putStrLn "r"
7575
test/test003/expected+3−3
@@ -1,7 +1,7 @@
11./test003a.lidr:1:0:Program line next to comment
2−a
3−d
2+'a'
3+'d'
44zabcdefg
55gfedcba
6−[g, f, e, d, c, b, a]
6+['g', 'f', 'e', 'd', 'c', 'b', 'a']
77abcdefg
test/test005/expected+2−2
@@ -1,6 +1,6 @@
118
221
3−(abc, 123)
4−(abc, 123)
3+("abc", "123")
4+("abc", "123")
55([1, 2], [3, 4, 5])
66([1, 2], [3, 4, 5])
test/test008/test008.idr+1−1
@@ -18,7 +18,7 @@ ioVals = do { return ("First", "second") }
1818
1919main : IO ()
2020main = do (a, b) <- ioVals
21− putStr (show a ++ " and " ++ show b ++ "? ")
21+ putStr (a ++ " and " ++ b ++ "? ")
2222 let x = "bar"
2323 putStrLn (show (getVal x 7 testlist))
2424 let ((y, z) :: _) = testlist
test/test011/expected+3−3
@@ -1,5 +1,5 @@
1−foo
2−Fred
1+"foo"
2+"Fred"
33[1, 2, 3]
4−[b, a]
4+["b", "a"]
5525
test/test020/expected+1−1
@@ -11,4 +11,4 @@ Specifically:
1111 List a
1212
1313[3, 2, 1]
14−Number 42
14+"Number 42"
test/test021/expected+1−3
@@ -1,6 +1,4 @@
1−[HELLO!!!
2−, WORLD!!!
3−, ]
1+["HELLO!!!\n", "WORLD!!!\n", ""]
423
5315
64Answer: 99
test/test028/test028.idr+1−1
@@ -2,4 +2,4 @@
22module Main
33--
44main : IO ()
5−main = print "hello, world!"
5+main = putStrLn "hello, world!"
test/test034/expected+1−1
@@ -1,5 +1,5 @@
11prim__floatToStr 0.0
2−0.0
2+"0.0"
33prim__strToFloat "0.0"
440.0
55prim__addFloat 1.0 1.0