on master help sign in
javascript: smarter compilation of conditionals
cd20320raichoo
src/IRTS/CodegenJavaScript.hs+9−1
@@ -170,8 +170,16 @@ compileJS (JSIndex lhs rhs) =
170 compileJS lhs ++ "[" ++ compileJS rhs ++ "]"
170 compileJS lhs ++ "[" ++ compileJS rhs ++ "]"
171
171
172compileJS (JSCond branches) =
172compileJS (JSCond branches) =
173− intercalate " else " $ map createIfBlock branches
173+ intercalate " else " $ map createIfBlock (eliminateDeadBranches branches)
174 where
174 where
175+ eliminateDeadBranches (e@(JSTrue, _):_) = [e]
176+ eliminateDeadBranches (x:xs) = x : eliminateDeadBranches xs
177+ eliminateDeadBranches [] = []
178+
179+ createIfBlock (JSTrue, e) =
180+ "{\n"
181+ ++ "return " ++ compileJS e
182+ ++ ";\n}"
175 createIfBlock (cond, e) =
183 createIfBlock (cond, e) =
176 "if (" ++ compileJS cond ++") {\n"
184 "if (" ++ compileJS cond ++") {\n"
177 ++ "return " ++ compileJS e
185 ++ "return " ++ compileJS e