1-----------------------------------------------------------------------------
3-- Module : Language.PureScript.Optimize
4-- Copyright : (c) Phil Freeman 2013
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
13-----------------------------------------------------------------------------
15module Language.PureScript.Optimize (
20import Data.Maybe (fromMaybe)
23import Language.PureScript.Names
24import Language.PureScript.CodeGen.JS.AST
27optimize = removeUnusedVariables . unThunk . etaConvert . inlineVariables
29replaceIdent :: (Data d) => Ident -> JS -> d -> d
30replaceIdent var1 js = everywhere (mkT replace)
32 replace (JSVar var2) | var1 == var2 = js
35replaceIdents :: (Data d) => [(Ident, JS)] -> d -> d
36replaceIdents vars = everywhere (mkT replace)
38 replace v@(JSVar var) = fromMaybe v $ lookup var vars
41isReassigned :: (Data d) => Ident -> d -> Bool
42isReassigned var1 = everything (||) (mkQ False check)
45 check (JSAssignment (JSAssignVariable var2) _) | var1 == var2 = True
48isUsed :: (Data d) => Ident -> d -> Bool
49isUsed var1 = everything (||) (mkQ False check)
52 check (JSVar var2) | var1 == var2 = True
53 check (JSAssignment target _) | var1 == targetVariable target = True
55 targetVariable :: JSAssignment -> Ident
56 targetVariable (JSAssignVariable var) = var
57 targetVariable (JSAssignProperty _ tgt) = targetVariable tgt
59shouldInline :: JS -> Bool
60shouldInline (JSVar _) = True
61shouldInline (JSNumericLiteral _) = True
62shouldInline (JSStringLiteral _) = True
63shouldInline (JSBooleanLiteral _) = True
64shouldInline (JSAccessor _ val) = shouldInline val
65shouldInline (JSIndexer index val) = shouldInline index && shouldInline val
68inlineVariables :: JS -> JS
69inlineVariables = everywhere (mkT removeFromBlock)
71 removeFromBlock :: JS -> JS
72 removeFromBlock (JSBlock sts) = JSBlock (go sts)
73 removeFromBlock js = js
76 go (JSVariableIntroduction var (Just js) : sts) | shouldInline js && not (isReassigned var sts) = go (replaceIdent var js sts)
77 go (s:sts) = s : go sts
79removeUnusedVariables :: JS -> JS
80removeUnusedVariables = everywhere (mkT removeFromBlock)
82 removeFromBlock :: JS -> JS
83 removeFromBlock (JSBlock sts) = JSBlock (go sts)
84 removeFromBlock js = js
87 go (JSVariableIntroduction var _ : sts) | not (isUsed var sts) = go sts
88 go (s:sts) = s : go sts
91etaConvert = everywhere (mkT convert)
94 convert (JSBlock [JSReturn (JSApp (JSFunction Nothing idents (JSBlock body)) args)])
95 | all shouldInline args = JSBlock (replaceIdents (zip idents args) body)
99unThunk = everywhere (mkT convert)
102 convert (JSBlock [JSReturn (JSApp (JSFunction Nothing [] (JSBlock body)) [])]) = JSBlock body
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list