-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GDScript: Implement pattern guards for match statement
Within a match statement, it is now possible to add guards in each branch: var a = 0 match a: 0 when false: print("does not run") 0 when true: print("but this does") This allows more complex logic for deciding which branch to take.
- Loading branch information
Showing
15 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +105,7 @@ class GDScriptTokenizer { | |
PASS, | ||
RETURN, | ||
MATCH, | ||
WHEN, | ||
// Keywords | ||
AS, | ||
ASSERT, | ||
|
4 changes: 4 additions & 0 deletions
4
modules/gdscript/tests/scripts/analyzer/errors/match_guard_invalid_expression.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
func test(): | ||
match 0: | ||
_ when a == 0: | ||
print("a does not exist") |
2 changes: 2 additions & 0 deletions
2
modules/gdscript/tests/scripts/analyzer/errors/match_guard_invalid_expression.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
GDTEST_ANALYZER_ERROR | ||
Identifier "a" not declared in the current scope. |
5 changes: 5 additions & 0 deletions
5
modules/gdscript/tests/scripts/parser/errors/match_guard_with_assignment.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
func test(): | ||
var a = 0 | ||
match a: | ||
0 when a = 1: | ||
print("assignment not allowed on pattern guard") |
2 changes: 2 additions & 0 deletions
2
modules/gdscript/tests/scripts/parser/errors/match_guard_with_assignment.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
GDTEST_PARSER_ERROR | ||
Assignment is not allowed inside an expression. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,7 @@ func test(): | |
|
||
var TAU = "TAU" | ||
print(TAU) | ||
|
||
# New keyword for pattern guards. | ||
var when = "when" | ||
print(when) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ PI | |
INF | ||
NAN | ||
TAU | ||
when |
71 changes: 71 additions & 0 deletions
71
modules/gdscript/tests/scripts/runtime/features/match_with_pattern_guards.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
var global := 0 | ||
|
||
func test(): | ||
var a = 0 | ||
var b = 1 | ||
|
||
match a: | ||
0 when b == 0: | ||
print("does not run" if true else "") | ||
0 when b == 1: | ||
print("guards work") | ||
_: | ||
print("does not run") | ||
|
||
match a: | ||
var a_bind when b == 0: | ||
prints("a is", a_bind, "and b is 0") | ||
var a_bind when b == 1: | ||
prints("a is", a_bind, "and b is 1") | ||
_: | ||
print("does not run") | ||
|
||
match a: | ||
var a_bind when a_bind < 0: | ||
print("a is less than zero") | ||
var a_bind when a_bind == 0: | ||
print("a is equal to zero") | ||
_: | ||
print("a is more than zero") | ||
|
||
match [1, 2, 3]: | ||
[1, 2, var element] when element == 0: | ||
print("does not run") | ||
[1, 2, var element] when element == 3: | ||
print("3rd element is 3") | ||
|
||
match a: | ||
_ when b == 0: | ||
print("does not run") | ||
_ when b == 1: | ||
print("works with wildcard too.") | ||
_: | ||
print("does not run") | ||
|
||
match a: | ||
0, 1 when b == 0: | ||
print("does not run") | ||
0, 1 when b == 1: | ||
print("guard with multiple patterns") | ||
_: | ||
print("does not run") | ||
|
||
match a: | ||
0 when b == 0: | ||
print("does not run") | ||
0: | ||
print("regular pattern after guard mismatch") | ||
|
||
match a: | ||
1 when side_effect(): | ||
print("should not run the side effect call") | ||
0 when side_effect(): | ||
print("will run the side effect call, but not this") | ||
_: | ||
assert(global == 1) | ||
print("side effect only ran once") | ||
|
||
func side_effect(): | ||
print("side effect") | ||
global += 1 | ||
return false |
10 changes: 10 additions & 0 deletions
10
modules/gdscript/tests/scripts/runtime/features/match_with_pattern_guards.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
GDTEST_OK | ||
guards work | ||
a is 0 and b is 1 | ||
a is equal to zero | ||
3rd element is 3 | ||
works with wildcard too. | ||
guard with multiple patterns | ||
regular pattern after guard mismatch | ||
side effect | ||
side effect only ran once |