on master help sign in
Be sure to avoid variable shadowing when generating temporary variables, fix for # 318
934e590Evan Czaplicki
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
2
2
3import Control.Applicative ((<$>),(<*>))
3import Control.Applicative ((<$>),(<*>))
4import Control.Arrow (first,second)
4import Control.Arrow (first,second)
@@ -21,7 +21,7 @@ toMatch patterns = do
21newVar :: State Int String
21newVar :: State Int String
22newVar = do n <- get
22newVar = do n <- get
23 modify (+1)
23 modify (+1)
24− return $ "_case" ++ show n
24+ return $ "_v" ++ show n
25
25
26data Match t v
26data Match t v
27 = Match String [Clause t v] (Match t v)
27 = Match String [Clause t v] (Match t v)
compiler/Generate/JavaScript.hs+6−7
@@ -117,18 +117,17 @@ expression (L span expr) =
117 Binop op e1 e2 -> binop span op e1 e2
117 Binop op e1 e2 -> binop span op e1 e2
118
118
119 Lambda p e@(L s _) ->
119 Lambda p e@(L s _) ->
120+ do (args, body) <- foldM depattern ([], innerBody) (reverse patterns)
120− do body' <- expression body
121+ body' <- expression body
121 return $ case length args < 2 || length args > 9 of
122 return $ case length args < 2 || length args > 9 of
122 True -> foldr (==>) body' (map (:[]) args)
123 True -> foldr (==>) body' (map (:[]) args)
123 False -> ref ("F" ++ show (length args)) <| (args ==> body')
124 False -> ref ("F" ++ show (length args)) <| (args ==> body')
124 where
125 where
125− (args, body) = foldr depattern ([], innerBody) (zip patterns [0..])
126+ depattern (args, body) pattern =
126−
127− depattern (pattern,n) (args, body) =
128 case pattern of
127 case pattern of
129− PVar x -> (args ++ [x], body)
128+ PVar x -> return (args ++ [x], body)
130− _ -> let arg = "arg" ++ show n
129+ _ -> do arg <- Case.newVar
131− in (args ++ [arg], L s (Case (L s (Var arg)) [(pattern, body)]))
130+ return (args ++ [arg], L s (Case (L s (Var arg)) [(pattern, body)]))
132
131
133 (patterns, innerBody) = collect [p] e
132 (patterns, innerBody) = collect [p] e
134
133