on master help sign in
filter out if branches after a True condition
c67a181Evan Czaplicki
src/Generate/JavaScript.hs+29−8
@@ -312,7 +312,7 @@ generateIf :: [(Canonical.Expr, Canonical.Expr)] -> Canonical.Expr -> State Int
312generateIf givenBranches givenFinally =
312generateIf givenBranches givenFinally =
313 let
313 let
314 (branches, finally) =
314 (branches, finally) =
315− combineIfs givenBranches givenFinally
315+ crushIfs givenBranches givenFinally
316
316
317 convertBranch (condition, expr) =
317 convertBranch (condition, expr) =
318 (,) <$> exprToJsExpr condition <*> exprToCode expr
318 (,) <$> exprToJsExpr condition <*> exprToCode expr
@@ -333,17 +333,38 @@ generateIf givenBranches givenFinally =
333 jsExpr (foldr ifExpression (toExpr jsFinally) (map (second toExpr) jsBranches))
333 jsExpr (foldr ifExpression (toExpr jsFinally) (map (second toExpr) jsBranches))
334
334
335
335
336−combineIfs
336+crushIfs
337 :: [(Canonical.Expr, Canonical.Expr)]
337 :: [(Canonical.Expr, Canonical.Expr)]
338 -> Canonical.Expr
338 -> Canonical.Expr
339 -> ([(Canonical.Expr, Canonical.Expr)], Canonical.Expr)
339 -> ([(Canonical.Expr, Canonical.Expr)], Canonical.Expr)
340−combineIfs branches finally =
340+crushIfs branches finally =
341− case finally of
341+ crushIfsHelp [] branches finally
342+
343+
344+crushIfsHelp
345+ :: [(Canonical.Expr, Canonical.Expr)]
346+ -> [(Canonical.Expr, Canonical.Expr)]
347+ -> Canonical.Expr
348+ -> ([(Canonical.Expr, Canonical.Expr)], Canonical.Expr)
349+crushIfsHelp visitedBranches unvisitedBranches finally =
350+ case unvisitedBranches of
351+ [] ->
352+ case finally of
342− A.A _ (MultiIf subBranches subFinally) ->
353+ A.A _ (MultiIf subBranches subFinally) ->
343− combineIfs (branches ++ subBranches) subFinally
354+ crushIfsHelp visitedBranches subBranches subFinally
355+
356+ _ ->
357+ (reverse visitedBranches, finally)
358+
359+ (A.A _ (Literal (Boolean True)), branch) : _ ->
360+ crushIfsHelp visitedBranches [] branch
361+
362+ (A.A _ (Var (Var.Canonical (Var.Module ["Basics"]) "otherwise")), branch) : _ ->
363+ crushIfsHelp visitedBranches [] branch
364+
365+ visiting : unvisited ->
366+ crushIfsHelp (visiting : visitedBranches) unvisited finally
344
367
345− _ ->
346− (branches, finally)
347
368
348
369
349-- DEFINITIONS
370-- DEFINITIONS