on master help sign in
Fix missing `var` problem.
35fe7d6evancz
compiler/Gen/CompileToJS.hs+10−13
@@ -138,8 +138,8 @@ class ToJS a where
138 toJS :: a -> GuidCounter String
138 toJS :: a -> GuidCounter String
139
139
140instance ToJS Def where
140instance ToJS Def where
141− toJS (FnDef x [] e) = assign' x `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
142+ toJS (FnDef f as e) = (assign f . wrapper . func) `liftM` toJS' e
143 where
143 where
144 func body = jsFunc (intercalate ", " as) (ret body)
144 func body = jsFunc (intercalate ", " as) (ret body)
145 wrapper e | length as == 1 = e
145 wrapper e | length as == 1 = e
@@ -152,7 +152,7 @@ instance ToJS Def where
152instance ToJS Statement where
152instance ToJS Statement where
153 toJS stmt =
153 toJS stmt =
154 case stmt of
154 case stmt of
155− Definition d -> (\asgn -> "\n" ++ asgn ++ ";") `liftM` toJS d
155+ Definition d -> toJS d
156 Datatype _ _ tcs -> concat `liftM` mapM (toJS . toDef) tcs
156 Datatype _ _ tcs -> concat `liftM` mapM (toJS . toDef) tcs
157 where toDef (name,args) =
157 where toDef (name,args) =
158 let vars = map (('a':) . show) [1..length args] in
158 let vars = map (('a':) . show) [1..length args] in
@@ -268,9 +268,8 @@ multiIfToJS span ps =
268
268
269jsLet defs e' = do ds <- jsDefs defs
269jsLet defs e' = do ds <- jsDefs defs
270 e <- toJS' e'
270 e <- toJS' e'
271− return $ jsFunc "" (combineDefs ds ++ ret e) ++ "()"
271+ return $ jsFunc "" (concat ds ++ ret e) ++ "()"
272 where
272 where
273− combineDefs = concatMap (\def -> "\nvar " ++ def ++ ";")
274 jsDefs defs = mapM toJS (sortBy f defs)
273 jsDefs defs = mapM toJS (sortBy f defs)
275 f a b = compare (valueOf a) (valueOf b)
274 f a b = compare (valueOf a) (valueOf b)
276 valueOf (FnDef _ args _) = min 1 (length args)
275 valueOf (FnDef _ args _) = min 1 (length args)
@@ -280,21 +279,19 @@ caseToJS span e ps = do
280 match <- caseToMatch ps
279 match <- caseToMatch ps
281 e' <- toJS' e
280 e' <- toJS' e
282 let (match',stmt) = case (match,e) of
281 let (match',stmt) = case (match,e) of
283− (Match name _ _, C _ _ (Var x)) -> (matchSubst [(name,x)] match, Nothing)
282+ (Match name _ _, C _ _ (Var x)) -> (matchSubst [(name,x)] match, "")
284− (Match name _ _, _) -> (match, Just (name ++ " = " ++ e'))
283+ (Match name _ _, _) -> (match, assign name e')
285− _ -> (match, Nothing)
284+ _ -> (match, "")
286 matches <- matchToJS span match'
285 matches <- matchToJS span match'
287− let enclose body = "function(){ " ++ body ++ " }()"
286+ return $ "function(){ " ++ stmt ++ matches ++ " }()"
288− return . enclose $ case stmt of
289− Nothing -> matches
290− Just exp -> "\nvar " ++ exp ++ ";" ++ matches
291
287
292matchToJS span match =
288matchToJS span match =
293 case match of
289 case match of
294 Match name clauses def ->
290 Match name clauses def ->
295 do cases <- concat `liftM` mapM (clauseToJS span name) clauses
291 do cases <- concat `liftM` mapM (clauseToJS span name) clauses
296 finally <- matchToJS span def
292 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 ]
298 Fail -> return ("_E.Case" ++ parens (quoted (show span)))
295 Fail -> return ("_E.Case" ++ parens (quoted (show span)))
299 Break -> return "break;"
296 Break -> return "break;"
300 Other e -> ret `liftM` toJS' e
297 Other e -> ret `liftM` toJS' e