Browse Source

Lua : refactor complicated array unop logic

Justin Donaldson 10 years ago
parent
commit
5a3be4b4bb
1 changed files with 29 additions and 53 deletions
  1. 29 53
      genlua.ml

+ 29 - 53
genlua.ml

@@ -561,61 +561,37 @@ and gen_expr ?(local=true) ctx e =
 		ctx.iife_assign <- false;
 	| TUnop ((Increment|Decrement) as op,unop_flag, e) ->
 		spr ctx "(function() ";
-		(match unop_flag with
-		| Ast.Prefix ->
-			(match e.eexpr with
-			|TArray(e1,e2) ->
-			    spr ctx "local _idx = ";
-			    gen_value ctx e2;
-			    spr ctx " local ret =";
-			    gen_value ctx e1; spr ctx "[_idx]";
-			    gen_value ctx e1; spr ctx "[_idx] = ret";
-			    (match op with
-			    |Increment -> spr ctx " +"
-			    |Decrement -> spr ctx " -"
-			    |_-> print ctx " %s" (Ast.s_unop op));
-			    spr ctx " 1 return ";
-			    gen_value ctx e1; spr ctx "[_idx]";
-			    spr ctx "end)()";
-			| _ ->
-			    gen_value ctx e;
-			    spr ctx " = ";
-			    gen_value ctx e;
-			    (match op with
-			    |Increment -> spr ctx " +"
-			    |Decrement -> spr ctx " -"
-			    |_-> print ctx " %s" (Ast.s_unop op));
-			    spr ctx " 1 return ";
-			    gen_value ctx e;
-			    spr ctx " end)()";
-			);
-		| Ast.Postfix ->
-			(match e.eexpr with
-			|TArray(e1,e2) ->
-			    spr ctx "local _idx = ";
-			    gen_value ctx e2;
-			    spr ctx " local ret =";
+		(match e.eexpr, unop_flag with 
+		    | TArray(e1,e2), _ ->
+			spr ctx "local _idx = ";
+			gen_value ctx e2;
+			spr ctx " local _ =";
+			gen_value ctx e1; spr ctx "[_idx]";
+			gen_value ctx e1; spr ctx "[_idx] = _";
+		    | _, Ast.Prefix -> 
+			gen_value ctx e;
+			spr ctx " = ";
+			gen_value ctx e;
+		    | _, Ast.Postfix -> 
+			spr ctx "local _ = ";
+			gen_value ctx e; spr ctx "; "; 
+			gen_value ctx e;
+			spr ctx " = ";
+			gen_value ctx e);
+		(match op with
+		    |Increment -> spr ctx " + 1;"
+		    |Decrement -> spr ctx " - 1;"
+		    |_-> print ctx " %s 1;" (Ast.s_unop op));
+		spr ctx " return ";
+		(match unop_flag, e.eexpr with
+		    | Ast.Prefix, TArray(e1,e2) -> 
 			    gen_value ctx e1; spr ctx "[_idx]";
-			    gen_value ctx e1; spr ctx "[_idx] = ret";
-			    (match op with
-			    |Increment -> spr ctx " +"
-			    |Decrement -> spr ctx " -"
-			    |_-> print ctx " %s" (Ast.s_unop op));
-			    spr ctx " 1 return ret end)()";
-			| _ ->
-			    spr ctx "local _ = ";
-			    gen_value ctx e;
-			    newline ctx;
+		    | Ast.Prefix, _ ->
 			    gen_value ctx e;
-			    spr ctx " = ";
-			    gen_value ctx e;
-			    (match op with
-			    |Increment -> spr ctx " +"
-			    |Decrement -> spr ctx " -"
-			    |_-> print ctx " %s" (Ast.s_unop op));
-			    spr ctx " 1 return _ end)()";
-			);
-		)
+		    | Ast.Postfix, _ -> 
+			    spr ctx "_");
+		semicolon ctx;
+		spr ctx " end)()";
 	| TUnop (Not,unop_flag,e) ->
 		spr ctx "not ";
 		gen_value ctx e;