@@ -94,6 +94,7 @@ data JS = JSRaw String
9494 | JSAlloc String (Maybe JS)
9595 | JSIndex JS JS
9696 | JSSwitch JS [(JS, JS)] (Maybe JS)
97+ | JSCond [(JS, JS)]
9798 | JSTernary JS JS JS
9899 | JSParens JS
99100 | JSWhile JS JS
@@ -207,8 +208,20 @@ compileJS (JSAlloc name val) =
207208compileJS (JSIndex lhs rhs) =
208209 compileJS lhs ++ "[" ++ compileJS rhs ++ "]"
209210
210−compileJS (JSSwitch cond branches def) =
211− "switch(" ++ compileJS cond ++ "){\n"
211+compileJS (JSCond branches) =
212+ intercalate " else " $ map createIfBlock branches
213+ where
214+ createIfBlock (JSNoop, e) =
215+ "{\n"
216+ ++ compileJS e
217+ ++ ";\n}"
218+ createIfBlock (cond, e) =
219+ "if (" ++ compileJS cond ++") {\n"
220+ ++ compileJS e
221+ ++ ";\n}"
222+
223+compileJS (JSSwitch val branches def) =
224+ "switch(" ++ compileJS val ++ "){\n"
212225 ++ concatMap mkBranch branches
213226 ++ mkDefault def
214227 ++ "}"
@@ -594,9 +607,9 @@ jsCASE safe reg cases def =
594607
595608jsCONSTCASE :: Reg -> [(Const, [BC])] -> Maybe [BC] -> JS
596609jsCONSTCASE reg cases def =
597− JSSwitch (translateReg reg) (
598− map (translateConstant *** prepBranch) cases
599− ) (fmap prepBranch def)
610+ JSCond $ (
611+ map (jsEq (translateReg reg) . translateConstant *** prepBranch) cases
612+ ) ++ (maybe [] ((:[]) . ((,) JSNoop) . prepBranch) def)
600613 where
601614 prepBranch :: [BC] -> JS
602615 prepBranch bc = JSSeq $ map translateBC bc