From 7ae7457cd096a13e2184b74dddb295f20ef1fe75 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Sun, 13 Dec 2020 14:18:24 -0600 Subject: [PATCH 1/6] First attempt --- Zend/tests/match/short-match.phpt | 16 ++++++++++++++++ Zend/zend_language_parser.y | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 Zend/tests/match/short-match.phpt diff --git a/Zend/tests/match/short-match.phpt b/Zend/tests/match/short-match.phpt new file mode 100644 index 0000000000000..a42e052d91477 --- /dev/null +++ b/Zend/tests/match/short-match.phpt @@ -0,0 +1,16 @@ +--TEST-- +Test multiple default arms in match in different arms +--FILE-- + 'small', + $a == 3 => 'medium', + default => 'large', +}; + +?> +--EXPECTF-- +medium diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y index ed800c46f867e..147399e89e207 100644 --- a/Zend/zend_language_parser.y +++ b/Zend/zend_language_parser.y @@ -652,7 +652,9 @@ case_separator: match: T_MATCH '(' expr ')' '{' match_arm_list '}' - { $$ = zend_ast_create(ZEND_AST_MATCH, $3, $6); }; + { $$ = zend_ast_create(ZEND_AST_MATCH, $3, $6); } + | T_MATCH '{' match_arm_list '}' + { $$ = zend_ast_create(ZEND_AST_MATCH, 'true', $3); } ; match_arm_list: From 76f0edb14278785566d976629ac64f453c9344e2 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Sun, 13 Dec 2020 17:18:16 -0600 Subject: [PATCH 2/6] Add support for short-handed match on true. --- Zend/tests/match/short-match.phpt | 2 +- Zend/zend_language_parser.y | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Zend/tests/match/short-match.phpt b/Zend/tests/match/short-match.phpt index a42e052d91477..6e3bab1e3324c 100644 --- a/Zend/tests/match/short-match.phpt +++ b/Zend/tests/match/short-match.phpt @@ -5,7 +5,7 @@ Test multiple default arms in match in different arms $a = 3; -match { +print match { $a < 2 => 'small', $a == 3 => 'medium', default => 'large', diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y index 147399e89e207..dbdc65cef5082 100644 --- a/Zend/zend_language_parser.y +++ b/Zend/zend_language_parser.y @@ -654,7 +654,10 @@ match: T_MATCH '(' expr ')' '{' match_arm_list '}' { $$ = zend_ast_create(ZEND_AST_MATCH, $3, $6); } | T_MATCH '{' match_arm_list '}' - { $$ = zend_ast_create(ZEND_AST_MATCH, 'true', $3); } + { + zval zv; + ZVAL_BOOL(&zv, 1); + $$ = zend_ast_create(ZEND_AST_MATCH, zend_ast_create_zval(&zv), $3); } ; match_arm_list: From 8608e2eb40c3bfe471d456c92028ceabf070704e Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Mon, 14 Dec 2020 11:24:38 -0600 Subject: [PATCH 3/6] Add flag for AST printer for short-match. --- Zend/tests/match/short-match.phpt | 2 +- Zend/zend_ast.c | 22 ++++++++++++++++------ Zend/zend_compile.h | 2 ++ Zend/zend_language_parser.y | 8 +++++--- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Zend/tests/match/short-match.phpt b/Zend/tests/match/short-match.phpt index 6e3bab1e3324c..bd2b36e397dfb 100644 --- a/Zend/tests/match/short-match.phpt +++ b/Zend/tests/match/short-match.phpt @@ -1,5 +1,5 @@ --TEST-- -Test multiple default arms in match in different arms +Short-hand match with implicit true subject. --FILE-- child[1], indent + 1); break; case ZEND_AST_MATCH: - smart_str_appends(str, "match ("); - zend_ast_export_ex(str, ast->child[0], 0, indent); - smart_str_appends(str, ") {\n"); - zend_ast_export_ex(str, ast->child[1], 0, indent + 1); - zend_ast_export_indent(str, indent); - smart_str_appendc(str, '}'); + if (ast->attr & ZEND_MATCH_SHORT) { + smart_str_appends(str, "match {\n"); +// zend_ast_export_ex(str, ast->child[0], 0, indent); +// smart_str_appends(str, ") {\n"); + zend_ast_export_ex(str, ast->child[1], 0, indent + 1); + zend_ast_export_indent(str, indent); + smart_str_appendc(str, '}'); + } + else { + smart_str_appends(str, "match ("); + zend_ast_export_ex(str, ast->child[0], 0, indent); + smart_str_appends(str, ") {\n"); + zend_ast_export_ex(str, ast->child[1], 0, indent + 1); + zend_ast_export_indent(str, indent); + smart_str_appendc(str, '}'); + } break; case ZEND_AST_MATCH_ARM: zend_ast_export_indent(str, indent); diff --git a/Zend/zend_compile.h b/Zend/zend_compile.h index b4af5d9e3fa12..efc1b3816dcc3 100644 --- a/Zend/zend_compile.h +++ b/Zend/zend_compile.h @@ -896,6 +896,8 @@ ZEND_API zend_string *zend_type_to_string(zend_type type); #define ZEND_PARAM_REF (1<<3) #define ZEND_PARAM_VARIADIC (1<<4) +#define ZEND_MATCH_SHORT (1<<5) + #define ZEND_NAME_FQ 0 #define ZEND_NAME_NOT_FQ 1 #define ZEND_NAME_RELATIVE 2 diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y index dbdc65cef5082..df0f032f6f87d 100644 --- a/Zend/zend_language_parser.y +++ b/Zend/zend_language_parser.y @@ -655,9 +655,11 @@ match: { $$ = zend_ast_create(ZEND_AST_MATCH, $3, $6); } | T_MATCH '{' match_arm_list '}' { - zval zv; - ZVAL_BOOL(&zv, 1); - $$ = zend_ast_create(ZEND_AST_MATCH, zend_ast_create_zval(&zv), $3); } + zval zv; + ZVAL_BOOL(&zv, 1); + $$ = zend_ast_create(ZEND_AST_MATCH, zend_ast_create_zval(&zv), $3); + $$->attr = ZEND_MATCH_SHORT; + } ; match_arm_list: From 4d6f13f736f72752c920a28705cd182e3b14e0fb Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Tue, 15 Dec 2020 11:27:54 -0600 Subject: [PATCH 4/6] Add missing test. --- Zend/tests/match/short-match-ast.phpt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Zend/tests/match/short-match-ast.phpt diff --git a/Zend/tests/match/short-match-ast.phpt b/Zend/tests/match/short-match-ast.phpt new file mode 100644 index 0000000000000..edb5662b97ae4 --- /dev/null +++ b/Zend/tests/match/short-match-ast.phpt @@ -0,0 +1,26 @@ +--TEST-- +Short-hand match with implicit true subject, AST printing. +--INI-- +assert.exception=0 +--FILE-- + 'small', + $a == 3 => 'medium', + default => 'large', + }; +})()); + +?> +--EXPECTF-- +Warning: assert(): assert(function () { + $a = 3; + match { + $a < 2 => 'small', + $a == 3 => 'medium', + default => 'large', + }; +}()) failed in %s on line %d From c2fe4ba6c0889f97f3fec83802bd90c48d5e0035 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Tue, 15 Dec 2020 11:28:07 -0600 Subject: [PATCH 5/6] Remove vestigial code. --- Zend/zend_ast.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c index 7adce461b6f78..e51bf85447dc2 100644 --- a/Zend/zend_ast.c +++ b/Zend/zend_ast.c @@ -1981,8 +1981,6 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio case ZEND_AST_MATCH: if (ast->attr & ZEND_MATCH_SHORT) { smart_str_appends(str, "match {\n"); -// zend_ast_export_ex(str, ast->child[0], 0, indent); -// smart_str_appends(str, ") {\n"); zend_ast_export_ex(str, ast->child[1], 0, indent + 1); zend_ast_export_indent(str, indent); smart_str_appendc(str, '}'); From b7b3464ffc2783cbea6a1eadd25acbd8402df304 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Tue, 15 Dec 2020 11:34:53 -0600 Subject: [PATCH 6/6] Shorten the branches. --- Zend/zend_ast.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c index e51bf85447dc2..fff981c2f16f6 100644 --- a/Zend/zend_ast.c +++ b/Zend/zend_ast.c @@ -1981,18 +1981,15 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio case ZEND_AST_MATCH: if (ast->attr & ZEND_MATCH_SHORT) { smart_str_appends(str, "match {\n"); - zend_ast_export_ex(str, ast->child[1], 0, indent + 1); - zend_ast_export_indent(str, indent); - smart_str_appendc(str, '}'); } else { smart_str_appends(str, "match ("); zend_ast_export_ex(str, ast->child[0], 0, indent); smart_str_appends(str, ") {\n"); - zend_ast_export_ex(str, ast->child[1], 0, indent + 1); - zend_ast_export_indent(str, indent); - smart_str_appendc(str, '}'); } + zend_ast_export_ex(str, ast->child[1], 0, indent + 1); + zend_ast_export_indent(str, indent); + smart_str_appendc(str, '}'); break; case ZEND_AST_MATCH_ARM: zend_ast_export_indent(str, indent);