on master help sign in
Be sure to avoid variable shadowing when generating temporary variables, fix for # 318
commit934e590a7b1547a21b7ec799e7c3f9fd57049858 authorEvan Czaplicki <info@elm-lang.org> dateSat 2 Nov 2013, 19:05 (13 years ago) parenta24872f
2 files changed+8−9
compiler/Generate/Cases.hs+2−2 compiler/Generate/JavaScript.hs+6−7
compiler/Generate/Cases.hs+2−2
@@ -1,4 +1,4 @@
1−module Generate.Cases (toMatch, Match (..), Clause (..), matchSubst) where
1+module Generate.Cases (toMatch, Match (..), Clause (..), matchSubst, newVar) where
22
33import Control.Applicative ((<$>),(<*>))
44import Control.Arrow (first,second)
@@ -21,7 +21,7 @@ toMatch patterns = do
2121newVar :: State Int String
2222newVar = do n <- get
2323 modify (+1)
24− return $ "_case" ++ show n
24+ return $ "_v" ++ show n
2525
2626data Match t v
2727 = Match String [Clause t v] (Match t v)
compiler/Generate/JavaScript.hs+6−7
@@ -117,18 +117,17 @@ expression (L span expr) =
117117 Binop op e1 e2 -> binop span op e1 e2
118118
119119 Lambda p e@(L s _) ->
120− do body' <- expression body
120+ do (args, body) <- foldM depattern ([], innerBody) (reverse patterns)
121+ body' <- expression body
121122 return $ case length args < 2 || length args > 9 of
122123 True -> foldr (==>) body' (map (:[]) args)
123124 False -> ref ("F" ++ show (length args)) <| (args ==> body')
124125 where
125− (args, body) = foldr depattern ([], innerBody) (zip patterns [0..])
126−
127− depattern (pattern,n) (args, body) =
126+ depattern (args, body) pattern =
128127 case pattern of
129− PVar x -> (args ++ [x], body)
130− _ -> let arg = "arg" ++ show n
131− in (args ++ [arg], L s (Case (L s (Var arg)) [(pattern, body)]))
128+ PVar x -> return (args ++ [x], body)
129+ _ -> do arg <- Case.newVar
130+ return (args ++ [arg], L s (Case (L s (Var arg)) [(pattern, body)]))
132131
133132 (patterns, innerBody) = collect [p] e
134133