Skip to content

Commit 69a6feb

Browse files
committed
Move the code generation from the lexer to a compile function, courtesy Ilija.
1 parent 0f8b8b5 commit 69a6feb

File tree

5 files changed

+33
-13
lines changed

5 files changed

+33
-13
lines changed

Zend/zend_ast.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,17 +2211,10 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
22112211
zend_ast_export_var(str, ast->child[1], 0, indent);
22122212
break;
22132213
case ZEND_AST_CALL:
2214-
if (ast->attr & ZEND_CALL_SYNTAX_PIPE) {
2215-
zend_ast_export_ex(str, ast->child[1], 0, indent);
2216-
smart_str_appends(str, " |> ");
2217-
zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2218-
}
2219-
else {
2220-
zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2221-
smart_str_appendc(str, '(');
2222-
zend_ast_export_ex(str, ast->child[1], 0, indent);
2223-
smart_str_appendc(str, ')');
2224-
}
2214+
zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2215+
smart_str_appendc(str, '(');
2216+
zend_ast_export_ex(str, ast->child[1], 0, indent);
2217+
smart_str_appendc(str, ')');
22252218
break;
22262219
case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
22272220
smart_str_append(str, Z_STR_P(zend_ast_get_zval(ast->child[0])));

Zend/zend_ast.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ enum _zend_ast_kind {
153153
ZEND_AST_MATCH_ARM,
154154
ZEND_AST_NAMED_ARG,
155155
ZEND_AST_PARENT_PROPERTY_HOOK_CALL,
156+
ZEND_AST_PIPE,
156157

157158
/* 3 child nodes */
158159
ZEND_AST_METHOD_CALL = 3 << ZEND_AST_NUM_CHILDREN_SHIFT,

Zend/zend_compile.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6598,6 +6598,30 @@ static void zend_compile_match(znode *result, zend_ast *ast)
65986598
efree(jmp_end_opnums);
65996599
}
66006600

6601+
static void zend_compile_pipe(znode *result, zend_ast *ast)
6602+
{
6603+
zend_ast *operand_ast = ast->child[0];
6604+
zend_ast *callable_ast = ast->child[1];
6605+
6606+
znode operand_result;
6607+
zend_compile_expr(&operand_result, operand_ast);
6608+
6609+
/* Turn $foo |> bar(...) into bar($foo). */
6610+
if (callable_ast->kind == ZEND_AST_CALL
6611+
&& callable_ast->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) {
6612+
callable_ast = callable_ast->child[0];
6613+
}
6614+
6615+
znode callable_result;
6616+
zend_compile_expr(&callable_result, callable_ast);
6617+
6618+
zend_ast *fcall_ast = zend_ast_create(ZEND_AST_CALL,
6619+
zend_ast_create_znode(&callable_result),
6620+
zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&operand_result)));
6621+
6622+
zend_compile_expr(result, fcall_ast);
6623+
}
6624+
66016625
static void zend_compile_try(zend_ast *ast) /* {{{ */
66026626
{
66036627
zend_ast *try_ast = ast->child[0];
@@ -11603,6 +11627,9 @@ static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */
1160311627
case ZEND_AST_MATCH:
1160411628
zend_compile_match(result, ast);
1160511629
return;
11630+
case ZEND_AST_PIPE:
11631+
zend_compile_pipe(result, ast);
11632+
return;
1160611633
default:
1160711634
ZEND_ASSERT(0 /* not supported */);
1160811635
}

Zend/zend_compile.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,6 @@ ZEND_API zend_string *zend_type_to_string(zend_type type);
10301030
/* These should not clash with ZEND_ACC_PPP_MASK and ZEND_ACC_PPP_SET_MASK */
10311031
#define ZEND_PARAM_REF (1<<3)
10321032
#define ZEND_PARAM_VARIADIC (1<<4)
1033-
#define ZEND_CALL_SYNTAX_PIPE (1u << 2u)
10341033

10351034
#define ZEND_NAME_FQ 0
10361035
#define ZEND_NAME_NOT_FQ 1

Zend/zend_language_parser.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ expr:
12811281
| expr T_IS_NOT_EQUAL expr
12821282
{ $$ = zend_ast_create_binary_op(ZEND_IS_NOT_EQUAL, $1, $3); }
12831283
| expr T_PIPE expr
1284-
{ $$ = zend_ast_create(ZEND_AST_CALL, $3, zend_ast_create_list(1, ZEND_AST_ARG_LIST, $1) ); $$->attr = ZEND_CALL_SYNTAX_PIPE; }
1284+
{ $$ = zend_ast_create(ZEND_AST_PIPE, $1, $3); }
12851285
| expr '<' expr
12861286
{ $$ = zend_ast_create_binary_op(ZEND_IS_SMALLER, $1, $3); }
12871287
| expr T_IS_SMALLER_OR_EQUAL expr

0 commit comments

Comments
 (0)