git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix a bug involving let bindings and unused variable removal. Support extended let in do-notation.
commitabd72b6a877829aa6c68a185b4e8768919c78ad8 authorPhil Freeman <paf31@cantab.net> dateWed 2 Apr 2014, 18:01 (13 years ago) parent649628d
8 files changed+32−19
examples/passing/Do.purs+10−2 examples/passing/Let.purs+7−1 purescript.cabal+1−1 src/Language/PureScript/Declarations.hs+1−1 src/Language/PureScript/Optimizer/Unused.hs+7−5 src/Language/PureScript/Parser/Declarations.hs+1−2 src/Language/PureScript/Pretty/Values.hs+2−4 src/Language/PureScript/Sugar/DoNotation.hs+3−3
examples/passing/Do.purs+10−2
@@ -35,8 +35,10 @@ test5 mx my mz = do
3535 Just (z + sum + 1)
3636
3737test6 mx = \_ -> do
38− let Just x = mx
39− Just x
38+ let
39+ f :: forall a. Maybe a -> a
40+ f (Just x) = x
41+ Just (f mx)
4042
4143test8 = \_ -> do
4244 Just (do
@@ -44,4 +46,10 @@ test8 = \_ -> do
4446
4547test9 = \_ -> (+) <$> Just 1 <*> Just 2
4648
49+test10 _ = do
50+ let
51+ f x = g x * 3
52+ g x = f x / 2
53+ Just (f 10)
54+
4755main = Debug.Trace.trace "Done"
examples/passing/Let.purs+7−1
@@ -24,7 +24,7 @@ test5 = let
2424 f x | x > 0 = g (x / 2) + 1
2525 f x = 0
2626 g x = f (x - 1) + 1
27− in g 10
27+ in f 10
2828
2929test6 = runPure (runST (do
3030 r <- newSTRef 0
@@ -47,6 +47,12 @@ test8 x = let
4747 go y = go $ (y + x / y) / 2
4848 in go x
4949
50+test10 _ =
51+ let
52+ f x = g x * 3
53+ g x = f x / 2
54+ in f 10
55+
5056main = do
5157 Debug.Trace.print (test1 1)
5258 Debug.Trace.print (test2 1 2)
purescript.cabal+1−1
@@ -1,5 +1,5 @@
11name: purescript
2−version: 0.4.12.1
2+version: 0.4.13
33cabal-version: >=1.8
44build-type: Custom
55license: MIT
src/Language/PureScript/Declarations.hs+1−1
@@ -378,7 +378,7 @@ data DoNotationElement
378378 -- |
379379 -- A let statement, i.e. a pure value with a binder
380380 --
381− | DoNotationLet Binder Value
381+ | DoNotationLet [Declaration]
382382 -- |
383383 -- A do notation element with source position information
384384 --
src/Language/PureScript/Optimizer/Unused.hs+7−5
@@ -24,12 +24,14 @@ import Language.PureScript.CodeGen.JS.AST
2424import Language.PureScript.Optimizer.Common
2525
2626removeUnusedVariables :: JS -> JS
27−removeUnusedVariables = everywhere (mkT $ removeFromBlock go)
27+removeUnusedVariables = everywhere (mkT $ removeFromBlock withBlock)
2828 where
29− go :: [JS] -> [JS]
30− go [] = []
31− go (JSVariableIntroduction var _ : sts) | not (isUsed var sts) = go sts
32− go (s:sts) = s : go sts
29+ withBlock :: [JS] -> [JS]
30+ withBlock sts = go sts sts
31+ go :: [JS] -> [JS] -> [JS]
32+ go _ [] = []
33+ go sts (JSVariableIntroduction var _ : rest) | not (isUsed var sts) = go sts rest
34+ go sts (s : rest) = s : go sts rest
3335
3436removeCodeAfterReturnStatements :: JS -> JS
3537removeCodeAfterReturnStatements = everywhere (mkT $ removeFromBlock go)
src/Language/PureScript/Parser/Declarations.hs+1−2
@@ -306,8 +306,7 @@ parseDo = do
306306 Do <$> C.mark (P.many (C.same *> C.mark parseDoNotationElement))
307307
308308parseDoNotationLet :: P.Parsec String ParseState DoNotationElement
309−parseDoNotationLet = DoNotationLet <$> (C.reserved "let" *> C.indented *> parseBinder)
310− <*> (C.indented *> C.reservedOp "=" *> parseValue)
309+parseDoNotationLet = DoNotationLet <$> (C.reserved "let" *> C.indented *> C.mark (P.many1 (C.same *> parseLocalDeclaration)))
311310
312311parseDoNotationBind :: P.Parsec String ParseState DoNotationElement
313312parseDoNotationBind = DoNotationBind <$> parseBinder <*> (C.indented *> C.reservedOp "<-" *> parseValue)
src/Language/PureScript/Pretty/Values.hs+2−4
@@ -104,12 +104,10 @@ prettyPrintDoNotationElement (DoNotationBind binder val) =
104104 , return " <- "
105105 , prettyPrintValue' val
106106 ]
107−prettyPrintDoNotationElement (DoNotationLet binder val) =
107+prettyPrintDoNotationElement (DoNotationLet ds) =
108108 fmap concat $ sequence
109109 [ return "let "
110− , prettyPrintBinder' binder
111− , return " = "
112− , prettyPrintValue' val
110+ , withIndent $ prettyPrintMany prettyPrintDeclaration ds
113111 ]
114112prettyPrintDoNotationElement (PositionedDoNotationElement _ el) = prettyPrintDoNotationElement el
115113
src/Language/PureScript/Sugar/DoNotation.hs+3−3
@@ -59,8 +59,8 @@ desugarDo = everywhereM (mkM replace)
5959 rest' <- go rest
6060 let ident = head $ unusedNames rest'
6161 return $ App (App bind val) (Abs (Left ident) (Case [Var (Qualified Nothing ident)] [CaseAlternative [binder] Nothing rest']))
62− go [DoNotationLet _ _] = Left $ mkErrorStack "Let statement cannot be the last statement in a do block" Nothing
63− go (DoNotationLet binder val : rest) = do
62+ go [DoNotationLet _] = Left $ mkErrorStack "Let statement cannot be the last statement in a do block" Nothing
63+ go (DoNotationLet ds : rest) = do
6464 rest' <- go rest
65− return $ Case [val] [CaseAlternative [binder] Nothing rest']
65+ return $ Let ds rest'
6666 go (PositionedDoNotationElement pos el : rest) = PositionedValue pos <$> go (el : rest)