1-- | Optimizer steps for simplifying JavaScript blocks
2module Language.PureScript.CoreImp.Optimizer.Blocks
3  ( collapseNestedBlocks
4  , collapseNestedIfs
5  ) where
6
7import Prelude.Compat
8
9import Language.PureScript.CoreImp.AST
10
11-- | Collapse blocks which appear nested directly below another block
12collapseNestedBlocks :: AST -> AST
13collapseNestedBlocks = everywhere collapse where
14  collapse :: AST -> AST
15  collapse (Block ss sts) = Block ss (concatMap go sts)
16  collapse js = js
17
18  go :: AST -> [AST]
19  go (Block _ sts) = sts
20  go s = [s]
21
22collapseNestedIfs :: AST -> AST
23collapseNestedIfs = everywhere collapse where
24  collapse :: AST -> AST
25  collapse (IfElse _ (BooleanLiteral _ True) (Block _ [js]) _) = js
26  collapse (IfElse s1 cond1 (Block _ [IfElse s2 cond2 body Nothing]) Nothing) =
27      IfElse s1 (Binary s2 And cond1 cond2) body Nothing
28  collapse js = js
29