git.iko.soy/rowtype-yoga/purescript-native
on 36bae69 help sign in
55 lines 1.5 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Optimize
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12--
13-----------------------------------------------------------------------------
15module Language.PureScript.Optimize (
16 optimize
17) where
19import Data.Data
20import Data.Generics
22import Language.PureScript.Names
23import Language.PureScript.CodeGen.JS.AST
25optimize :: JS -> JS
26optimize = etaConvert . inlineVariables
28replaceIdent :: (Data d) => Ident -> JS -> d -> d
29replaceIdent var1 js = everywhere (mkT replace)
30 where
31 replace (JSVar var2) | var1 == var2 = js
32 replace js = js
34shouldInline :: JS -> Bool
35shouldInline (JSVar _) = True
36shouldInline (JSAccessor _ val) = shouldInline val
37shouldInline _ = False
39inlineVariables :: JS -> JS
40inlineVariables = everywhere (mkT removeFromBlock)
41 where
42 removeFromBlock :: JS -> JS
43 removeFromBlock (JSBlock sts) = JSBlock (go sts)
44 removeFromBlock js = js
45 go :: [JS] -> [JS]
46 go [] = []
47 go (JSVariableIntroduction var js : sts) | shouldInline js = go (replaceIdent var js sts)
48 go (s:sts) = s : go sts
50etaConvert :: JS -> JS
51etaConvert = everywhere (mkT convert)
52 where
53 convert :: JS -> JS
54 convert (JSBlock [JSReturn (JSApp (JSFunction Nothing [ident] (JSBlock body)) [arg])]) | shouldInline arg = JSBlock (replaceIdent ident arg body)
55 convert js = js