Browse Source

Lua: Improve while/continue behavior.

The while loops use a lua goto label to function as a "continue"
statement.  Previously, I was using a counter to make each label unique.
 This isn't necessary since the label is scoped to the current block.

 Also, Simn gave a nice way to avoid generating the label if continue is
 never used in the block, which cleans up the code nicely.
Justin Donaldson 10 years ago
parent
commit
c54a7379cc
1 changed files with 7 additions and 15 deletions
  1. 7 15
      genlua.ml

+ 7 - 15
genlua.ml

@@ -39,7 +39,6 @@ type ctx = {
 	mutable iife_assign : bool;
 	mutable handle_break : bool;
 	mutable id_counter : int;
-	mutable continue_counter : int;
 	mutable type_accessor : module_type -> string;
 	mutable separator : bool;
 	mutable found_expose : bool;
@@ -469,7 +468,7 @@ and gen_expr ctx e =
 		if ctx.handle_break then spr ctx "throw \"__break__\"" else spr ctx "break"
 	| TContinue ->
 		if not ctx.in_loop then unsupported e.epos;
-		print ctx "goto _hx_continue_%i" ctx.continue_counter;
+		spr ctx "goto _hx_continue";
 	| TBlock el ->
 		let bend = open_block ctx in
 		List.iter (gen_block_element ctx) el;
@@ -596,31 +595,26 @@ and gen_expr ctx e =
 		spr ctx (Ast.s_unop op)
 	| TWhile (cond,e,Ast.NormalWhile) ->
 		let handle_break = handle_break ctx e in
-		ctx.continue_counter <- ctx.continue_counter + 1;
-		let id = ctx.continue_counter in
 		spr ctx "while ";
 		gen_cond ctx cond;
 		spr ctx " do ";
 		gen_block_element ctx e;
 		handle_break();
-		newline ctx;
 		(* TODO: generate this label conditionally *)
-		if has_continue e then
-		    print ctx "::_hx_continue_%i::" id;
+		if has_continue e then begin
+		    newline ctx;
+		    spr ctx "::_hx_continue::";
+		end;
 		newline ctx;
 		spr ctx " end ";
 	| TWhile (cond,e,Ast.DoWhile) ->
 		let handle_break = handle_break ctx e in
-		ctx.continue_counter <- ctx.continue_counter + 1;
-		let id = ctx.continue_counter in
 		spr ctx "while true do ";
 		gen_expr ctx e;
 		spr ctx "break end";
 		newline ctx;
-		print ctx "::_hx_continue_%i::" id;
+		spr ctx "::_hx_continue::";
 		newline ctx;
-		ctx.continue_counter <- ctx.continue_counter + 1;
-		let id2 = ctx.continue_counter in
 		spr ctx " while ";
 		gen_cond ctx cond;
 		spr ctx " do ";
@@ -628,7 +622,7 @@ and gen_expr ctx e =
 		handle_break();
 		newline ctx;
 		(* TODO: generate this label conditionally *)
-		print ctx "::_hx_continue_%i::" id2;
+		spr ctx "::_hx_continue::";
 		newline ctx;
 		spr ctx " end ";
 	| TObjectDecl fields ->
@@ -1159,7 +1153,6 @@ let generate_class___name__ ctx c =
 let generate_class ctx c =
 	ctx.current <- c;
 	ctx.id_counter <- 0;
-	ctx.continue_counter <- 0;
 	(match c.cl_path with
 	| [],"Function" -> error "This class redefines a native one" c.cl_pos
 	| _ -> ());
@@ -1413,7 +1406,6 @@ let alloc_ctx com =
 		in_loop = false;
 		handle_break = false;
 		id_counter = 0;
-		continue_counter = 0;
 		type_accessor = (fun _ -> assert false);
 		separator = false;
 		found_expose = false;