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 (
22import Language.PureScript.Names
23import Language.PureScript.CodeGen.JS.AST
26optimize = removeUnusedVariables . unThunk . etaConvert . inlineVariables
28replaceIdent :: (Data d) => Ident -> JS -> d -> d
29replaceIdent var1 js = everywhere (mkT replace)
31 replace (JSVar var2) | var1 == var2 = js
34isReassigned :: (Data d) => Ident -> d -> Bool
35isReassigned var1 = everything (||) (mkQ False check)
38 check (JSAssignment var2 _) | var1 == var2 = True
41isUsed :: (Data d) => Ident -> d -> Bool
42isUsed var1 = everything (||) (mkQ False check)
45 check (JSVar var2) | var1 == var2 = True
46 check (JSAssignment var2 _) | var1 == var2 = True
49shouldInline :: JS -> Bool
50shouldInline (JSVar _) = True
51shouldInline (JSNumericLiteral _) = True
52shouldInline (JSStringLiteral _) = True
53shouldInline (JSBooleanLiteral _) = True
54shouldInline (JSAccessor _ val) = shouldInline val
55shouldInline (JSIndexer index val) = shouldInline index && shouldInline val
58inlineVariables :: JS -> JS
59inlineVariables = everywhere (mkT removeFromBlock)
61 removeFromBlock :: JS -> JS
62 removeFromBlock (JSBlock sts) = JSBlock (go sts)
63 removeFromBlock js = js
66 go (JSVariableIntroduction var js : sts) | shouldInline js && not (isReassigned var sts) = go (replaceIdent var js sts)
67 go (s:sts) = s : go sts
69removeUnusedVariables :: JS -> JS
70removeUnusedVariables = everywhere (mkT removeFromBlock)
72 removeFromBlock :: JS -> JS
73 removeFromBlock (JSBlock sts) = JSBlock (go sts)
74 removeFromBlock js = js
77 go (JSVariableIntroduction var _ : sts) | not (isUsed var sts) = go sts
78 go (s:sts) = s : go sts
81etaConvert = everywhere (mkT convert)
84 convert (JSBlock [JSReturn (JSApp (JSFunction Nothing [ident] (JSBlock body)) [arg])]) | shouldInline arg = JSBlock (replaceIdent ident arg body)
88unThunk = everywhere (mkT convert)
91 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