Bladeren bron

added pf_for_to_while

Nicolas Cannasse 9 jaren geleden
bovenliggende
commit
f25e4033b7
2 gewijzigde bestanden met toevoegingen van 31 en 11 verwijderingen
  1. 15 10
      common.ml
  2. 16 1
      typer.ml

+ 15 - 10
common.ml

@@ -98,6 +98,8 @@ type platform_config = {
 	pf_can_skip_non_nullable_argument : bool;
 	(** type paths that are reserved on the platform *)
 	pf_reserved_type_paths : path list;
+	(** transform for in the corresponding while *)
+	pf_for_to_while : bool;
 }
 
 type display_mode =
@@ -555,6 +557,7 @@ let default_config =
 		pf_pattern_matching = false;
 		pf_can_skip_non_nullable_argument = true;
 		pf_reserved_type_paths = [];
+		pf_for_to_while = false;
 	}
 
 let get_config com =
@@ -564,6 +567,7 @@ let get_config com =
 		default_config
 	| Js ->
 		{
+			default_config with
 			pf_static = false;
 			pf_sys = false;
 			pf_locals_scope = false;
@@ -579,6 +583,7 @@ let get_config com =
 		}
 	| Neko ->
 		{
+			default_config with
 			pf_static = false;
 			pf_sys = true;
 			pf_locals_scope = true;
@@ -594,6 +599,7 @@ let get_config com =
 		}
 	| Flash when defined Define.As3 ->
 		{
+			default_config with
 			pf_static = true;
 			pf_sys = false;
 			pf_locals_scope = false;
@@ -609,6 +615,7 @@ let get_config com =
 		}
 	| Flash ->
 		{
+			default_config with
 			pf_static = true;
 			pf_sys = false;
 			pf_locals_scope = true;
@@ -624,6 +631,7 @@ let get_config com =
 		}
 	| Php ->
 		{
+			default_config with
 			pf_static = false;
 			pf_sys = true;
 			pf_locals_scope = false; (* some duplicate work is done in genPhp *)
@@ -639,6 +647,7 @@ let get_config com =
 		}
 	| Cpp ->
 		{
+			default_config with
 			pf_static = true;
 			pf_sys = true;
 			pf_locals_scope = true;
@@ -654,6 +663,7 @@ let get_config com =
 		}
 	| Cs ->
 		{
+			default_config with
 			pf_static = true;
 			pf_sys = true;
 			pf_locals_scope = false;
@@ -669,6 +679,7 @@ let get_config com =
 		}
 	| Java ->
 		{
+			default_config with
 			pf_static = true;
 			pf_sys = true;
 			pf_locals_scope = false;
@@ -684,6 +695,7 @@ let get_config com =
 		}
 	| Python ->
 		{
+			default_config with
 			pf_static = false;
 			pf_sys = true;
 			pf_locals_scope = false;
@@ -699,18 +711,11 @@ let get_config com =
 		}
 	| Hl ->
 		{
-			pf_static = true;
-			pf_sys = true;
-			pf_locals_scope = true;
-			pf_captured_scope = true;
-			pf_unique_locals = false;
+			default_config with
 			pf_capture_policy = CPWrapRef;
-			pf_pad_nulls = false;
-			pf_add_final_return = false;
-			pf_overload = false;
-			pf_pattern_matching = false;
+			pf_pad_nulls = true;
 			pf_can_skip_non_nullable_argument = false;
-			pf_reserved_type_paths = [];
+			pf_for_to_while = true;
 		}
 
 let memory_marker = [|Unix.time()|]

+ 16 - 1
typer.ml

@@ -3166,7 +3166,22 @@ and type_expr ctx (e,p) (with_type:with_type) =
 					)
 				) in
 				let e2 = type_expr ctx e2 NoValue in
-				(try Optimizer.optimize_for_loop_iterator ctx i e1 e2 p with Exit -> mk (TFor (i,e1,e2)) ctx.t.tvoid p)
+				(try
+					Optimizer.optimize_for_loop_iterator ctx i e1 e2 p
+				with Exit ->
+					if ctx.com.config.pf_for_to_while then begin
+						let v = gen_local ctx e1.etype in
+						mk (TBlock [
+							mk (TVar (v,Some e1)) ctx.t.tvoid p;
+							mk (TWhile (build_call ctx (type_field ctx (mk (TLocal v) v.v_type p) "hasNext" p MCall) [] Value p,
+								mk (TBlock [
+									mk (TVar (i, Some (build_call ctx (type_field ctx (mk (TLocal v) v.v_type p) "next" p MCall) [] Value p))) ctx.t.tvoid p;
+									e2
+								]) ctx.t.tvoid p
+ 							,NormalWhile)) ctx.t.tvoid p;
+						]) ctx.t.tvoid p
+					end else
+						mk (TFor (i,e1,e2)) ctx.t.tvoid p)
 		) in
 		ctx.in_loop <- old_loop;
 		old_locals();