on master help sign in
Merge pull request #875 from raichoo/javascript

javascript: remove redundant local constructors

commitce4fef09dd8ba2da22fcad16a4e7a37caf801c49 authorEdwin Brady <edwin.brady@gmail.com> dateThu 13 Feb 2014, 20:00 (13 years ago) parentba03e21d3f3fe5
1 file changed+19−1
src/IRTS/CodegenJavaScript.hs+19−1
src/IRTS/CodegenJavaScript.hs+19−1
@@ -1031,7 +1031,7 @@ extractLocalConstructors js =
10311031
10321032 extractLocalConstructors' :: JS -> [JS]
10331033 extractLocalConstructors' js@(JSAlloc fun (Just (JSFunction args body))) =
1034− map allocCon cons ++ [foldr (uncurry jsSubst) js (reverse cons)]
1034+ addCons cons [foldr (uncurry jsSubst) js (reverse cons)]
10351035 where
10361036 cons :: [(JS, JS)]
10371037 cons = zipWith genName (foldJS match (++) [] body) [1..]
@@ -1047,6 +1047,24 @@ extractLocalConstructors js =
10471047 | otherwise = []
10481048
10491049
1050+ addCons :: [(JS, JS)] -> [JS] -> [JS]
1051+ addCons [] js = js
1052+ addCons (con@(_, name):cons) js
1053+ | sum (map (countOccur name) js) > 0 =
1054+ addCons cons ((allocCon con) : js)
1055+ | otherwise =
1056+ addCons cons js
1057+
1058+
1059+ countOccur :: JS -> JS -> Int
1060+ countOccur ident js = foldJS match (+) 0 js
1061+ where
1062+ match :: JS -> Int
1063+ match js
1064+ | js == ident = 1
1065+ | otherwise = 0
1066+
1067+
10501068 allocCon :: (JS, JS) -> JS
10511069 allocCon (js, JSIdent name) =
10521070 JSAlloc name (Just js)