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
25import Language.PureScript.Options
27optimize :: Options -> JS -> JS
28optimize opts = tco opts . removeUnusedVariables . unThunk . etaConvert . inlineVariables
30replaceIdent :: (Data d) => Ident -> JS -> d -> d
31replaceIdent var1 js = everywhere (mkT replace)
33 replace (JSVar var2) | var1 == var2 = js
36replaceIdents :: (Data d) => [(Ident, JS)] -> d -> d
37replaceIdents vars = everywhere (mkT replace)
39 replace v@(JSVar var) = fromMaybe v $ lookup var vars
42isReassigned :: (Data d) => Ident -> d -> Bool
43isReassigned var1 = everything (||) (mkQ False check)
46 check (JSAssignment (JSAssignVariable var2) _) | var1 == var2 = True
49isUsed :: (Data d) => Ident -> d -> Bool
50isUsed var1 = everything (||) (mkQ False check)
53 check (JSVar var2) | var1 == var2 = True
54 check (JSAssignment target _) | var1 == targetVariable target = True
56 targetVariable :: JSAssignment -> Ident
57 targetVariable (JSAssignVariable var) = var
58 targetVariable (JSAssignProperty _ tgt) = targetVariable tgt
60shouldInline :: JS -> Bool
61shouldInline (JSVar _) = True
62shouldInline (JSNumericLiteral _) = True
63shouldInline (JSStringLiteral _) = True
64shouldInline (JSBooleanLiteral _) = True
65shouldInline (JSAccessor _ val) = shouldInline val
66shouldInline (JSIndexer index val) = shouldInline index && shouldInline val
69inlineVariables :: JS -> JS
70inlineVariables = everywhere (mkT removeFromBlock)
72 removeFromBlock :: JS -> JS
73 removeFromBlock (JSBlock sts) = JSBlock (go sts)
74 removeFromBlock js = js
77 go (JSVariableIntroduction var (Just js) : sts) | shouldInline js && not (isReassigned var sts) = go (replaceIdent var js sts)
78 go (s:sts) = s : go sts
80removeUnusedVariables :: JS -> JS
81removeUnusedVariables = everywhere (mkT removeFromBlock)
83 removeFromBlock :: JS -> JS
84 removeFromBlock (JSBlock sts) = JSBlock (go sts)
85 removeFromBlock js = js
88 go (JSVariableIntroduction var _ : sts) | not (isUsed var sts) = go sts
89 go (s:sts) = s : go sts
92etaConvert = everywhere (mkT convert)
95 convert (JSBlock [JSReturn (JSApp (JSFunction Nothing idents (JSBlock body)) args)])
96 | all shouldInline args = JSBlock (replaceIdents (zip idents args) body)
100unThunk = everywhere (mkT convert)
103 convert (JSBlock [JSReturn (JSApp (JSFunction Nothing [] (JSBlock body)) [])]) = JSBlock body
106tco :: Options -> JS -> JS
107tco opts | optionsTco opts = tco'
111tco' = everywhere (mkT convert)
114 convert (JSVariableIntroduction name (Just (JSFunction Nothing args body)))
115 | isTailCall name body = (JSVariableIntroduction name (Just (JSFunction Nothing args (toLoop name args body))))
117 isTailCall :: Ident -> JS -> Bool
118 isTailCall _ _ = False
119 toLoop :: Ident -> [Ident] -> JS -> JS
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