1. PHP Version: PHP 7.3.0-dev (CLI) (Built:mar 2018 00:28:55) (NTS)
2. Unless
Syntax structure:
Unless ($cond) {
Statement
}
Grammatical effects:
Executes the statement statement when the $cond is false.
3. Code modification
Lexical analysis file: ZEND/ZEND_LANGUAGE_SCANNER.L, about 1195 of the place, add 5-7 rows, 13-15 lines of content:
1<ST_IN_SCRIPTING> "while" {2 Return_token (t_while);3 }4 5 <ST_IN_SCRIPTING> "unless" { 6 Return_token (t_unless); 7 } 8 9<ST_IN_SCRIPTING> "Endwhile" {Ten Return_token (t_endwhile); One } A <ST_IN_SCRIPTING> "endunless" { return_token (t_endunless); }
Parsing files: zend/zend_language_parser.y, adding content:
File about 271 lines, add t_unless and t_endunless content:
1Reserved_non_modifiers:2T_include | T_include_once | T_eval | T_require | T_require_once | T_logical_or | T_logical_xor |T_logical_and3| t_instanceof | T_new | T_clone | T_exit | t_if | T_elseif | T_else | T_endif | T_echo | T_do | T_while |T_endwhile4| T_for | T_endfor | T_foreach | T_endforeach | T_declare | T_enddeclare | T_as | T_try | T_catch |t_finally5| T_throw | T_use | T_insteadof | T_global | T_var | T_unset | T_isset | T_empty | T_continue |T_goto6| t_function | T_const | T_return | T_print | T_yield | T_list | T_switch | T_endswitch | T_case | T_default |T_break7| T_array | t_callable | T_extends | t_implements | T_namespace | t_trait | T_interface |T_class8| T_class_c | T_trait_c | T_func_c | T_method_c | T_line | T_file | T_dir | T_ns_c |t_unless | t_endunless 9;
File about 152 lines, add the contents of rows 2 and 4:
1 %token t_while "while (t_while)"2 %token t_unless "unless (t_unless)"3 %token t_endwhile "Endwhile (t_endwhile)"4 %token t_endunless "endunless (t_endunless)"
About 235 lines, add Unless_statement:
Unless_statement
At approximately 428 lines, add the contents of the Rows 3 and 4:
1 | T_while ' (' expr ') ' while_statement2 {$$ = Zend_ast_create (Zend_ast_while, $ $, $);} 3 | T_unless ' (' expr ') ' unless_statement4 {$$ = Zend_ast_create (zend_ast_unless, $ $, $);}
About 598 lines, add 6-9 lines of content:
1 while_statement:2 statement {$$ = $;} 3 | ': ' inner_statement_list t_endwhile '; ' {$$ = $;} 4 ; 5 6 unless_statement:7 statement {$$= $8 | ': ' Inner_statement_list t_endunless '; ' {$$ = $;} 9 ;
In the zend/zend_ast.h header file, about 134 lines, add 2 lines of content:
1 Zend_ast_group_use,2 zend_ast_unless,34 /* * /
In the zend/zend_ast.c file, within the function zend_ast_export_stmt, about 915 line, add 7 rows of content:
1Zend_ast_export_indent (str,indent);2Zend_ast_export_indent (str,indent);3ZEND_AST_EXPORT_EX (str, AST, 0,indent);4 Switch(ast->kind) {5 CaseZend_ast_label:6 CaseZEND_AST_IF: 7 case zend_ast_unless: 8 CaseZend_ast_switch:9 CaseZend_ast_while:Ten CaseZend_ast_try: One CaseZend_ast_for: A CaseZend_ast_foreach: - CaseZEND_AST_FUNC_DECL: - CaseZend_ast_method: the CaseZend_ast_class: - CaseZend_ast_use_trait: - CaseZend_ast_namespace: - CaseZend_ast_declare: + Break; - default: +SMART_STR_APPENDC (str, '; ')); A Break; at } -SMART_STR_APPENDC (str, ' \ n ');
zend/zend_compile.c file,
About 4667 rows, add the zend_compile_ unless function:
1void Zend_compile_unless (Zend_ast *AST)2 {3Zend_ast *cond_ast = ast->child[0];4Zend_ast *stmt_ast = ast->child[1];5 Znode Cond_node;6 7 uint32_t Opnum_cond;8 9Zend_compile_expr (&cond_node,cond_ast);Ten OneOpnum_cond = Zend_emit_cond_jump (zend_jmpnz,&cond_node,0); A - zend_compile_stmt (stmt_ast); - the Zend_update_jump_target_to_next (opnum_cond); -}
In the ZEND_COMPILE_STMT function, approximately 8200 lines are located:
1 Case Zend_ast_if:2 zend_compile_if (AST); 3 Break ; 4 case zend_ast_unless:5 zend_compile_unless (AST); 6 break;
4. Remove the zend_language_parser.c and zend_language_scanner.c files from the Zend directory, recompile, and install PHP.
5. Test the code:
1 <? PHP 2 unless (0)3{4 echo "Hello the world!\n"; 5 }67 unless (0):8 echo "Hello the world!\n"; 9 endunless;
Output:
1 [email protected]:~/tmp#2 Hello the world! 3 Hello the world!
Reference:
Http://www.laruence.com/2010/07/16/1648.html
PHP joins unless syntax