浏览代码

[php] move Syntax.coalsce() implementation to the generator

Alexander Kuzmenko 7 年之前
父节点
当前提交
f79d1cb539
共有 2 个文件被更改,包括 15 次插入3 次删除
  1. 14 0
      src/generators/genphp7.ml
  2. 1 3
      std/php/Syntax.hx

+ 14 - 0
src/generators/genphp7.ml

@@ -418,6 +418,7 @@ let needs_dereferencing for_assignment expr =
 			| TCall ({ eexpr = TField (_, FStatic ({ cl_path = syntax_type_path }, { cf_name = name })) }, _) ->
 			| TCall ({ eexpr = TField (_, FStatic ({ cl_path = syntax_type_path }, { cf_name = name })) }, _) ->
 				(match name with
 				(match name with
 					| "codeDeref" -> for_assignment
 					| "codeDeref" -> for_assignment
+					| "coalesce" -> for_assignment
 					| _ -> false
 					| _ -> false
 				)
 				)
 			| _ -> false
 			| _ -> false
@@ -2397,6 +2398,7 @@ class code_writer (ctx:Common.context) hx_type_path php_name =
 			in
 			in
 			match name with
 			match name with
 				| "code" | "codeDeref" -> self#write_expr_syntax_code args
 				| "code" | "codeDeref" -> self#write_expr_syntax_code args
+				| "coalesce" -> self#write_expr_syntax_coalesce args
 				| "instanceof" -> self#write_expr_syntax_instanceof args
 				| "instanceof" -> self#write_expr_syntax_instanceof args
 				| "nativeClassName" -> self#write_expr_syntax_native_class_name args
 				| "nativeClassName" -> self#write_expr_syntax_native_class_name args
 				| "foreach" -> self#write_expr_syntax_foreach args
 				| "foreach" -> self#write_expr_syntax_foreach args
@@ -2544,6 +2546,18 @@ class code_writer (ctx:Common.context) hx_type_path php_name =
 			self#write "(";
 			self#write "(";
 			write_args self#write (fun e -> self#write_expr e) args;
 			write_args self#write (fun e -> self#write_expr e) args;
 			self#write ")"
 			self#write ")"
+		(**
+			Writes `$left ?? $right` expression to output buffer (for `php.Syntax.coalesce()`)
+		*)
+		method write_expr_syntax_coalesce args =
+			match args with
+				| left :: right :: [] ->
+					self#write "(";
+					self#write_expr left;
+					self#write " ?? ";
+					self#write_expr right;
+					self#write ")";
+				| _ -> fail self#pos __POS__
 		(**
 		(**
 			Writes `instanceof` expression to output buffer (for `php.Syntax.instanceof()`)
 			Writes `instanceof` expression to output buffer (for `php.Syntax.instanceof()`)
 		*)
 		*)

+ 1 - 3
std/php/Syntax.hx

@@ -41,9 +41,7 @@ extern class Syntax {
     /**
     /**
         Generates `$left ?? $right`
         Generates `$left ?? $right`
     **/
     **/
-    static inline function coalesce<T>( left:T, right:T ) : T {
-        return codeDeref('({0} ?? {1})', left, right);
-    }
+    static function coalesce<T>( left:T, right:T ) : T ;
 
 
     /**
     /**
         Generates `$left . $right`
         Generates `$left . $right`