Pārlūkot izejas kodu

optimized code generated for underscore in enum matches

Nicolas Cannasse 19 gadi atpakaļ
vecāks
revīzija
cd88c450d0
6 mainītis faili ar 34 papildinājumiem un 21 dzēšanām
  1. 1 0
      doc/CHANGES.txt
  2. 10 7
      genjs.ml
  3. 11 7
      genneko.ml
  4. 7 4
      genswf8.ml
  5. 1 1
      type.ml
  6. 4 2
      typer.ml

+ 1 - 0
doc/CHANGES.txt

@@ -15,6 +15,7 @@
 	added signatures, Iterator is now a signature
 	getter/setter and public/private unification
 	allowed anonymous declaration with interface-style syntax
+	optimized code generated for underscore in enum matches
 
 2006-05-02: RC1
 	added the Socket class

+ 10 - 7
genjs.ml

@@ -368,13 +368,16 @@ and gen_expr ctx e =
 			(match params with 
 			| None | Some [] -> ()
 			| Some l -> 
-				let n = ref 1 in
-				spr ctx "var ";
-				concat ctx ", " (fun (v,_) -> 
-					print ctx "%s = $e[%d]" v (!n);
-					incr n;
-				) l;
-				newline ctx);
+				let n = ref 0 in
+				let l = List.fold_left (fun acc (v,_) -> incr n; match v with None -> acc | Some v -> (v,!n) :: acc) [] l in
+				match l with
+				| [] -> ()
+				| l ->
+					spr ctx "var ";
+					concat ctx ", " (fun (v,n) -> 
+						print ctx "%s = $e[%d]" v n;
+					) l;
+					newline ctx);
 			gen_expr ctx (block e);
 			print ctx "break";
 			newline ctx

+ 11 - 7
genneko.ml

@@ -54,7 +54,7 @@ let add_local ctx v p =
 			(match eo with None -> () | Some e -> loop flag e);
 			List.iter (fun (_,vars,e) ->
 				match vars with
-				| Some l when List.exists (fun (a,_) -> a = v) l -> ()
+				| Some l when List.exists (fun (a,_) -> a = Some v) l -> ()
 				| _ -> loop flag e
 			) cases
 		| TBlock l ->
@@ -350,13 +350,17 @@ and gen_expr ctx e =
 						| None -> gen_expr ctx e2
 						| Some el ->
 							let b = block ctx [e2] in
-							let vars = List.map (fun (v,_) -> 
+							let vars = List.fold_left (fun acc (v,_) -> 								
 								incr count; 
-								let isref = add_local ctx v p in
-								let e = (EArray (ident p "@tmp",int p (!count)),p) in
-								let e = (if isref then call p (builtin p "array") [e] else e) in
-								v , Some e
-							) el in
+								match v with
+								| None ->
+									acc
+								| Some v ->
+									let isref = add_local ctx v p in
+									let e = (EArray (ident p "@tmp",int p (!count)),p) in
+									let e = (if isref then call p (builtin p "array") [e] else e) in
+									(v , Some e) :: acc
+							) [] el in
 							let e2 = gen_expr ctx e2 in
 							b();
 							(EBlock [

+ 7 - 4
genswf8.ml

@@ -657,10 +657,13 @@ and gen_match ctx retval e cases def =
 		let n = ref 0 in
 		List.iter (fun (a,t) ->
 			incr n;
-			define_var ctx a (Some (fun() ->
-				push ctx [VReg renum; VInt !n];
-				write ctx AObjGet
-			)) [e]
+			match a with
+			| None -> ()
+			| Some a ->
+				define_var ctx a (Some (fun() ->
+					push ctx [VReg renum; VInt !n];
+					write ctx AObjGet
+				)) [e]
 		) (match args with None -> [] | Some l -> l);
 		gen_expr ctx retval e;
 		if retval then ctx.stack_size <- ctx.stack_size - 1;

+ 1 - 1
type.ml

@@ -70,7 +70,7 @@ and texpr_expr =
 	| TIf of texpr * texpr * texpr option
 	| TWhile of texpr * texpr * Ast.while_flag
 	| TSwitch of texpr * (texpr * texpr) list * texpr option
-	| TMatch of texpr * (tenum * t list) * (string * (string * t) list option * texpr) list * texpr option
+	| TMatch of texpr * (tenum * t list) * (string * (string option * t) list option * texpr) list * texpr option
 	| TTry of texpr * (string * t * texpr) list
 	| TReturn of texpr option
 	| TBreak

+ 4 - 2
typer.ml

@@ -56,7 +56,7 @@ type access_kind =
 	| AccSet of texpr * string * t * string
 
 type switch_mode =
-	| CMatch of (string * (string * t) list option)
+	| CMatch of (string * (string option * t) list option)
 	| CExpr of texpr
 
 type error_msg =
@@ -808,9 +808,11 @@ let type_matching ctx (enum,params) (e,p) ecases =
 		) in
 		let idents = List.map2 (fun (e,_) t ->
 			match e with
+			| EConst (Ident "_") ->
+				None , t
 			| EConst (Ident name) | EConst (Type name) ->
 				let name = add_local ctx name t in
-				name , t
+				Some name , t
 			| _ -> invalid()
 		) el args in
 		(name,Some idents)