@@ -138,8 +138,8 @@ class ToJS a where
138138 toJS :: a -> GuidCounter String
139139
140140instance ToJS Def where
141− toJS (FnDef x [] e) = assign' x `liftM` toJS' e
142− toJS (FnDef f as e) = (assign' f . wrapper . func) `liftM` toJS' e
141+ toJS (FnDef x [] e) = assign x `liftM` toJS' e
142+ toJS (FnDef f as e) = (assign f . wrapper . func) `liftM` toJS' e
143143 where
144144 func body = jsFunc (intercalate ", " as) (ret body)
145145 wrapper e | length as == 1 = e
@@ -152,7 +152,7 @@ instance ToJS Def where
152152instance ToJS Statement where
153153 toJS stmt =
154154 case stmt of
155− Definition d -> (\asgn -> "\n" ++ asgn ++ ";") `liftM` toJS d
155+ Definition d -> toJS d
156156 Datatype _ _ tcs -> concat `liftM` mapM (toJS . toDef) tcs
157157 where toDef (name,args) =
158158 let vars = map (('a':) . show) [1..length args] in
@@ -268,9 +268,8 @@ multiIfToJS span ps =
268268
269269jsLet defs e' = do ds <- jsDefs defs
270270 e <- toJS' e'
271− return $ jsFunc "" (combineDefs ds ++ ret e) ++ "()"
271+ return $ jsFunc "" (concat ds ++ ret e) ++ "()"
272272 where
273− combineDefs = concatMap (\def -> "\nvar " ++ def ++ ";")
274273 jsDefs defs = mapM toJS (sortBy f defs)
275274 f a b = compare (valueOf a) (valueOf b)
276275 valueOf (FnDef _ args _) = min 1 (length args)
@@ -280,21 +279,19 @@ caseToJS span e ps = do
280279 match <- caseToMatch ps
281280 e' <- toJS' e
282281 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)
282+ (Match name _ _, C _ _ (Var x)) -> (matchSubst [(name,x)] match, "")
283+ (Match name _ _, _) -> (match, assign name e')
284+ _ -> (match, "")
286285 matches <- matchToJS span match'
287− let enclose body = "function(){ " ++ body ++ " }()"
288− return . enclose $ case stmt of
289− Nothing -> matches
290− Just exp -> "\nvar " ++ exp ++ ";" ++ matches
286+ return $ "function(){ " ++ stmt ++ matches ++ " }()"
291287
292288matchToJS span match =
293289 case match of
294290 Match name clauses def ->
295291 do cases <- concat `liftM` mapM (clauseToJS span name) clauses
296292 finally <- matchToJS span def
297− return $ concat [ "\nswitch(", name, ".ctor){", indent cases, "\n}", finally ]
293+ return $ concat [ "\nswitch (", name, ".ctor) {",
294+ indent cases, "\n}", finally ]
298295 Fail -> return ("_E.Case" ++ parens (quoted (show span)))
299296 Break -> return "break;"
300297 Other e -> ret `liftM` toJS' e