on master help sign in
javascript: remove redundant local constructors
commitd3f3fe5220247f6363f3b3b7237103e16bea9002 authorraichoo <raichoo@googlemail.com> dateTue 11 Feb 2014, 20:25 (13 years ago) parent98ddd5b
1 file changed+19−1
src/IRTS/CodegenJavaScript.hs+19−1
src/IRTS/CodegenJavaScript.hs+19−1
@@ -1027,7 +1027,7 @@ extractLocalConstructors js =
10271027
10281028 extractLocalConstructors' :: JS -> [JS]
10291029 extractLocalConstructors' js@(JSAlloc fun (Just (JSFunction args body))) =
1030− map allocCon cons ++ [foldr (uncurry jsSubst) js (reverse cons)]
1030+ addCons cons [foldr (uncurry jsSubst) js (reverse cons)]
10311031 where
10321032 cons :: [(JS, JS)]
10331033 cons = zipWith genName (foldJS match (++) [] body) [1..]
@@ -1043,6 +1043,24 @@ extractLocalConstructors js =
10431043 | otherwise = []
10441044
10451045
1046+ addCons :: [(JS, JS)] -> [JS] -> [JS]
1047+ addCons [] js = js
1048+ addCons (con@(_, name):cons) js
1049+ | sum (map (countOccur name) js) > 0 =
1050+ addCons cons ((allocCon con) : js)
1051+ | otherwise =
1052+ addCons cons js
1053+
1054+
1055+ countOccur :: JS -> JS -> Int
1056+ countOccur ident js = foldJS match (+) 0 js
1057+ where
1058+ match :: JS -> Int
1059+ match js
1060+ | js == ident = 1
1061+ | otherwise = 0
1062+
1063+
10461064 allocCon :: (JS, JS) -> JS
10471065 allocCon (js, JSIdent name) =
10481066 JSAlloc name (Just js)