Skip to content

Commit 095d538

Browse files
committed
Implement match blocks
1 parent 1bddd4e commit 095d538

File tree

8 files changed

+320
-83
lines changed

8 files changed

+320
-83
lines changed

Zend/tests/match/block_001.phpt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Match blocks
3+
--FILE--
4+
<?php
5+
function foo() {
6+
echo "foo()\n";
7+
}
8+
9+
function bar() {
10+
return 3;
11+
}
12+
13+
function test($value) {
14+
var_dump(match ($value) {
15+
1 { 1 },
16+
2 { $x = 2; $x },
17+
3 {
18+
foo();
19+
bar()
20+
},
21+
});
22+
}
23+
24+
test(1);
25+
test(2);
26+
test(3);
27+
?>
28+
--EXPECT--
29+
int(1)
30+
int(2)
31+
foo()
32+
int(3)

Zend/tests/match/block_002.phpt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
Match blocks
3+
--FILE--
4+
<?php
5+
6+
function throw_($value) {
7+
var_dump([new \stdClass] + match ($value) {
8+
1 { throw new Exception('Exception with live var'); [] },
9+
});
10+
}
11+
12+
function return_($value) {
13+
var_dump([new \stdClass] + match ($value) {
14+
1 { return 42; [] },
15+
});
16+
}
17+
18+
function goto_($value) {
19+
var_dump([new \stdClass] + match ($value) {
20+
1 { goto end; [] },
21+
});
22+
end:
23+
return 42;
24+
}
25+
26+
try {
27+
throw_(1);
28+
} catch (Exception $e) {
29+
echo $e->getMessage(), "\n";
30+
}
31+
32+
var_dump(return_(1));
33+
var_dump(goto_(1));
34+
35+
?>
36+
--EXPECT--
37+
Exception with live var
38+
int(42)
39+
int(42)

Zend/tests/match/block_003.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Match blocks
3+
--XFAIL--
4+
--FILE--
5+
<?php
6+
7+
class Foo {
8+
public function bar($value) {}
9+
}
10+
11+
function foo() {
12+
return new Foo();
13+
}
14+
15+
function test() {
16+
foo()?->bar(match (1) {
17+
1 { return 2; null }
18+
});
19+
}
20+
21+
var_dump(test());
22+
23+
?>
24+
--EXPECT--
25+
int(2)

Zend/zend_ast.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ enum _zend_ast_kind {
148148
ZEND_AST_ATTRIBUTE,
149149
ZEND_AST_MATCH,
150150
ZEND_AST_MATCH_ARM,
151+
ZEND_AST_MATCH_ARM_BLOCK,
151152
ZEND_AST_NAMED_ARG,
152153

153154
/* 3 child nodes */

0 commit comments

Comments
 (0)