git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: remove unnecessary 'continue' statements from rendered code
commite9a3a544ede4775748282c9435c3fea269678a1a authorAndy Arvanitis <andyarvanitis@gmail.com> dateMon 29 Feb 2016, 20:12 (11 years ago) parent2cd12ce
4 files changed+17−3
src/Language/PureScript/CodeGen/Cpp/Optimizer.hs+1 src/Language/PureScript/CodeGen/Cpp/Optimizer/Blocks.hs+2 src/Language/PureScript/CodeGen/Cpp/Optimizer/Unused.hs+8 src/Language/PureScript/Pretty/Cpp.hs+6−3
src/Language/PureScript/CodeGen/Cpp/Optimizer.hs+1
@@ -73,6 +73,7 @@ optimize' cpp = do
7373 , tco opts
7474 , magicDo opts
7575 , removeCodeAfterReturnStatements
76+ , removeCodeAfterContinueStatements
7677 , removeUnusedArg
7778 , unThunk
7879 , etaConvert
src/Language/PureScript/CodeGen/Cpp/Optimizer/Blocks.hs+2
@@ -20,6 +20,7 @@ module Language.PureScript.CodeGen.Cpp.Optimizer.Blocks
2020 ) where
2121
2222import Language.PureScript.CodeGen.Cpp.AST
23+import qualified Language.PureScript.Constants as C
2324
2425-- |
2526-- Collapse blocks which appear nested directly below another block
@@ -78,4 +79,5 @@ collapseIfElses = everywhereOnCpp collapse
7879 returns _ = False
7980 go (cpp' : cpps') = cpp' : go cpps'
8081 go cpp' = cpp'
82+ collapse (CppIfElse (CppAccessor (CppVar "otherwise") (CppVar mn')) body _) | mn' == C.prelude = body
8183 collapse cpp = cpp
src/Language/PureScript/CodeGen/Cpp/Optimizer/Unused.hs+8
@@ -29,6 +29,14 @@ removeCodeAfterReturnStatements = everywhereOnCpp (removeFromBlock go)
2929 isCppReturn (CppReturn _) = True
3030 isCppReturn _ = False
3131
32+removeCodeAfterContinueStatements :: Cpp -> Cpp
33+removeCodeAfterContinueStatements = everywhereOnCpp (removeFromBlock go)
34+ where
35+ go :: [Cpp] -> [Cpp]
36+ go cpps | not (any isCppContinue cpps) = cpps
37+ | otherwise = let (body, ret : _) = span (not . isCppContinue) cpps in body ++ [ret]
38+ isCppContinue = (== CppContinue)
39+
3240removeUnusedArg :: Cpp -> Cpp
3341removeUnusedArg = everywhereOnCpp convert
3442 where
src/Language/PureScript/Pretty/Cpp.hs+6−3
@@ -214,11 +214,14 @@ literals = mkPattern' match
214214 , return " = "
215215 , prettyPrintCpp' value
216216 ]
217− match (CppWhile cond sts) = fmap concat $ sequence
217+ match (CppWhile _ (CppBlock [])) = return []
218+ match (CppWhile cond sts@(CppBlock sts')) = fmap concat $ sequence
218219 [ return "while ("
219220 , prettyPrintCpp' cond
220221 , return ") "
221− , prettyPrintCpp' sts
222+ , prettyPrintCpp' $ if last sts' == CppContinue
223+ then CppBlock (init sts')
224+ else sts
222225 ]
223226 match (CppIfElse cond thens elses) = fmap concat $ sequence
224227 [ return "if ("
@@ -269,7 +272,7 @@ literals = mkPattern' match
269272 [ return "throw "
270273 , prettyPrintCpp' value
271274 ]
272− match CppContinue = return "continue;"
275+ match CppContinue = return "continue"
273276 match (CppComment [] cpp) = match cpp
274277 match (CppComment com cpp) = fmap concat $ sequence $
275278 [ return "\n"