warp icon indicating copy to clipboard operation
warp copied to clipboard

Add conditionals to literalExpressionEvaluator pass

Open piwonskp opened this issue 2 years ago • 5 comments
trafficstars

Currently conditional expressions (ternary operation) are not being evaluated in literalExpressionEvaluator pass.

Conditionals have the form: condition ? thenBranch : elseBranch. If the value of condition can be known during this pass, then the whole conditional expression can be replaced with the corresponding branch expression. In other words, if condition is evaluated to be true then : condition ? thenBranch : elseBranch can be replaced by thenBranch Similar thing happens in case it is evaluated to be false

piwonskp avatar Jan 15 '23 19:01 piwonskp

In which file can I find this?

yash251 avatar Jan 19 '23 16:01 yash251

Passes are stored in src/passes so you can find that in src/passes/literalExpressionEvaluator/literalExpressionEvaluator.ts

piwonskp avatar Jan 19 '23 23:01 piwonskp

Does the issue want us to replace if-else statements of expressions with conditional/ternary operators?

aniket2405 avatar Jan 30 '23 12:01 aniket2405

actually the changes have been made by someone else already. can close this issue.

yash251 avatar Jan 30 '23 13:01 yash251

Actually no, the issue hasn't been addressed yet.

This task requires the pass to visit Conditional nodes (which are the ones that represent ternary operations) and evaluate the condition expression. Since the condition expression should be a boolean value, its evaluation should return true, false or null (in case it wasn't possible to determine the value of the expression at this point). So, when the evaluation is different from null it is possible to replace the conditional node by the corresponding branch expression node.

I just updated the description to make it clearer

cicr99 avatar Jan 30 '23 15:01 cicr99