Parcourir la source

fixed precedence of ternary operator

Nicolas Cannasse il y a 18 ans
Parent
commit
23d8665ba9
4 fichiers modifiés avec 8 ajouts et 1 suppressions
  1. 1 0
      ast.ml
  2. 1 0
      doc/CHANGES.txt
  3. 4 1
      parser.ml
  4. 2 0
      typer.ml

+ 1 - 0
ast.ml

@@ -189,6 +189,7 @@ and expr_def =
 	| ECast of expr * type_path option
 	| EDisplay of expr
 	| EDisplayNew of type_path_normal
+	| ETernary of expr * expr * expr
 
 and expr = expr_def * pos
 

+ 1 - 0
doc/CHANGES.txt

@@ -18,6 +18,7 @@
 	added haxe.remoting.NekoSocketConnection and SocketProtocol
 	don't allow for...in iteration with Dynamic or Unknown
 	added flash9 resources
+	fixed precedence of ternary operator ?:
 
 2007-05-18: 1.13
 	fixed bug with local variable masking package in catch type

+ 4 - 1
parser.ml

@@ -91,6 +91,9 @@ let rec make_binop op e ((v,p2) as e2) =
 	| EBinop (_op,_e,_e2) when can_swap _op op && (is_not_assign _op || is_not_assign op) ->
 		let _e = make_binop op e _e in
 		EBinop (_op,_e,_e2) , punion (pos _e) (pos _e2)
+	| ETernary (e1,e2,e3) when is_not_assign op ->
+		let e = make_binop op e e1 in
+		ETernary (e,e2,e3) , punion (pos e) (pos e3)
 	| _ ->
 		EBinop (op,e,e2) , punion (pos e) (pos e2)
 
@@ -589,7 +592,7 @@ and expr_next e1 = parser
 	| [< '(Unop op,p) when is_postfix e1 op; s >] ->
 		expr_next (EUnop (op,Postfix,e1), punion (pos e1) p) s
 	| [< '(Question,_); e2 = expr; '(DblDot,_); e3 = expr >] ->
-		(EIf (e1,e2,Some e3),punion (pos e1) (pos e3))
+		(ETernary (e1,e2,e3),punion (pos e1) (pos e3))
 	| [< >] -> e1
 
 and parse_switch_cases eswitch cases = parser

+ 2 - 0
typer.ml

@@ -1782,6 +1782,8 @@ and type_expr ctx ?(need_val=true) (e,p) =
 		ctx.in_loop <- old_loop;
 		old_locals();
 		e
+	| ETernary (e1,e2,e3) ->
+		type_expr ctx ~need_val (EIf (e1,e2,Some e3),p)
 	| EIf (e,e1,e2) ->
 		let e = type_expr ctx e in
 		unify ctx e.etype (t_bool ctx) e.epos;