on master help sign in
javascript: remove redundant local constructors
d3f3fe5raichoo
src/IRTS/CodegenJavaScript.hs+19−1
@@ -1027,7 +1027,7 @@ extractLocalConstructors js =
1027
1027
1028 extractLocalConstructors' :: JS -> [JS]
1028 extractLocalConstructors' :: JS -> [JS]
1029 extractLocalConstructors' js@(JSAlloc fun (Just (JSFunction args body))) =
1029 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)]
1031 where
1031 where
1032 cons :: [(JS, JS)]
1032 cons :: [(JS, JS)]
1033 cons = zipWith genName (foldJS match (++) [] body) [1..]
1033 cons = zipWith genName (foldJS match (++) [] body) [1..]
@@ -1043,6 +1043,24 @@ extractLocalConstructors js =
1043 | otherwise = []
1043 | otherwise = []
1044
1044
1045
1045
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+
1046 allocCon :: (JS, JS) -> JS
1064 allocCon :: (JS, JS) -> JS
1047 allocCon (js, JSIdent name) =
1065 allocCon (js, JSIdent name) =
1048 JSAlloc name (Just js)
1066 JSAlloc name (Just js)