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 declarations as condition for if-statement #98000

Closed
wants to merge 1 commit into from

Conversation

zjin123
Copy link

@zjin123 zjin123 commented Oct 9, 2024

Implement godotengine/godot-proposals#2727 for if-statement.

This implementation is simple and intuitive but not elegant.

Idea is to parse code like

if var a := foo():
    do_something(a)
else:
    do_other_thing()

if a > b, var c := bar(a, b), c > d:
    do_something(c)
else:
    do_other_thing()

into code llike

if true:
    var a := foo()
    if a:                           # jump address when !a will be patched to
        do_something(a)
else:                               # ... here
    do_other_thing()

if true:
    if a > b:                       # all of jump address when !(a>b)
        var c := bar(a, b)
        if c:                       # ... and jump address when !c
            if c > d:               # ... and jump address when !(c>d)
                do_something(c)
else:                               # ... will be patched to here
    do_other_thing()

Such transform will guarantee short-circuit evaluation and scope visibility.

Semantics will be ensured by patching jump addresses (as above) duration compilation.

@zjin123
Copy link
Author

zjin123 commented Oct 9, 2024

It appears generating IfNode for each condition breaks warning annotation function for all if-statement. This implementation may not be suitable.

@zjin123 zjin123 closed this Oct 9, 2024
@AThousandShips AThousandShips removed this from the 4.x milestone Oct 9, 2024
@zjin123 zjin123 deleted the if-var branch October 24, 2024 09:41
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.

3 participants