@@ -371,28 +371,59 @@ generateDecisionTree root decisionTree =
371371 DT.Match (Opt.Jump target) ->
372372 return [ BreakStmt () (Just (targetLabel root target)) ]
373373
374− DT.Decision testPath edges fallback ->
375− do accessExpr <- generateJsExpr (pathToExpr root testPath)
376−
377− let testExpr =
378− case fst (head edges) of
379− DT.Constructor _ ->
380− DotRef () accessExpr (Id () "ctor")
381−
382− DT.Literal _ ->
383− accessExpr
374+ DT.Decision testPath [(test, subTree)] (Just fallback) ->
375+ let
376+ (tests, deepestSubTree) =
377+ collapseTests [(testPath, test)] fallback subTree
378+
379+ makeTest (path, test) =
380+ do testExpr <- pathToTestableExpr root path test
381+ return (InfixExpr () OpStrictEq testExpr (testToExpr test))
382+ in
383+ do testExprs <- mapM makeTest tests
384+ let cond = List.foldl1' (InfixExpr () OpLAnd) testExprs
385+ thenBranch <- generateDecisionTree root deepestSubTree
386+ elseBranch <- generateDecisionTree root fallback
387+ return [ IfStmt () cond (BlockStmt () thenBranch) (BlockStmt () elseBranch) ]
384388
389+ DT.Decision testPath edges fallback ->
390+ do testExpr <- pathToTestableExpr root testPath (fst (head edges))
385391 caseClauses <- mapM (edgeToCaseClause root) edges
386392 caseDefault <- fallbackToDefault root fallback
387−
388393 return [ SwitchStmt () testExpr (caseClauses ++ caseDefault) ]
389394
390395
396+collapseTests
397+ :: [(DT.Path, DT.Test)]
398+ -> DT.DecisionTree Opt.Jump
399+ -> DT.DecisionTree Opt.Jump
400+ -> ([(DT.Path, DT.Test)], DT.DecisionTree Opt.Jump)
401+collapseTests tests outerFallback decisionTree =
402+ case decisionTree of
403+ DT.Decision testPath [(test, subTree)] (Just fallback)
404+ | fallback == outerFallback ->
405+ collapseTests ((testPath, test) : tests) outerFallback subTree
406+
407+ _ ->
408+ (tests, decisionTree)
409+
410+
391411edgeToCaseClause :: String -> (DT.Test, DT.DecisionTree Opt.Jump) -> State Int (CaseClause ())
392412edgeToCaseClause root (test, subTree) =
393413 CaseClause () (testToExpr test) <$> generateDecisionTree root subTree
394414
395415
416+pathToTestableExpr :: String -> DT.Path -> DT.Test -> State Int (Expression ())
417+pathToTestableExpr root path exampleTest =
418+ do accessExpr <- generateJsExpr (pathToExpr root path)
419+ case exampleTest of
420+ DT.Constructor _ ->
421+ return (DotRef () accessExpr (Id () "ctor"))
422+
423+ DT.Literal _ ->
424+ return accessExpr
425+
426+
396427testToExpr :: DT.Test -> Expression ()
397428testToExpr test =
398429 case test of