on master help sign in
javascript: added proper string translation
commitede6fc4f62ae6b407d223f33bc98d5a7f34383d0 authorraichoo <raichoo@googlemail.com> dateSat 15 Feb 2014, 18:52 (13 years ago) parentde394f4
1 file changed+33−11
src/IRTS/CodegenJavaScript.hs+33−11
src/IRTS/CodegenJavaScript.hs+33−11
@@ -14,6 +14,7 @@ import Util.System
1414import Control.Arrow
1515import Control.Applicative ((<$>), (<*>), pure)
1616import Data.Char
17+import Numeric
1718import Data.List
1819import Data.Maybe
1920import System.IO
@@ -71,7 +72,6 @@ data JS = JSRaw String
7172 | JSFalse
7273 | JSArray [JS]
7374 | JSString String
74− | JSChar String
7575 | JSNum JSNum
7676 | JSAssign JS JS
7777 | JSAlloc String (Maybe JS)
@@ -162,10 +162,7 @@ compileJS (JSArray elems) =
162162 "[" ++ intercalate "," (map compileJS elems) ++ "]"
163163
164164compileJS (JSString str) =
165− show str
166−
167−compileJS (JSChar chr) =
168− chr
165+ "\"" ++ str ++ "\""
169166
170167compileJS (JSNum num)
171168 | JSInt i <- num = show i
@@ -420,7 +417,6 @@ removeAllocations js = transformJS removeAllocations js
420417isJSConstant :: JS -> Bool
421418isJSConstant js
422419 | JSString _ <- js = True
423− | JSChar _ <- js = True
424420 | JSNum _ <- js = True
425421 | JSType _ <- js = True
426422 | JSNull <- js = True
@@ -1503,11 +1499,8 @@ translateSpecialName name
15031499translateConstant :: Const -> JS
15041500translateConstant (I i) = JSNum (JSInt i)
15051501translateConstant (Fl f) = JSNum (JSFloat f)
1506−translateConstant (Ch '\DEL') = JSChar "'\\u007F'"
1507−translateConstant (Ch '\a') = JSChar "'\\u0007'"
1508−translateConstant (Ch '\SO') = JSChar "'\\u000E'"
1509−translateConstant (Ch c) = JSString [c]
1510−translateConstant (Str s) = JSString s
1502+translateConstant (Ch c) = JSString $ translateChar c
1503+translateConstant (Str s) = JSString $ concatMap translateChar s
15111504translateConstant (AType (ATInt ITNative)) = JSType JSIntTy
15121505translateConstant StrType = JSType JSStringTy
15131506translateConstant (AType (ATInt ITBig)) = JSType JSIntegerTy
@@ -1520,6 +1513,35 @@ translateConstant c =
15201513 jsError $ "Unimplemented Constant: " ++ show c
15211514
15221515
1516+translateChar :: Char -> String
1517+translateChar ch
1518+ | '\a' <- ch = "\\u0007"
1519+ | '\b' <- ch = "\\b"
1520+ | '\f' <- ch = "\\f"
1521+ | '\n' <- ch = "\\n"
1522+ | '\r' <- ch = "\\r"
1523+ | '\t' <- ch = "\\t"
1524+ | '\v' <- ch = "\\v"
1525+ | '\SO' <- ch = "\\u000E"
1526+ | '\DEL' <- ch = "\\u007F"
1527+ | '\\' <- ch = "\\\\"
1528+ | '\"' <- ch = "\\\""
1529+ | '\'' <- ch = "\\\'"
1530+ | ch `elem` asciiTab = "\\u00" ++ fill (showIntAtBase 16 intToDigit (ord ch) "")
1531+ | otherwise = [ch]
1532+ where
1533+ fill :: String -> String
1534+ fill s = if length s == 1
1535+ then '0' : s
1536+ else s
1537+
1538+ asciiTab =
1539+ ['\NUL', '\SOH', '\STX', '\ETX', '\EOT', '\ENQ', '\ACK', '\BEL',
1540+ '\BS', '\HT', '\LF', '\VT', '\FF', '\CR', '\SO', '\SI',
1541+ '\DLE', '\DC1', '\DC2', '\DC3', '\DC4', '\NAK', '\SYN', '\ETB',
1542+ '\CAN', '\EM', '\SUB', '\ESC', '\FS', '\GS', '\RS', '\US']
1543+
1544+
15231545translateDeclaration :: (String, SDecl) -> [JS]
15241546translateDeclaration (path, SFun name params stackSize body)
15251547 | (MN _ ap) <- name