@@ -33,15 +33,16 @@ data CompileInfo = CompileInfo { compileInfoApplyCases :: [Int]
35initCompileInfo :: [(Name, [BC])] -> CompileInfo
35initCompileInfo :: [(Name, [BC])] -> CompileInfo
36−initCompileInfo bc = CompileInfo (collectApplyCases bc) []
36+initCompileInfo bc =
37+ CompileInfo (collectCases "APPLY" bc) (collectCases "EVAL" bc)
38− collectApplyCases :: [(Name, [BC])] -> [Int]
39+ collectCases :: String -> [(Name, [BC])] -> [Int]
39− collectApplyCases bc = getCases $ findApply bc
40+ collectCases fun bc = getCases $ findFunction fun bc
41− findApply :: [(Name, [BC])] -> [BC]
42+ findFunction :: String -> [(Name, [BC])] -> [BC]
42− findApply ((MN 0 fun, bc):_)
43+ findFunction f ((MN 0 fun, bc):_)
43− | fun == txt "APPLY" = bc
44+ | fun == txt f = bc
44− findApply (_:bc) = findApply bc
45+ findFunction f (_:bc) = findFunction f bc
46 getCases :: [BC] -> [Int]
47 getCases :: [BC] -> [Int]
47 getCases = concatMap analyze
48 getCases = concatMap analyze
@@ -343,7 +344,7 @@ translateDecl info (name@(MN 0 fun), bc)
343 translateName name
344 translateName name
344 ) (Just $ JSFunction ["vm", "oldbase"] (
345 ) (Just $ JSFunction ["vm", "oldbase"] (
345 JSSeq $ JSAlloc "myoldbase" Nothing : map (translateBC info) (fst body) ++ [
346 JSSeq $ JSAlloc "myoldbase" Nothing : map (translateBC info) (fst body) ++ [
346− JSCond [ ( (translateReg $ caseReg (snd body)) `jsInstanceOf` "i$CON"
347+ JSCond [ ( (translateReg $ caseReg (snd body)) `jsInstanceOf` "i$CON" `jsAnd` (JSProj (translateReg $ caseReg (snd body)) "app")
347 , JSApp (JSProj (translateReg $ caseReg (snd body)) "app") [ JSIdent "vm"
348 , JSApp (JSProj (translateReg $ caseReg (snd body)) "app") [ JSIdent "vm"
348 , JSIdent "oldbase"
349 , JSIdent "oldbase"
349 , JSIdent "myoldbase"
350 , JSIdent "myoldbase"
@@ -357,6 +358,27 @@ translateDecl info (name@(MN 0 fun), bc)
362+ | txt "EVAL" == fun =
363+ allocCaseFunctions (snd body)
366+ ) (Just $ JSFunction ["vm", "oldbase"] (
367+ JSSeq $ JSAlloc "myoldbase" Nothing : map (translateBC info) (fst body) ++ [
368+ JSCond [ ( (translateReg $ caseReg (snd body)) `jsInstanceOf` "i$CON" `jsAnd` (JSProj (translateReg $ caseReg (snd body)) "ev")
369+ , JSApp (JSProj (translateReg $ caseReg (snd body)) "ev") [ JSIdent "vm"
371+ , JSIdent "myoldbase"
375+ , JSSeq $ map (translateBC info) (defaultCase (snd body))
361 body :: ([BC], [BC])
383 body :: ([BC], [BC])
362 body = break isCase bc
384 body = break isCase bc
@@ -568,6 +590,9 @@ jsMKCON info r t rs =
568 , if t `elem` compileInfoApplyCases info
590 , if t `elem` compileInfoApplyCases info
569 then JSIdent $ translateName (sMN 0 "APPLY") ++ "$" ++ show t
591 then JSIdent $ translateName (sMN 0 "APPLY") ++ "$" ++ show t
570 else JSNull
592 else JSNull
593+ , if t `elem` compileInfoEvalCases info
594+ then JSIdent $ translateName (sMN 0 "EVAL") ++ "$" ++ show t
@@ -638,11 +663,9 @@ jsCASE info safe reg cases def =
639 jsTAG :: JS -> JS
664 jsTAG :: JS -> JS
640 jsTAG js =
665 jsTAG js =
641− JSTernary (jsIsNumber js `jsOr` jsIsNull js) (
642− JSNum (JSInt $ negate 1)
643− ) (JSTernary (js `jsInstanceOf` "i$CON") (
666+ (JSTernary (js `jsInstanceOf` "i$CON") (
644− JSProj js "tag"
667+ JSProj js "tag"
645− ) (JSNum (JSInt $ negate 1)))
668+ ) (JSNum (JSInt $ negate 1)))
647 jsCTAG :: JS -> JS
670 jsCTAG :: JS -> JS
648 jsCTAG js = JSProj js "tag"
671 jsCTAG js = JSProj js "tag"