git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: remove unnecessary 'continue' statements from rendered code
e9a3a54Andy Arvanitis
src/Language/PureScript/CodeGen/Cpp/Optimizer.hs+1
@@ -73,6 +73,7 @@ optimize' cpp = do
73 , tco opts
73 , tco opts
74 , magicDo opts
74 , magicDo opts
75 , removeCodeAfterReturnStatements
75 , removeCodeAfterReturnStatements
76+ , removeCodeAfterContinueStatements
76 , removeUnusedArg
77 , removeUnusedArg
77 , unThunk
78 , unThunk
78 , etaConvert
79 , etaConvert
src/Language/PureScript/CodeGen/Cpp/Optimizer/Blocks.hs+2
@@ -20,6 +20,7 @@ module Language.PureScript.CodeGen.Cpp.Optimizer.Blocks
20 ) where
20 ) where
21
21
22import Language.PureScript.CodeGen.Cpp.AST
22import Language.PureScript.CodeGen.Cpp.AST
23+import qualified Language.PureScript.Constants as C
23
24
24-- |
25-- |
25-- Collapse blocks which appear nested directly below another block
26-- Collapse blocks which appear nested directly below another block
@@ -78,4 +79,5 @@ collapseIfElses = everywhereOnCpp collapse
78 returns _ = False
79 returns _ = False
79 go (cpp' : cpps') = cpp' : go cpps'
80 go (cpp' : cpps') = cpp' : go cpps'
80 go cpp' = cpp'
81 go cpp' = cpp'
82+ collapse (CppIfElse (CppAccessor (CppVar "otherwise") (CppVar mn')) body _) | mn' == C.prelude = body
81 collapse cpp = cpp
83 collapse cpp = cpp
src/Language/PureScript/CodeGen/Cpp/Optimizer/Unused.hs+8
@@ -29,6 +29,14 @@ removeCodeAfterReturnStatements = everywhereOnCpp (removeFromBlock go)
29 isCppReturn (CppReturn _) = True
29 isCppReturn (CppReturn _) = True
30 isCppReturn _ = False
30 isCppReturn _ = False
31
31
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+
32removeUnusedArg :: Cpp -> Cpp
40removeUnusedArg :: Cpp -> Cpp
33removeUnusedArg = everywhereOnCpp convert
41removeUnusedArg = everywhereOnCpp convert
34 where
42 where
src/Language/PureScript/Pretty/Cpp.hs+6−3
@@ -214,11 +214,14 @@ literals = mkPattern' match
214 , return " = "
214 , return " = "
215 , prettyPrintCpp' value
215 , prettyPrintCpp' value
216 ]
216 ]
217+ match (CppWhile _ (CppBlock [])) = return []
217− match (CppWhile cond sts) = fmap concat $ sequence
218+ match (CppWhile cond sts@(CppBlock sts')) = fmap concat $ sequence
218 [ return "while ("
219 [ return "while ("
219 , prettyPrintCpp' cond
220 , prettyPrintCpp' cond
220 , return ") "
221 , return ") "
221− , prettyPrintCpp' sts
222+ , prettyPrintCpp' $ if last sts' == CppContinue
223+ then CppBlock (init sts')
224+ else sts
222 ]
225 ]
223 match (CppIfElse cond thens elses) = fmap concat $ sequence
226 match (CppIfElse cond thens elses) = fmap concat $ sequence
224 [ return "if ("
227 [ return "if ("
@@ -269,7 +272,7 @@ literals = mkPattern' match
269 [ return "throw "
272 [ return "throw "
270 , prettyPrintCpp' value
273 , prettyPrintCpp' value
271 ]
274 ]
272− match CppContinue = return "continue;"
275+ match CppContinue = return "continue"
273 match (CppComment [] cpp) = match cpp
276 match (CppComment [] cpp) = match cpp
274 match (CppComment com cpp) = fmap concat $ sequence $
277 match (CppComment com cpp) = fmap concat $ sequence $
275 [ return "\n"
278 [ return "\n"