Просмотр исходного кода

Handle nan/inf/-inf in float to haxe expression code.

Luca Deltodesco 12 лет назад
Родитель
Сommit
d863961928
1 измененных файлов с 16 добавлено и 2 удалено
  1. 16 2
      interp.ml

+ 16 - 2
interp.ml

@@ -2037,6 +2037,20 @@ let z_lib =
 (* ---------------------------------------------------------------------- *)
 (* ---------------------------------------------------------------------- *)
 (* MACRO LIBRARY *)
 (* MACRO LIBRARY *)
 
 
+(* convert float value to haxe expression, handling inf/-inf/nan *)
+let haxe_float f p =
+    let one = (Ast.EConst (Ast.Float ("1.0")), p) in
+    let negone = (Ast.EConst (Ast.Float ("-1.0")), p) in
+    let zero = (Ast.EConst (Ast.Float ("0.0")), p) in
+    if (f = infinity) then
+        (Ast.EBinop (Ast.OpDiv, one, zero), p)
+    else if (f = neg_infinity) then
+        (Ast.EBinop (Ast.OpDiv, negone, zero), p)
+    else if (f <> f) then
+        (Ast.EBinop (Ast.OpDiv, zero, zero), p)
+    else
+        (Ast.EConst (Ast.Float (string_of_float f)), p)
+
 let macro_lib =
 let macro_lib =
 	let error() =
 	let error() =
 		raise Builtin_error
 		raise Builtin_error
@@ -2058,7 +2072,7 @@ let macro_lib =
 			| VString s, VAbstract (APos p) ->
 			| VString s, VAbstract (APos p) ->
 				raise (Typecore.Fatal_error (s,p))
 				raise (Typecore.Fatal_error (s,p))
 			| _ -> error()
 			| _ -> error()
-		);		
+		);
 		"warning", Fun2 (fun msg p ->
 		"warning", Fun2 (fun msg p ->
 			match msg, p with
 			match msg, p with
 			| VString s, VAbstract (APos p) ->
 			| VString s, VAbstract (APos p) ->
@@ -2152,7 +2166,7 @@ let macro_lib =
 					| VBool b -> (Ast.EConst (Ast.Ident (if b then "true" else "false")),p)
 					| VBool b -> (Ast.EConst (Ast.Ident (if b then "true" else "false")),p)
 					| VInt i -> (Ast.EConst (Ast.Int (string_of_int i)),p)
 					| VInt i -> (Ast.EConst (Ast.Int (string_of_int i)),p)
 					| VInt32 i -> (Ast.EConst (Ast.Int (Int32.to_string i)),p)
 					| VInt32 i -> (Ast.EConst (Ast.Int (Int32.to_string i)),p)
-					| VFloat f -> (Ast.EConst (Ast.Float (string_of_float f)),p)
+					| VFloat f -> haxe_float f p
 					| VAbstract (APos p) ->
 					| VAbstract (APos p) ->
 						(Ast.EObjectDecl (
 						(Ast.EObjectDecl (
 							("fileName" , (Ast.EConst (Ast.String p.Ast.pfile) , p)) ::
 							("fileName" , (Ast.EConst (Ast.String p.Ast.pfile) , p)) ::