on master help sign in
javascript: added proper string translation
ede6fc4raichoo
src/IRTS/CodegenJavaScript.hs+33−11
@@ -14,6 +14,7 @@ import Util.System
14import Control.Arrow
14import Control.Arrow
15import Control.Applicative ((<$>), (<*>), pure)
15import Control.Applicative ((<$>), (<*>), pure)
16import Data.Char
16import Data.Char
17+import Numeric
17import Data.List
18import Data.List
18import Data.Maybe
19import Data.Maybe
19import System.IO
20import System.IO
@@ -71,7 +72,6 @@ data JS = JSRaw String
71 | JSFalse
72 | JSFalse
72 | JSArray [JS]
73 | JSArray [JS]
73 | JSString String
74 | JSString String
74− | JSChar String
75 | JSNum JSNum
75 | JSNum JSNum
76 | JSAssign JS JS
76 | JSAssign JS JS
77 | JSAlloc String (Maybe JS)
77 | JSAlloc String (Maybe JS)
@@ -162,10 +162,7 @@ compileJS (JSArray elems) =
162 "[" ++ intercalate "," (map compileJS elems) ++ "]"
162 "[" ++ intercalate "," (map compileJS elems) ++ "]"
163
163
164compileJS (JSString str) =
164compileJS (JSString str) =
165− show str
165+ "\"" ++ str ++ "\""
166−
167−compileJS (JSChar chr) =
168− chr
169
166
170compileJS (JSNum num)
167compileJS (JSNum num)
171 | JSInt i <- num = show i
168 | JSInt i <- num = show i
@@ -420,7 +417,6 @@ removeAllocations js = transformJS removeAllocations js
420isJSConstant :: JS -> Bool
417isJSConstant :: JS -> Bool
421isJSConstant js
418isJSConstant js
422 | JSString _ <- js = True
419 | JSString _ <- js = True
423− | JSChar _ <- js = True
424 | JSNum _ <- js = True
420 | JSNum _ <- js = True
425 | JSType _ <- js = True
421 | JSType _ <- js = True
426 | JSNull <- js = True
422 | JSNull <- js = True
@@ -1503,11 +1499,8 @@ translateSpecialName name
1503translateConstant :: Const -> JS
1499translateConstant :: Const -> JS
1504translateConstant (I i) = JSNum (JSInt i)
1500translateConstant (I i) = JSNum (JSInt i)
1505translateConstant (Fl f) = JSNum (JSFloat f)
1501translateConstant (Fl f) = JSNum (JSFloat f)
1506−translateConstant (Ch '\DEL') = JSChar "'\\u007F'"
1502+translateConstant (Ch c) = JSString $ translateChar c
1507−translateConstant (Ch '\a') = JSChar "'\\u0007'"
1508−translateConstant (Ch '\SO') = JSChar "'\\u000E'"
1509−translateConstant (Ch c) = JSString [c]
1510−translateConstant (Str s) = JSString s
1503+translateConstant (Str s) = JSString $ concatMap translateChar s
1511translateConstant (AType (ATInt ITNative)) = JSType JSIntTy
1504translateConstant (AType (ATInt ITNative)) = JSType JSIntTy
1512translateConstant StrType = JSType JSStringTy
1505translateConstant StrType = JSType JSStringTy
1513translateConstant (AType (ATInt ITBig)) = JSType JSIntegerTy
1506translateConstant (AType (ATInt ITBig)) = JSType JSIntegerTy
@@ -1520,6 +1513,35 @@ translateConstant c =
1520 jsError $ "Unimplemented Constant: " ++ show c
1513 jsError $ "Unimplemented Constant: " ++ show c
1521
1514
1522
1515
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+
1523translateDeclaration :: (String, SDecl) -> [JS]
1545translateDeclaration :: (String, SDecl) -> [JS]
1524translateDeclaration (path, SFun name params stackSize body)
1546translateDeclaration (path, SFun name params stackSize body)
1525 | (MN _ ap) <- name
1547 | (MN _ ap) <- name