on master help sign in
Switch back to putting let and case expressions in closures.

In the former case it is actually necessary, in the latter, it partially fixes a mysterious bug with Dict.diff. Also stop using the LetBoundVars module.

commit5f1e3bd69609e3b4e2c17a69607af8d8ac888a5a authorevancz <info@elm-lang.org> dateTue 21 May 2013, 22:28 (13 years ago) parentce51a0e
2 files changed+31−35
Elm.cabal−2 compiler/Gen/CompileToJS.hs+31−33
Elm.cabal−2
@@ -41,7 +41,6 @@ Library
4141 ExtractNoscript,
4242 GenerateHtml,
4343 Guid,
44− LetBoundVars,
4544 Libraries,
4645 LoadLibraries,
4746 Optimize,
@@ -97,7 +96,6 @@ Executable elm
9796 ExtractNoscript,
9897 GenerateHtml,
9998 Guid,
100− LetBoundVars,
10199 Libraries,
102100 LoadLibraries,
103101 Optimize,
compiler/Gen/CompileToJS.hs+31−33
@@ -13,7 +13,6 @@ import Context
1313import Rename (derename)
1414import Cases
1515import Guid
16−import LetBoundVars
1716import Rename (deprime)
1817import Types.Types ( Type(RecordT) )
1918
@@ -24,7 +23,7 @@ showErr err = globalAssign "Elm.Main" (jsFunc "elm" body)
2423 \return { main : T.text(T.monospace(" ++ msg ++ ")) };"
2524
2625indent = concatMap f
27− where f '\n' = "\n "
26+ where f '\n' = "\n "
2827 f c = [c]
2928
3029internalImports =
@@ -122,9 +121,8 @@ jsImport (modul, how) =
122121
123122stmtsToJS :: [Statement] -> String
124123stmtsToJS stmts = run $ do program <- mapM toJS (sortBy cmpStmt stmts)
125− return (vars ++ concat program)
124+ return (concat program)
126125 where
127− vars = "\nvar " ++ intercalate ", " ("e":"case0":letBoundVars stmts) ++ ";"
128126 cmpStmt s1 s2 = compare (valueOf s1) (valueOf s2)
129127 valueOf s = case s of
130128 Datatype _ _ _ -> 1
@@ -142,9 +140,10 @@ class ToJS a where
142140instance ToJS Def where
143141 toJS (FnDef x [] e) = assign' x `liftM` toJS' e
144142 toJS (FnDef f as e) = (assign' f . wrapper . func) `liftM` toJS' e
145− where func body = jsFunc (intercalate ", " as) (ret body)
146− wrapper e | length as == 1 = e
147− | otherwise = 'F' : show (length as) ++ parens e
143+ where
144+ func body = jsFunc (intercalate ", " as) (ret body)
145+ wrapper e | length as == 1 = e
146+ | otherwise = 'F' : show (length as) ++ parens e
148147 toJS (OpDef op a1 a2 e) =
149148 do body <- toJS' e
150149 let func = "F2" ++ parens (jsFunc (a1 ++ ", " ++ a2) (ret body))
@@ -161,7 +160,7 @@ instance ToJS Statement where
161160 Data (derename name) (map (noContext . Var) vars)
162161 ImportEvent js base elm _ ->
163162 do v <- toJS' base
164− return $ concat [ "\nvar " ++ elm ++ "=Elm.Signal.constant(" ++ v ++ ");"
163+ return $ concat [ "\nvar " ++ elm ++ "=Elm.Signal(elm).constant(" ++ v ++ ");"
165164 , "\ndocument.addEventListener('", js
166165 , "_' + elm.id, function(e) { elm.notify(", elm
167166 , ".id, e.value); });" ]
@@ -268,9 +267,10 @@ multiIfToJS span ps =
268267 return (b' ++ " ? " ++ e')
269268
270269jsLet defs e' = do ds <- jsDefs defs
271− e <- toJS' e'
272− return $ parens (intercalate ", " ds ++ ", " ++ e)
270+ e <- toJS' e'
271+ return $ jsFunc "" (combineDefs ds ++ ret e) ++ "()"
273272 where
273+ combineDefs = concatMap (\def -> "\nvar " ++ def ++ ";")
274274 jsDefs defs = mapM toJS (sortBy f defs)
275275 f a b = compare (valueOf a) (valueOf b)
276276 valueOf (FnDef _ args _) = min 1 (length args)
@@ -279,39 +279,37 @@ jsLet defs e' = do ds <- jsDefs defs
279279caseToJS span e ps = do
280280 match <- caseToMatch ps
281281 e' <- toJS' e
282− (match',stmt) <- case (match,e) of
283− (Match name _ _, C _ _ (Var x)) ->
284− return (matchSubst [(name,x)] match, Nothing)
285− (Match name _ _, _) ->
286− return (match, Just (name ++ " = " ++ e'))
287− _ -> return (match, Nothing)
282+ let (match',stmt) = case (match,e) of
283+ (Match name _ _, C _ _ (Var x)) -> (matchSubst [(name,x)] match, Nothing)
284+ (Match name _ _, _) -> (match, Just (name ++ " = " ++ e'))
285+ _ -> (match, Nothing)
288286 matches <- matchToJS span match'
289− return $ case stmt of
290− Nothing -> matches
291− Just exp -> parens (exp ++ ", " ++ matches)
287+ let enclose body = "function(){ " ++ body ++ " }()"
288+ return . enclose $ case stmt of
289+ Nothing -> matches
290+ Just exp -> "\nvar " ++ exp ++ ";" ++ matches
292291
293292matchToJS span match =
294− let sqnc e1 e2 = parens ("e=" ++ e1 ++ ":null,e!==null?e:" ++ e2)
295− in case match of
293+ case match of
296294 Match name clauses def ->
297− do cases <- intercalate ":" `liftM` mapM (clauseToJS span name) clauses
295+ do cases <- concat `liftM` mapM (clauseToJS span name) clauses
298296 finally <- matchToJS span def
299− return (sqnc cases finally)
297+ return $ concat [ "\nswitch(", name, ".ctor){", indent cases, "\n}", finally ]
300298 Fail -> return ("_E.Case" ++ parens (quoted (show span)))
301− Break -> return "null"
302− Other e -> toJS' e
303− Seq ms -> foldr1 sqnc `liftM` mapM (matchToJS span) (dropEnd [] ms)
304− where dropEnd acc [] = acc
305− dropEnd acc (m:ms) =
306− case m of
307− Other _ -> acc ++ [m]
308− _ -> dropEnd (acc ++ [m]) ms
309−
299+ Break -> return "break;"
300+ Other e -> ret `liftM` toJS' e
301+ Seq ms -> concat `liftM` mapM (matchToJS span) (dropEnd [] ms)
302+ where
303+ dropEnd acc [] = acc
304+ dropEnd acc (m:ms) =
305+ case m of
306+ Other _ -> acc ++ [m]
307+ _ -> dropEnd (acc ++ [m]) ms
310308
311309clauseToJS span var (Clause name vars e) = do
312310 let vars' = map (\n -> var ++ "._" ++ show n) [0..]
313311 s <- matchToJS span $ matchSubst (zip vars vars') e
314− return $ concat [ var, ".ctor===", quoted name, "?", s ]
312+ return $ concat [ "\ncase ", quoted name, ":", indent s ]
315313
316314jsNil = "_L.Nil"
317315jsCons e1 e2 = "_L.Cons(" ++ e1 ++ "," ++ e2 ++ ")"