浏览代码

sanitize now better match optimizer (better unit testing with --no-opt)

Nicolas Cannasse 14 年之前
父节点
当前提交
bb44192265
共有 3 个文件被更改,包括 26 次插入20 次删除
  1. 1 1
      main.ml
  2. 24 19
      optimizer.ml
  3. 1 0
      tests/unit/params.hxml

+ 1 - 1
main.ml

@@ -583,7 +583,7 @@ try
 		com.lines <- Lexer.build_line_index();
 		com.lines <- Lexer.build_line_index();
 		if com.platform = Flash9 then Common.add_filter com (fun() -> List.iter Codegen.fix_overrides com.types);
 		if com.platform = Flash9 then Common.add_filter com (fun() -> List.iter Codegen.fix_overrides com.types);
 		let filters = [
 		let filters = [
-			if com.foptimize then Optimizer.reduce_expression ctx else Optimizer.sanitize;
+			if com.foptimize then Optimizer.reduce_expression ctx else Optimizer.sanitize ctx;
 			Codegen.check_local_vars_init;
 			Codegen.check_local_vars_init;
 			Codegen.block_vars com;
 			Codegen.block_vars com;
 		] in
 		] in

+ 24 - 19
optimizer.ml

@@ -420,8 +420,29 @@ let sanitize_expr e =
 	| _ ->
 	| _ ->
 		e
 		e
 
 
-let rec sanitize e =
-	Type.map_expr sanitize (sanitize_expr e)
+let reduce_expr ctx e =
+	match e.eexpr with
+	| TSwitch (_,cases,_) ->
+		List.iter (fun (cl,_) ->
+			List.iter (fun e ->
+				match e.eexpr with
+				| TCall ({ eexpr = TEnumField _ },_) -> error "Not-constant enum in switch cannot be matched" e.epos
+				| _ -> ()
+			) cl
+		) cases;
+		e
+	| TBlock [{ eexpr = TConst _ } as ec] ->
+		{ ec with epos = e.epos }
+	| TParenthesis ec ->
+		(match ec.eexpr with
+		| TBinop _ -> e (* TODO : we could remove this after we check all operators works well *)
+		| TNew _ when ctx.com.platform = Cpp -> e (* TODO : fix in cpp generator *)
+		| _ -> { ec with epos = e.epos })
+	| _ ->
+		e
+
+let rec sanitize ctx e =
+	sanitize_expr (reduce_expr ctx (Type.map_expr (sanitize ctx) e))
 
 
 (* ---------------------------------------------------------------------- *)
 (* ---------------------------------------------------------------------- *)
 (* REDUCE *)
 (* REDUCE *)
@@ -564,24 +585,8 @@ let rec reduce_loop ctx e =
 			| Some e -> e)
 			| Some e -> e)
 		| _ ->
 		| _ ->
 			e)
 			e)
-	| TBlock [{ eexpr = TConst _ } as ec] ->
-		{ ec with epos = e.epos }
-	| TParenthesis ec ->
-		(match ec.eexpr with
-		| TBinop _ -> e (* TODO : we could remove this after we check all operators works well *)
-		| TNew _ when ctx.com.platform = Cpp -> e (* TODO : fix in cpp generator *)
-		| _ -> { ec with epos = e.epos })
-	| TSwitch (_,cases,_) ->
-		List.iter (fun (cl,_) ->
-			List.iter (fun e ->
-				match e.eexpr with
-				| TCall ({ eexpr = TEnumField _ },_) -> error "Not-constant enum in switch cannot be matched" e.epos
-				| _ -> ()
-			) cl
-		) cases;
-		e
 	| _ ->
 	| _ ->
-		e)
+		reduce_expr ctx e)
 
 
 let reduce_expression ctx e =
 let reduce_expression ctx e =
 	if ctx.com.foptimize then reduce_loop ctx e else e
 	if ctx.com.foptimize then reduce_loop ctx e else e

+ 1 - 0
tests/unit/params.hxml

@@ -2,3 +2,4 @@
 -cp ..
 -cp ..
 -resource res1.txt
 -resource res1.txt
 -resource res2.bin
 -resource res2.bin
+--no-opt