Browse Source

fix ternary if else generation (closes #2951)

frabbit 11 years ago
parent
commit
4d0e2c23d8
2 changed files with 46 additions and 1 deletions
  1. 1 1
      genpy.ml
  2. 45 0
      tests/unit/issues/TestIssue2951.hx

+ 1 - 1
genpy.ml

@@ -1271,7 +1271,7 @@ module Printer = struct
 			| TCast(e1,None) ->
 				print_expr pctx e1
 			| TMeta((Meta.Custom ":ternaryIf",_,_),{eexpr = TIf(econd,eif,Some eelse)}) ->
-				Printf.sprintf "%s if %s else %s" (print_expr pctx eif) (print_expr pctx econd) (print_expr pctx eelse)
+				Printf.sprintf "(%s if %s else %s)" (print_expr pctx eif) (print_expr pctx econd) (print_expr pctx eelse)
 			| TMeta(_,e1) ->
 				print_expr pctx e1
 			| TPatMatch _ | TSwitch _ | TCast(_, Some _) | TFor _ | TUnop(_,Postfix,_) ->

+ 45 - 0
tests/unit/issues/TestIssue2951.hx

@@ -0,0 +1,45 @@
+
+package unit.issues;
+
+import unit.Test;
+
+class TestIssue2951 extends Test{
+
+  public static inline function foo (x) {
+    trace(x);
+  }
+
+  public static function setCwd(x:Dynamic) {
+
+  }
+
+  public static function getLast(x:Dynamic) {
+    return true;
+  }
+
+  public static function exists(x:Dynamic) {
+    return true;
+  }
+
+  public function test ()
+  {
+  	trace("here");
+      var last = null;
+      var args = [];
+      var HxString = {
+        charCodeAt : function (a:Array<Dynamic>, i:Int) {
+          return 1;
+        }
+      };
+      if ((if ((if ((exists(last)))getLast(last) else false)){
+          if ((! (args.length < 2))){
+              var _g = HxString.charCodeAt(args[args.length - 2],0);
+              var _g1 = 45;
+              _g != _g1;
+          } else true;
+      } else false)){
+          if ((args.length == 0)) true else false;
+          setCwd(last);
+      };
+  }
+}