on master help sign in
javascript: fix caseconst
31f9833raichoo
src/IRTS/CodegenJavaScript.hs+18−5
@@ -94,6 +94,7 @@ data JS = JSRaw String
94 | JSAlloc String (Maybe JS)
94 | JSAlloc String (Maybe JS)
95 | JSIndex JS JS
95 | JSIndex JS JS
96 | JSSwitch JS [(JS, JS)] (Maybe JS)
96 | JSSwitch JS [(JS, JS)] (Maybe JS)
97+ | JSCond [(JS, JS)]
97 | JSTernary JS JS JS
98 | JSTernary JS JS JS
98 | JSParens JS
99 | JSParens JS
99 | JSWhile JS JS
100 | JSWhile JS JS
@@ -207,8 +208,20 @@ compileJS (JSAlloc name val) =
207compileJS (JSIndex lhs rhs) =
208compileJS (JSIndex lhs rhs) =
208 compileJS lhs ++ "[" ++ compileJS rhs ++ "]"
209 compileJS lhs ++ "[" ++ compileJS rhs ++ "]"
209
210
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+
210−compileJS (JSSwitch cond branches def) =
223+compileJS (JSSwitch val branches def) =
211− "switch(" ++ compileJS cond ++ "){\n"
224+ "switch(" ++ compileJS val ++ "){\n"
212 ++ concatMap mkBranch branches
225 ++ concatMap mkBranch branches
213 ++ mkDefault def
226 ++ mkDefault def
214 ++ "}"
227 ++ "}"
@@ -594,9 +607,9 @@ jsCASE safe reg cases def =
594
607
595jsCONSTCASE :: Reg -> [(Const, [BC])] -> Maybe [BC] -> JS
608jsCONSTCASE :: Reg -> [(Const, [BC])] -> Maybe [BC] -> JS
596jsCONSTCASE reg cases def =
609jsCONSTCASE reg cases def =
597− JSSwitch (translateReg reg) (
610+ JSCond $ (
598− map (translateConstant *** prepBranch) cases
611+ map (jsEq (translateReg reg) . translateConstant *** prepBranch) cases
599− ) (fmap prepBranch def)
612+ ) ++ (maybe [] ((:[]) . ((,) JSNoop) . prepBranch) def)
600 where
613 where
601 prepBranch :: [BC] -> JS
614 prepBranch :: [BC] -> JS
602 prepBranch bc = JSSeq $ map translateBC bc
615 prepBranch bc = JSSeq $ map translateBC bc