summaryrefslogtreecommitdiff
path: root/src/Language/PureScript/CoreImp/Optimizer/Blocks.hs
blob: 04febf2039e4ec71d1479bed2176383d04fcf6fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-- | Optimizer steps for simplifying JavaScript blocks
module Language.PureScript.CoreImp.Optimizer.Blocks
  ( collapseNestedBlocks
  , collapseNestedIfs
  ) where

import Prelude.Compat

import Language.PureScript.CoreImp.AST

-- | Collapse blocks which appear nested directly below another block
collapseNestedBlocks :: AST -> AST
collapseNestedBlocks = everywhere collapse where
  collapse :: AST -> AST
  collapse (Block ss sts) = Block ss (concatMap go sts)
  collapse js = js
  
  go :: AST -> [AST]
  go (Block _ sts) = sts
  go s = [s]

collapseNestedIfs :: AST -> AST
collapseNestedIfs = everywhere collapse where
  collapse :: AST -> AST
  collapse (IfElse _ (BooleanLiteral _ True) (Block _ [js]) _) = js
  collapse (IfElse s1 cond1 (Block _ [IfElse s2 cond2 body Nothing]) Nothing) =
      IfElse s1 (Binary s2 And cond1 cond2) body Nothing
  collapse js = js