Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GDScript: support variable definitions in if-statement #98335

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zjin123
Copy link

@zjin123 zjin123 commented Oct 19, 2024

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 of IfNode is replaced with a list of either ExpressionNode or VariableNode. 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, one ExpressionNode is created. For a variable definition, one VariableNode and one ExpressionNode 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 to write_else(int count).

@KoBeWi
Copy link
Member

KoBeWi commented Oct 21, 2024

I'm not sure about the comma syntax, it's basically a weird and that allows multiple definitions. I think single definition is enough in most cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow to define variables inside conditions
3 participants