Details
-
Task
-
Resolution: Fixed
-
Major - P3
-
None
-
None
Description
Add the following helper method:
var (var, simpleAst) = AstExpression.UseVarIfNotSimple(name, expression);
|
If the expression is "simple" return:
(null, expression) // null means no var is needed |
if the expression is not "simple" return:
(var, simpleAst)
|
where:
var = AstExpression.ComputedField(name, expression);
|
simpleAst = AstExpression.Field("$" + name); |
in other words, define a variable whose value is `expression` and create a reference to that variable.
This helper method will be used whenever there is a possibly complex expression that is used more than once in a translation. Rather than allowing the complex expression to appear more than once in the translation, the complex expression is assigned to a variable and then the variable is referenced as many times as necessary.
However, there is no need for the variable to be used when the expression is "simple". In that case the expression itself can be referenced multiple times with no performance or conceptual cost.
The current definition of "simple" is: null, a constant, or a variable reference. Everything else is "not simple".
Since `UseVarIfNotSimple` returns `null` if no variable is needed, change the behavior of `Let` to handle null vars differently and add a few new overloads:
AstExpression.Let(var, @in) |
AstExpression.Let(var1, var2, @in) |
AstExpression.Let(var1, var2, var3, @in) |
For each overload, if all vars are `null` no `$let` is needed and the `@in` expression is returned directly. If one or more vars are not `null` then a `$let` is created defining whichever vars are not null.