Make && and || by name
b-studios opened this issue · comments
We can make these two logical operators by name, by simply desugaring them to if
.
This could either happen before Typer (that is in Parser), or after Typer (that is in the translation to core).
EXPR1 || EXPR2 = if (EXPR1) true else EXPR2
EXPR1 && EXPR2 = if (EXPR1) EXPR2 else false
Doing it in Parser has the advantage that it is really just a few lines of code to implement.
Doing it in core.Transformer has the advantage that we can selectively perform this translation on effekt.infixOr
and effekt.InfixAnd
and the operators can be overloaded (for instance for logic programming).
We could also do the translation selectively and only translate it if EXPR2 is a Stmt or a side effecting Expr (EXPR1 does not matter, since it is forced anyway).
EXPR1 || EXPR2 = infixOr { () => EXPR1 } { () => EXPR2 }
EXPR1 && EXPR2 = infixAnd { () => EXPR1 } { () => EXPR2 }
def infixOr { first: () => Boolean } { second: () => Boolean }: Boolean =
if (first()) true else second()
The second is probably better. In the future, then, we need to optimize the inliner to make sure this is actually inlined.