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 (JSAssignVariable 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 target _) | var1 == targetVariable target = True
48 targetVariable :: JSAssignment -> Ident
49 targetVariable (JSAssignVariable var) = var
50 targetVariable (JSAssignProperty _ tgt) = targetVariable tgt
52shouldInline :: JS -> Bool
53shouldInline (JSVar _) = True
54shouldInline (JSNumericLiteral _) = True
55shouldInline (JSStringLiteral _) = True
56shouldInline (JSBooleanLiteral _) = True
57shouldInline (JSAccessor _ val) = shouldInline val
58shouldInline (JSIndexer index val) = shouldInline index && shouldInline val
61inlineVariables :: JS -> JS
62inlineVariables = everywhere (mkT removeFromBlock)
64 removeFromBlock :: JS -> JS
65 removeFromBlock (JSBlock sts) = JSBlock (go sts)
66 removeFromBlock js = js
69 go (JSVariableIntroduction var (Just js) : sts) | shouldInline js && not (isReassigned var sts) = go (replaceIdent var js sts)
70 go (s:sts) = s : go sts
72removeUnusedVariables :: JS -> JS
73removeUnusedVariables = everywhere (mkT removeFromBlock)
75 removeFromBlock :: JS -> JS
76 removeFromBlock (JSBlock sts) = JSBlock (go sts)
77 removeFromBlock js = js
80 go (JSVariableIntroduction var _ : sts) | not (isUsed var sts) = go sts
81 go (s:sts) = s : go sts
84etaConvert = everywhere (mkT convert)
87 convert (JSBlock [JSReturn (JSApp (JSFunction Nothing [ident] (JSBlock body)) [arg])]) | shouldInline arg = JSBlock (replaceIdent ident arg body)
91unThunk = everywhere (mkT convert)
94 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