GDScript: support variable definitions in if-statement #98335
+394
−102
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Another attempt to implement godotengine/godot-proposals#2727 for if-statement. This patch implements two functions: allow variable definition as condition (
if var n := foo():
) and allow multiple conditions separated by comma in a single if-statement(if foo(), var n := bar(), n > 1:
).The
ExpressionNode *condition
field ofIfNode
is replaced with a list of eitherExpressionNode
orVariableNode
. When parsing a single if-statement, a temporary block(SuiteNode
) is created for parsing multiple conditions within and then the true block within. For an expression condition, oneExpressionNode
is created. For a variable definition, oneVariableNode
and oneExpressionNode
are created. The former one is used for generating code for initial assignment while the later one is used for testing as usual.Compilation is done like usual except that, all of the not-true-jump-address for each condition will be patched to the same starting address else/elif if any, otherwise the end address of the if statement. Thus, the
write_else()
interface is changed towrite_else(int count)
.