on master help sign in
Cosmetic changes
commit2f49151c29372c2be4161f869c40ada63960442d authorEvan Czaplicki <info@elm-lang.org> dateMon 29 Jul 2013, 13:22 (13 years ago) parentab00bce
1 file changed+15−12
compiler/Generate/JavaScript.hs+15−12
compiler/Generate/JavaScript.hs+15−12
@@ -3,7 +3,7 @@ module Generate.JavaScript (showErr, jsModule) where
33import Control.Arrow (first,second)
44import Control.Monad (liftM,(<=<),join,ap)
55import Data.Char (isAlpha,isDigit)
6−import Data.List (intercalate,inits,foldl')
6+import qualified Data.List as List
77import qualified Data.Map as Map
88import qualified Data.Set as Set
99import Data.Either (partitionEithers)
@@ -36,8 +36,10 @@ internalImports =
3636
3737parens s = "(" ++ s ++ ")"
3838brackets s = "{" ++ s ++ "}"
39−jsObj = brackets . intercalate ", "
40−jsList ss = "["++ intercalate "," ss ++"]"
39+commaSep = List.intercalate ", "
40+dotSep = List.intercalate "."
41+jsObj = brackets . commaSep
42+jsList ss = "["++ List.intercalate "," ss ++"]"
4143jsFunc args body = "function(" ++ args ++ "){" ++ indent body ++ "}"
4244assign x e = "\nvar " ++ x ++ " = " ++ e ++ ";"
4345ret e = "\nreturn "++ e ++";"
@@ -61,7 +63,7 @@ jsModule modul =
6163 , globalAssign ("Elm." ++ modName)
6264 (jsFunc "elm" $ makeProgram body foreignImport) ]
6365 where
64− modName = intercalate "." (names modul)
66+ modName = dotSep (names modul)
6567 makeProgram body foreignImport =
6668 concat [ "\nvar " ++ usefulFuncs ++ ";"
6769 , assign "_op" "{}"
@@ -75,8 +77,8 @@ jsModule modul =
7577 , ret (assign' ("elm." ++ modName) "_")
7678 ]
7779 setup names = concatMap (\n -> globalAssign n $ n ++ "||{}") .
78− map (intercalate ".") . drop 2 . inits $ init names
79− usefulFuncs = intercalate ", " (map (uncurry assign') internalImports)
80+ map dotSep . drop 2 . List.inits $ init names
81+ usefulFuncs = commaSep (map (uncurry assign') internalImports)
8082
8183 jsExport x =
8284 if isOp x then "\n_._op['" ++ x ++ "'] = _op['" ++ x ++ "'];"
@@ -106,13 +108,13 @@ jsImport (modul, how) =
106108 where
107109 imprt v = assign' v ("_." ++ v)
108110 def x = imprt $ if isOp x then "_op['" ++ x ++ "']" else deprime x
109− named = if null vs then "" else "\nvar " ++ intercalate ", " (map def vs) ++ ";"
111+ named = if null vs then "" else "\nvar " ++ commaSep (map def vs) ++ ";"
110112 where
111113 include = "\nvar _ = Elm." ++ modul ++ parens "elm" ++ ";" ++ setup modul
112114 setup moduleName = " var " ++ concatMap (++";") (defs ++ [assign' moduleName "_"])
113115 where
114116 defs = map (\n -> assign' n (n ++ "||{}")) (subnames moduleName)
115− subnames = map (intercalate ".") . tail . inits . init . split
117+ subnames = map dotSep . tail . List.inits . init . split
116118 split names = case go [] names of
117119 (name, []) -> [name]
118120 (name, ns) -> name : split ns
@@ -163,12 +165,12 @@ access x e = e ++ "." ++ x
163165makeRecord kvs = record `liftM` collect kvs
164166 where
165167 combine r (k,v) = Map.insertWith (++) k v r
166− collect = liftM (foldl' combine Map.empty) . mapM prep
168+ collect = liftM (List.foldl' combine Map.empty) . mapM prep
167169 prep (k, e) =
168170 do v <- toJS' e
169171 return (k,[v])
170172 fields fs =
171− brackets ("\n "++intercalate ",\n " (map (\(k,v) -> k++":"++v) fs))
173+ brackets ("\n "++List.intercalate ",\n " (map (\(k,v) -> k++":"++v) fs))
172174 hidden = fields . map (second jsList) .
173175 filter (not . null . snd) . Map.toList . Map.map tail
174176 record kvs = fields . (("_", hidden kvs) :) . Map.toList . Map.map head $ kvs
@@ -198,7 +200,7 @@ instance ToJS (Expr t v) where
198200 Record fs -> makeRecord fs
199201 Binop op e1 e2 -> binop op `liftM` toJS' e1 `ap` toJS' e2
200202
201− Lambda p e -> liftM (jsFunc (intercalate ", " args) . ret) (toJS' body)
203+ Lambda p e -> liftM (jsFunc (commaSep args) . ret) (toJS' body)
202204 where
203205 (args, body) = foldr depattern ([], innerBody) (zip patterns [1..])
204206
@@ -239,7 +241,7 @@ jsApp e1 e2 =
239241 as <- mapM toJS' args
240242 return $ case as of
241243 [a] -> f ++ parens a
242− _ -> "A" ++ show (length as) ++ parens (intercalate ", " (f:as))
244+ _ -> "A" ++ show (length as) ++ parens (commaSep (f:as))
243245 where
244246 (func, args) = go [e2] e1
245247 go args e =
@@ -315,6 +317,7 @@ jsCons e1 e2 = "_L.Cons(" ++ e1 ++ "," ++ e2 ++ ")"
315317jsRange e1 e2 = "_L.range" ++ parens (e1 ++ "," ++ e2)
316318jsCompare e1 e2 op = parens ("_N.cmp(" ++ e1 ++ "," ++ e2 ++ ").ctor" ++ op)
317319
320+-- todo: this is incorrect. Operators come in prefixed by their module now.
318321binop (o:p) e1 e2
319322 | isAlpha o || '_' == o = (o:p) ++ parens e1 ++ parens e2
320323 | otherwise =