diff --git a/jscomp/core/js_dump.ml b/jscomp/core/js_dump.ml index 942d371dd2..6aff15c336 100644 --- a/jscomp/core/js_dump.ml +++ b/jscomp/core/js_dump.ml @@ -219,10 +219,10 @@ let comma_idents (cxt: cxt) f ls = comma let pp_paren_params - (inner_cxt : cxt) (f : Ext_pp.t) - (lexical : Ident.t list) : unit = + (inner_cxt : cxt) (f : Ext_pp.t) + (lexical : Ident.t list) : unit = P.string f L.lparen; - ignore @@ comma_idents inner_cxt f lexical; + let _ : cxt = comma_idents inner_cxt f lexical in P.string f L.rparen (** Print as underscore for unused vars, may not be @@ -344,7 +344,7 @@ let is_var (b : J.expression) a = let rec try_optimize_curry cxt f len function_id = Curry_gen.pp_optimize_curry f len ; - P.paren_group f 1 (fun _ -> expression 1 cxt f function_id ) + P.paren_group f 1 (fun _ -> expression ~level:1 cxt f function_id ) and pp_function is_method @@ -401,11 +401,7 @@ and pp_function is_method if the function does not capture any variable, then the context is empty *) let inner_cxt = Ext_pp_scope.sub_scope outer_cxt set_env in - - - (* (if not @@ Js_fun_env.is_empty env then *) - (* pp_comment f (Some (Js_fun_env.to_string env))) ; *) - let param_body () = + let param_body () : unit = if is_method then match l with | [] -> assert false @@ -414,7 +410,7 @@ and pp_function is_method formal_parameter_list inner_cxt f 1 arguments env ) in P.space f ; - ignore @@ P.brace_vgroup f 1 (fun _ -> + P.brace_vgroup f 1 (fun _ -> let cxt = if Js_fun_env.get_unused env 0 then cxt else pp_var_assign_this cxt f this in @@ -424,7 +420,8 @@ and pp_function is_method let cxt = P.paren_group f 1 (fun _ -> formal_parameter_list inner_cxt f 0 l env ) in P.space f ; - ignore @@ P.brace_vgroup f 1 (fun _ -> function_body cxt f b ) in + P.brace_vgroup f 1 (fun _ -> function_body cxt f b ) + in let lexical : Ident_set.t = Js_fun_env.get_lexical_scope env in let enclose lexical return = let handle lexical = @@ -440,7 +437,7 @@ and pp_function is_method P.space f ; param_body ()) | Name_non_top x -> - ignore @@ pp_var_assign inner_cxt f x ; + ignore (pp_var_assign inner_cxt f x : cxt ); P.string f L.function_; P.space f ; param_body (); @@ -448,7 +445,7 @@ and pp_function is_method | Name_top x -> P.string f L.function_; P.space f ; - ignore (Ext_pp_scope.ident inner_cxt f x); + ignore (Ext_pp_scope.ident inner_cxt f x : cxt); param_body ()) else (* print as @@ -461,7 +458,7 @@ and pp_function is_method match name with | No_name -> () | Name_non_top name | Name_top name-> - ignore @@ pp_var_assign inner_cxt f name + ignore (pp_var_assign inner_cxt f name : cxt) ) ; P.string f L.lparen; @@ -544,12 +541,12 @@ and vident cxt f (v : J.vident) = Js_dump_property.property_access f name ; cxt - -and expression l cxt f (exp : J.expression) : cxt = +(* The higher the level, the more likely that inner has to add parens *) +and expression ~level:l cxt f (exp : J.expression) : cxt = pp_comment_option f exp.comment ; - expression_desc cxt l f exp.expression_desc + expression_desc cxt ~level:l f exp.expression_desc -and expression_desc cxt (level:int) f x : cxt = +and expression_desc cxt ~(level:int) f x : cxt = match x with | Null -> P.string f L.null; cxt @@ -561,9 +558,9 @@ and expression_desc cxt (level:int) f x : cxt = bool f b ; cxt | Seq (e1, e2) -> P.cond_paren_group f (level > 0) 1 (fun () -> - let cxt = expression 0 cxt f e1 in + let cxt = expression ~level:0 cxt f e1 in comma_sp f; - expression 0 cxt f e2 ) + expression ~level:0 cxt f e2 ) | Fun (method_, l, b, env) -> (* TODO: dump for comments *) pp_function method_ cxt f false l b env (* TODO: @@ -582,7 +579,7 @@ and expression_desc cxt (level:int) f x : cxt = match info, el with | {arity = Full }, _ | _, [] -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in P.paren_group f 1 (fun _ -> arguments cxt f el ) | _ , _ -> @@ -600,27 +597,27 @@ and expression_desc cxt (level:int) f x : cxt = end)) | FlatCall(e,el) -> P.group f 1 (fun _ -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in P.string f L.dot; P.string f L.apply; P.paren_group f 1 (fun _ -> P.string f L.null; comma_sp f ; - expression 1 cxt f el + expression ~level:1 cxt f el ) ) | Char_to_int e -> (match e.expression_desc with | String_index (a,b) -> P.group f 1 (fun _ -> - let cxt = expression 15 cxt f a in + let cxt = expression ~level:15 cxt f a in P.string f L.dot; P.string f L.char_code_at; - P.paren_group f 1 (fun _ -> expression 0 cxt f b); + P.paren_group f 1 (fun _ -> expression ~level:0 cxt f b); ) | _ -> P.group f 1 (fun _ -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in P.string f L.dot; P.string f L.char_code_at; P.string f "(0)"; @@ -692,7 +689,7 @@ and expression_desc cxt (level:int) f x : cxt = cxt | Is_null_or_undefined e -> P.cond_paren_group f (level > 0) 1 (fun _ -> - let cxt = expression 1 cxt f e in + let cxt = expression ~level:1 cxt f e in P.space f ; P.string f "=="; P.space f ; @@ -701,54 +698,17 @@ and expression_desc cxt (level:int) f x : cxt = | Js_not e -> P.cond_paren_group f (level > 13) 1 (fun _ -> P.string f "!" ; - expression 13 cxt f e + expression ~level:13 cxt f e ) | Typeof e -> P.string f "typeof"; P.space f; - expression 13 cxt f e - | Bin (Eq, {expression_desc = Var i }, - {expression_desc = - ( - Bin( - (Plus as op), {expression_desc = Var j}, delta) - | Bin( - (Plus as op), delta, {expression_desc = Var j}) - | Bin( - (Minus as op), {expression_desc = Var j}, delta) - ) - }) - when Js_op_util.same_vident i j -> - (* TODO: parenthesize when necessary *) - begin match delta, op with - | {expression_desc = Number (Int { i = 1l; _})}, Plus - (* TODO: float 1. instead, - since in JS, ++ is a float operation - *) - | {expression_desc = Number (Int { i = -1l; _})}, Minus - -> - P.string f L.plusplus; - P.space f ; - vident cxt f i - - | {expression_desc = Number (Int { i = -1l; _})}, Plus - | {expression_desc = Number (Int { i = 1l; _})}, Minus - -> - P.string f L.minusminus; - P.space f ; - vident cxt f i; - | _, _ -> - let cxt = vident cxt f i in - P.space f ; - if op = Plus then P.string f "+=" - else P.string f "-="; - P.space f ; - expression 13 cxt f delta - end - | Bin (Eq, {expression_desc = Array_index({expression_desc = Var i; _}, + expression ~level:13 cxt f e + + | Bin (Eq, ({expression_desc = Array_index({expression_desc = Var i; _}, {expression_desc = Number (Int {i = k0 })} - ) }, + ) } as lhs), {expression_desc = (Bin((Plus as op), {expression_desc = Array_index( @@ -781,12 +741,6 @@ and expression_desc cxt (level:int) f x : cxt = handle parens.. *) -> - let aux cxt f vid i = - let cxt = vident cxt f vid in - P.string f "["; - P.string f (Int32.to_string i); - P.string f"]"; - cxt in (** TODO: parenthesize when necessary *) (match delta, op with | {expression_desc = Number (Int { i = 1l; _})}, Plus @@ -794,20 +748,22 @@ and expression_desc cxt (level:int) f x : cxt = -> P.string f L.plusplus; P.space f ; - aux cxt f i k0 + expression ~level:13 cxt f lhs (* Static index level is 15*) | {expression_desc = Number (Int { i = -1l; _})}, Plus | {expression_desc = Number (Int { i = 1l; _})}, Minus -> P.string f L.minusminus; P.space f ; - aux cxt f i k0 + expression ~level:13 cxt f lhs + | _, _ -> - let cxt = aux cxt f i k0 in + let cxt = expression ~level:13 cxt f lhs in P.space f ; - if op = Plus then P.string f "+=" - else P.string f "-="; + P.string f (if op = Plus then "+=" else "-="); P.space f ; - expression 13 cxt f delta) + expression ~level:13 cxt f delta) + + | Bin (Minus, {expression_desc = Number (Int {i=0l;_} | Float {f = "0."})}, e) (* TODO: Handle multiple cases like @@ -818,7 +774,7 @@ and expression_desc cxt (level:int) f x : cxt = -> P.cond_paren_group f (level > 13 ) 1 (fun _ -> P.string f "-" ; - expression 13 cxt f e + expression ~level:13 cxt f e ) | Bin (op, e1, e2) -> let (out, lft, rght) = Js_op_util.op_prec op in @@ -827,34 +783,34 @@ and expression_desc cxt (level:int) f x : cxt = (* We are more conservative here, to make the generated code more readable to the user *) P.cond_paren_group f need_paren 1 (fun _ -> - let cxt = expression lft cxt f e1 in + let cxt = expression ~level:lft cxt f e1 in P.space f; P.string f (Js_op_util.op_str op); P.space f; - expression rght cxt f e2) + expression ~level:rght cxt f e2) | String_append (e1, e2) -> let op : Js_op.binop = Plus in let (out, lft, rght) = Js_op_util.op_prec op in let need_paren = level > out || (match op with Lsl | Lsr | Asr -> true | _ -> false) in P.cond_paren_group f need_paren 1 (fun _ -> - let cxt = expression lft cxt f e1 in + let cxt = expression ~level:lft cxt f e1 in P.space f ; P.string f "+"; P.space f; - expression rght cxt f e2) + expression ~level:rght cxt f e2) | Array (el,_) -> (** TODO: simplify for singleton list *) (match el with - | []| [ _ ] -> P.bracket_group f 1 @@ fun _ -> array_element_list cxt f el - | _ -> P.bracket_vgroup f 1 @@ fun _ -> array_element_list cxt f el) + | []| [ _ ] -> P.bracket_group f 1 (fun _ -> array_element_list cxt f el) + | _ -> P.bracket_vgroup f 1 (fun _ -> array_element_list cxt f el)) | Optional_block (e,identity) -> - expression level cxt f + expression ~level cxt f (if identity then e else E.runtime_call Js_runtime_modules.option "some" [e]) | Caml_block(el,_, _, Blk_module fields) -> - expression_desc cxt (level:int) f (Object ( + expression_desc cxt ~level f (Object ( (Ext_list.map_combine fields el Ext_ident.convert))) | Caml_block( el, mutable_flag, tag, tag_info) -> @@ -870,7 +826,7 @@ and expression_desc cxt (level:int) f x : cxt = *) if not !Js_config.debug then begin if not (Js_block_runtime.needBlockRuntime tag tag_info) then - expression_desc cxt level f (Array (el, mutable_flag)) + expression_desc cxt ~level f (Array (el, mutable_flag)) else begin pp_block_create f; @@ -879,7 +835,7 @@ and expression_desc cxt (level:int) f x : cxt = end else if not (Js_block_runtime.needChromeRuntime tag tag_info) then - expression_desc cxt level f (Array (el, mutable_flag)) + expression_desc cxt ~level f (Array (el, mutable_flag)) else ( match tag_info with @@ -938,7 +894,7 @@ and expression_desc cxt (level:int) f x : cxt = | Caml_block_tag e -> P.group f 1 (fun _ -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in P.string f L.dot ; P.string f L.tag ; cxt) @@ -946,13 +902,13 @@ and expression_desc cxt (level:int) f x : cxt = | String_index (e,p) -> P.cond_paren_group f (level > 15) 1 (fun _ -> - P.group f 1 @@ fun _ -> - let cxt = expression 15 cxt f e in - P.bracket_group f 1 @@ fun _ -> - expression 0 cxt f p ) + P.group f 1 (fun _ -> + let cxt = expression ~level:15 cxt f e in + P.bracket_group f 1 (fun _ -> + expression ~level:0 cxt f p ))) | Static_index (e, s,_) -> P.cond_paren_group f (level > 15) 1 (fun _ -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in Js_dump_property.property_access f s ; (* See [ .obj_of_exports] maybe in the ast level we should have @@ -963,24 +919,23 @@ and expression_desc cxt (level:int) f x : cxt = | Length (e, _) -> (** Todo: check parens *) P.cond_paren_group f (level > 15) 1 (fun _ -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in P.string f L.dot; P.string f L.length; cxt) | New (e, el) -> P.cond_paren_group f (level > 15) 1 (fun _ -> - P.group f 1 @@ fun _ -> - P.string f L.new_; - P.space f; - let cxt = expression 16 cxt f e in - P.paren_group f 1 @@ fun _ -> - match el with - | Some el -> arguments cxt f el - | None -> cxt) + P.group f 1 ( fun _ -> + P.string f L.new_; + P.space f; + let cxt = expression ~level:16 cxt f e in + P.paren_group f 1 (fun _ -> + match el with + | Some el -> arguments cxt f el + | None -> cxt))) | Cond (e, e1, e2) -> - let action () = - (* P.group f 1 @@ fun _ -> *) - let cxt = expression 3 cxt f e in + let action () = + let cxt = expression ~level:3 cxt f e in P.space f; P.string f L.question; P.space f; @@ -988,15 +943,13 @@ and expression_desc cxt (level:int) f x : cxt = [level 1] is correct, however to make nice indentation , force nested conditional to be parenthesized *) - let cxt = P.group f 1 (fun _ -> expression 3 cxt f e1) in - (* let cxt = (P.group f 1 @@ fun _ -> expression 1 cxt f e1) in *) + let cxt = P.group f 1 (fun _ -> expression ~level:3 cxt f e1) in + P.space f; P.string f L.colon; P.space f ; - (* idem *) - P.group f 1 @@ fun _ -> expression 3 cxt f e2 - (* P.group f 1 @@ fun _ -> expression 1 cxt f e2 *) + P.group f 1 (fun _ -> expression ~level:3 cxt f e2) in if level > 2 then P.paren_vgroup f 1 action else action () @@ -1005,8 +958,8 @@ and expression_desc cxt (level:int) f x : cxt = | [] -> P.string f "{ }" ; cxt | _ -> let action () = - P.brace_vgroup f 1 @@ fun _ -> - property_name_and_value_list cxt f lst in + P.brace_vgroup f 1 (fun _ -> + property_name_and_value_list cxt f lst) in if level > 1 then (* #1946 object literal is easy to be interpreted as block statement @@ -1018,18 +971,19 @@ and expression_desc cxt (level:int) f x : cxt = P.paren_group f 1 action else action () -and property_name_and_value_list cxt f l = +and property_name_and_value_list cxt f (l : J.property_map) = iter_lst cxt f l (fun cxt f (pn,e) -> Js_dump_property.property_key f pn ; P.string f L.colon; P.space f; - expression 1 cxt f e + expression ~level:1 cxt f e ) comma_nl -and array_element_list cxt f el : cxt = - iter_lst cxt f el (fun cxt f e -> expression 1 cxt f e ) comma_nl + +and array_element_list cxt f (el : E.t list) : cxt = + iter_lst cxt f el (expression ~level:1) comma_nl -and arguments cxt f l : cxt = - iter_lst cxt f l (fun cxt f e -> expression 1 cxt f e) comma_sp +and arguments cxt f (l : E.t list) : cxt = + iter_lst cxt f l (expression ~level:1) comma_sp and variable_declaration top cxt f (variable : J.variable_declaration) : cxt = @@ -1061,7 +1015,7 @@ and variable_declaration top cxt f acxt | _, _ -> let cxt = pp_var_assign cxt f name in - let cxt = expression 1 cxt f e in + let cxt = expression ~level:1 cxt f e in semi f; cxt @@ -1108,7 +1062,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = if exp_need_paren e then P.paren_group f 1 else P.group f 0 - ) (fun _ -> expression 0 cxt f e ) in + ) (fun _ -> expression ~level:0 cxt f e ) in semi f; cxt | Block b -> (* No braces needed here *) @@ -1122,7 +1076,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = | If (e, s1, s2) -> (* TODO: always brace those statements *) P.string f L.if_; P.space f; - let cxt = P.paren_group f 1 (fun _ -> expression 0 cxt f e) in + let cxt = P.paren_group f 1 (fun _ -> expression ~level:0 cxt f e) in P.space f; let cxt = block cxt f s1 in (match s2 with @@ -1160,7 +1114,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = cxt | _ -> P.string f L.while_; - let cxt = P.paren_group f 1 (fun _ -> expression 0 cxt f e) in + let cxt = P.paren_group f 1 (fun _ -> expression ~level:0 cxt f e) in P.space f ; cxt in @@ -1170,59 +1124,59 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = end | ForRange (for_ident_expression, finish, id, direction, s, env) -> let action cxt = - P.vgroup f 0 @@ fun _ -> - let cxt = P.group f 0 @@ fun _ -> - (* The only place that [semi] may have semantics here *) - P.string f L.for_ ; - P.paren_group f 1 @@ fun _ -> - let cxt, new_id = - match for_ident_expression, finish.expression_desc with - | Some ident_expression , (Number _ | Var _ ) -> - let cxt = pp_var_assign cxt f id in - expression 0 cxt f ident_expression, None - | Some ident_expression, _ -> - let cxt = pp_var_assign cxt f id in - let cxt = expression 1 cxt f ident_expression in - P.space f ; - comma f; - let id = Ext_ident.create (Ident.name id ^ "_finish") in - let cxt = Ext_pp_scope.ident cxt f id in - P.space f ; - P.string f L.eq; - P.space f; - expression 1 cxt f finish, Some id - | None, (Number _ | Var _) -> - cxt, None - | None , _ -> - let id = Ext_ident.create (Ident.name id ^ "_finish") in - let cxt = pp_var_assign cxt f id in - expression 15 cxt f finish, Some id in - semi f ; - P.space f; - let cxt = Ext_pp_scope.ident cxt f id in - P.space f; - let right_prec = - match direction with - | Upto -> - let (_,_,right) = Js_op_util.op_prec Le in - P.string f L.le; - right - | Downto -> - let (_,_,right) = Js_op_util.op_prec Ge in - P.string f L.ge ; - right - in - P.space f ; - let cxt = - expression right_prec cxt f - (match new_id with - | Some i -> E.var i - | None -> finish) in - semi f; - P.space f; - pp_direction f direction; - Ext_pp_scope.ident cxt f id in - block cxt f s in + P.vgroup f 0 ( fun _ -> + let cxt = P.group f 0 (fun _ -> + (* The only place that [semi] may have semantics here *) + P.string f L.for_ ; + P.paren_group f 1 ( fun _ -> + let cxt, new_id = + match for_ident_expression, finish.expression_desc with + | Some ident_expression , (Number _ | Var _ ) -> + let cxt = pp_var_assign cxt f id in + expression ~level:0 cxt f ident_expression, None + | Some ident_expression, _ -> + let cxt = pp_var_assign cxt f id in + let cxt = expression ~level:1 cxt f ident_expression in + P.space f ; + comma f; + let id = Ext_ident.create (Ident.name id ^ "_finish") in + let cxt = Ext_pp_scope.ident cxt f id in + P.space f ; + P.string f L.eq; + P.space f; + expression ~level:1 cxt f finish, Some id + | None, (Number _ | Var _) -> + cxt, None + | None , _ -> + let id = Ext_ident.create (Ident.name id ^ "_finish") in + let cxt = pp_var_assign cxt f id in + expression ~level:15 cxt f finish, Some id in + semi f ; + P.space f; + let cxt = Ext_pp_scope.ident cxt f id in + P.space f; + let right_prec = + match direction with + | Upto -> + let (_,_,right) = Js_op_util.op_prec Le in + P.string f L.le; + right + | Downto -> + let (_,_,right) = Js_op_util.op_prec Ge in + P.string f L.ge ; + right + in + P.space f ; + let cxt = + expression ~level:right_prec cxt f + (match new_id with + | Some i -> E.var i + | None -> finish) in + semi f; + P.space f; + pp_direction f direction; + Ext_pp_scope.ident cxt f id)) in + block cxt f s ) in let lexical = Js_closure.get_lexical_scope env in if Ident_set.is_empty lexical then action cxt @@ -1263,7 +1217,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = return_sp f ; (* P.string f "return ";(\* ASI -- when there is a comment*\) *) P.group f return_indent (fun _ -> - let cxt = expression 0 cxt f e in + let cxt = expression ~level:0 cxt f e in semi f; cxt) (* There MUST be a space between the return and its @@ -1272,27 +1226,26 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = | Int_switch (e, cc, def) -> P.string f L.switch; P.space f; - let cxt = P.paren_group f 1 (fun _ -> expression 0 cxt f e) in + let cxt = P.paren_group f 1 (fun _ -> expression ~level:0 cxt f e) in P.space f; - P.brace_vgroup f 1 @@ fun _ -> - let cxt = loop_case_clauses cxt f (fun f i -> P.string f (string_of_int i) ) cc in - (match def with - | None -> cxt - | Some def -> - P.group f 1 @@ fun _ -> - P.string f L.default; - P.string f L.colon; - P.newline f; - statement_list false cxt f def - ) + P.brace_vgroup f 1 (fun _ -> + let cxt = loop_case_clauses cxt f (fun f i -> P.string f (string_of_int i) ) cc in + match def with + | None -> cxt + | Some def -> + P.group f 1 (fun _ -> + P.string f L.default; + P.string f L.colon; + P.newline f; + statement_list false cxt f def)) | String_switch (e, cc, def) -> P.string f L.switch; P.space f; - let cxt = P.paren_group f 1 @@ fun _ -> expression 0 cxt f e in + let cxt = P.paren_group f 1 (fun _ -> expression ~level:0 cxt f e) in P.space f; P.brace_vgroup f 1 (fun _ -> - let cxt = loop_case_clauses cxt f (fun f i -> Js_dump_string.pp_string f i ) cc in + let cxt = loop_case_clauses cxt f Js_dump_string.pp_string cc in match def with | None -> cxt | Some def -> @@ -1305,37 +1258,38 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = P.string f L.throw; P.space f ; P.group f throw_indent (fun _ -> - let cxt = expression 0 cxt f e in + let cxt = expression ~level:0 cxt f e in semi f ; cxt) (* There must be a space between the return and its argument. A line return would not work *) | Try (b, ctch, fin) -> - P.vgroup f 0 @@ fun _-> - P.string f L.try_; - P.space f ; - let cxt = block cxt f b in - let cxt = - match ctch with - | None -> - cxt - | Some (i, b) -> - P.newline f; - P.string f "catch ("; - let cxt = Ext_pp_scope.ident cxt f i in - P.string f ")"; - block cxt f b in - match fin with - | None -> cxt - | Some b -> - P.group f 1 (fun _ -> - P.string f L.finally; - P.space f; - block cxt f b) + P.vgroup f 0 ( + fun _-> + P.string f L.try_; + P.space f ; + let cxt = block cxt f b in + let cxt = + match ctch with + | None -> + cxt + | Some (i, b) -> + P.newline f; + P.string f "catch ("; + let cxt = Ext_pp_scope.ident cxt f i in + P.string f ")"; + block cxt f b in + match fin with + | None -> cxt + | Some b -> + P.group f 1 (fun _ -> + P.string f L.finally; + P.space f; + block cxt f b)) -and function_body cxt f b = +and function_body (cxt : cxt) f (b : J.block) : unit = match b with - | [] -> cxt + | [] -> () | [s] -> begin match s.statement_desc with | If (bool, @@ -1344,9 +1298,9 @@ and function_body cxt f b = statement_desc = Return {return_value = {expression_desc = Undefined}} }]) -> - statement false cxt f {s with statement_desc = If(bool,then_, [])} + ignore (statement false cxt f {s with statement_desc = If(bool,then_, [])} : cxt) | _ -> - statement false cxt f s + ignore (statement false cxt f s : cxt) end | s :: r -> let cxt = statement false cxt f s in @@ -1360,7 +1314,7 @@ and statement_list top cxt f b = (fun f -> P.newline f ; P.force_newline f ) else P.newline ) - (* (fun f -> P.newline f ; if top then P.force_newline f ) *) + and block cxt f b = (* This one is for '{' *) @@ -1369,29 +1323,19 @@ and block cxt f b = -(* let program f cxt ( x : J.program ) = - let () = P.force_newline f in - let cxt = statement_list true cxt f x.block in - let () = P.force_newline f in - Js_dump_import_export.exports cxt f x.exports *) - -(* let dump_program (x : J.program) oc = - ignore (program (P.from_channel oc) Ext_pp_scope.empty x ) *) - -let string_of_block block - = +let string_of_block (block : J.block) = let buffer = Buffer.create 50 in let f = P.from_buffer buffer in - let _scope = statement_list true Ext_pp_scope.empty f block in + let _ : cxt = statement_list true Ext_pp_scope.empty f block in P.flush f (); Buffer.contents buffer -let string_of_expression e = +let string_of_expression (e : J.expression) = let buffer = Buffer.create 50 in let f = P.from_buffer buffer in - let _scope = expression 0 Ext_pp_scope.empty f e in + let _ : cxt = expression ~level:0 Ext_pp_scope.empty f e in P.flush f (); Buffer.contents buffer diff --git a/jscomp/test/class_type_ffi_test.js b/jscomp/test/class_type_ffi_test.js index fb81f17012..64287f4ca5 100644 --- a/jscomp/test/class_type_ffi_test.js +++ b/jscomp/test/class_type_ffi_test.js @@ -5,7 +5,7 @@ var Curry = require("../../lib/js/curry.js"); function sum_float_array(arr) { var v = 0; for(var i = 0 ,i_finish = arr.length - 1 | 0; i <= i_finish; ++i){ - v += arr.case(i); + v = v + arr.case(i); } return v; } diff --git a/jscomp/test/mario_game.js b/jscomp/test/mario_game.js index 19381152c7..950c11631b 100644 --- a/jscomp/test/mario_game.js +++ b/jscomp/test/mario_game.js @@ -1084,8 +1084,8 @@ function normalize_origin(pos, spr) { var p = spr[/* params */0]; var match = p[/* bbox_offset */5]; var match$1 = p[/* bbox_size */6]; - pos[0] -= match[0]; - pos[1] -= (match[1] + match$1[1]); + pos[/* x */0] -= match[0]; + pos[/* y */1] -= (match[1] + match$1[1]); return /* () */0; } diff --git a/lib/4.06.1/unstable/js_compiler.ml b/lib/4.06.1/unstable/js_compiler.ml index b1999a62f7..54210a0ff6 100644 --- a/lib/4.06.1/unstable/js_compiler.ml +++ b/lib/4.06.1/unstable/js_compiler.ml @@ -96971,10 +96971,10 @@ let comma_idents (cxt: cxt) f ls = comma let pp_paren_params - (inner_cxt : cxt) (f : Ext_pp.t) - (lexical : Ident.t list) : unit = + (inner_cxt : cxt) (f : Ext_pp.t) + (lexical : Ident.t list) : unit = P.string f L.lparen; - ignore @@ comma_idents inner_cxt f lexical; + let _ : cxt = comma_idents inner_cxt f lexical in P.string f L.rparen (** Print as underscore for unused vars, may not be @@ -97096,7 +97096,7 @@ let is_var (b : J.expression) a = let rec try_optimize_curry cxt f len function_id = Curry_gen.pp_optimize_curry f len ; - P.paren_group f 1 (fun _ -> expression 1 cxt f function_id ) + P.paren_group f 1 (fun _ -> expression ~level:1 cxt f function_id ) and pp_function is_method @@ -97153,11 +97153,7 @@ and pp_function is_method if the function does not capture any variable, then the context is empty *) let inner_cxt = Ext_pp_scope.sub_scope outer_cxt set_env in - - - (* (if not @@ Js_fun_env.is_empty env then *) - (* pp_comment f (Some (Js_fun_env.to_string env))) ; *) - let param_body () = + let param_body () : unit = if is_method then match l with | [] -> assert false @@ -97166,7 +97162,7 @@ and pp_function is_method formal_parameter_list inner_cxt f 1 arguments env ) in P.space f ; - ignore @@ P.brace_vgroup f 1 (fun _ -> + P.brace_vgroup f 1 (fun _ -> let cxt = if Js_fun_env.get_unused env 0 then cxt else pp_var_assign_this cxt f this in @@ -97176,7 +97172,8 @@ and pp_function is_method let cxt = P.paren_group f 1 (fun _ -> formal_parameter_list inner_cxt f 0 l env ) in P.space f ; - ignore @@ P.brace_vgroup f 1 (fun _ -> function_body cxt f b ) in + P.brace_vgroup f 1 (fun _ -> function_body cxt f b ) + in let lexical : Ident_set.t = Js_fun_env.get_lexical_scope env in let enclose lexical return = let handle lexical = @@ -97192,7 +97189,7 @@ and pp_function is_method P.space f ; param_body ()) | Name_non_top x -> - ignore @@ pp_var_assign inner_cxt f x ; + ignore (pp_var_assign inner_cxt f x : cxt ); P.string f L.function_; P.space f ; param_body (); @@ -97200,7 +97197,7 @@ and pp_function is_method | Name_top x -> P.string f L.function_; P.space f ; - ignore (Ext_pp_scope.ident inner_cxt f x); + ignore (Ext_pp_scope.ident inner_cxt f x : cxt); param_body ()) else (* print as @@ -97213,7 +97210,7 @@ and pp_function is_method match name with | No_name -> () | Name_non_top name | Name_top name-> - ignore @@ pp_var_assign inner_cxt f name + ignore (pp_var_assign inner_cxt f name : cxt) ) ; P.string f L.lparen; @@ -97296,12 +97293,12 @@ and vident cxt f (v : J.vident) = Js_dump_property.property_access f name ; cxt - -and expression l cxt f (exp : J.expression) : cxt = +(* The higher the level, the more likely that inner has to add parens *) +and expression ~level:l cxt f (exp : J.expression) : cxt = pp_comment_option f exp.comment ; - expression_desc cxt l f exp.expression_desc + expression_desc cxt ~level:l f exp.expression_desc -and expression_desc cxt (level:int) f x : cxt = +and expression_desc cxt ~(level:int) f x : cxt = match x with | Null -> P.string f L.null; cxt @@ -97313,9 +97310,9 @@ and expression_desc cxt (level:int) f x : cxt = bool f b ; cxt | Seq (e1, e2) -> P.cond_paren_group f (level > 0) 1 (fun () -> - let cxt = expression 0 cxt f e1 in + let cxt = expression ~level:0 cxt f e1 in comma_sp f; - expression 0 cxt f e2 ) + expression ~level:0 cxt f e2 ) | Fun (method_, l, b, env) -> (* TODO: dump for comments *) pp_function method_ cxt f false l b env (* TODO: @@ -97334,7 +97331,7 @@ and expression_desc cxt (level:int) f x : cxt = match info, el with | {arity = Full }, _ | _, [] -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in P.paren_group f 1 (fun _ -> arguments cxt f el ) | _ , _ -> @@ -97352,27 +97349,27 @@ and expression_desc cxt (level:int) f x : cxt = end)) | FlatCall(e,el) -> P.group f 1 (fun _ -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in P.string f L.dot; P.string f L.apply; P.paren_group f 1 (fun _ -> P.string f L.null; comma_sp f ; - expression 1 cxt f el + expression ~level:1 cxt f el ) ) | Char_to_int e -> (match e.expression_desc with | String_index (a,b) -> P.group f 1 (fun _ -> - let cxt = expression 15 cxt f a in + let cxt = expression ~level:15 cxt f a in P.string f L.dot; P.string f L.char_code_at; - P.paren_group f 1 (fun _ -> expression 0 cxt f b); + P.paren_group f 1 (fun _ -> expression ~level:0 cxt f b); ) | _ -> P.group f 1 (fun _ -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in P.string f L.dot; P.string f L.char_code_at; P.string f "(0)"; @@ -97444,7 +97441,7 @@ and expression_desc cxt (level:int) f x : cxt = cxt | Is_null_or_undefined e -> P.cond_paren_group f (level > 0) 1 (fun _ -> - let cxt = expression 1 cxt f e in + let cxt = expression ~level:1 cxt f e in P.space f ; P.string f "=="; P.space f ; @@ -97453,54 +97450,17 @@ and expression_desc cxt (level:int) f x : cxt = | Js_not e -> P.cond_paren_group f (level > 13) 1 (fun _ -> P.string f "!" ; - expression 13 cxt f e + expression ~level:13 cxt f e ) | Typeof e -> P.string f "typeof"; P.space f; - expression 13 cxt f e - | Bin (Eq, {expression_desc = Var i }, - {expression_desc = - ( - Bin( - (Plus as op), {expression_desc = Var j}, delta) - | Bin( - (Plus as op), delta, {expression_desc = Var j}) - | Bin( - (Minus as op), {expression_desc = Var j}, delta) - ) - }) - when Js_op_util.same_vident i j -> - (* TODO: parenthesize when necessary *) - begin match delta, op with - | {expression_desc = Number (Int { i = 1l; _})}, Plus - (* TODO: float 1. instead, - since in JS, ++ is a float operation - *) - | {expression_desc = Number (Int { i = -1l; _})}, Minus - -> - P.string f L.plusplus; - P.space f ; - vident cxt f i - - | {expression_desc = Number (Int { i = -1l; _})}, Plus - | {expression_desc = Number (Int { i = 1l; _})}, Minus - -> - P.string f L.minusminus; - P.space f ; - vident cxt f i; - | _, _ -> - let cxt = vident cxt f i in - P.space f ; - if op = Plus then P.string f "+=" - else P.string f "-="; - P.space f ; - expression 13 cxt f delta - end - | Bin (Eq, {expression_desc = Array_index({expression_desc = Var i; _}, + expression ~level:13 cxt f e + + | Bin (Eq, ({expression_desc = Array_index({expression_desc = Var i; _}, {expression_desc = Number (Int {i = k0 })} - ) }, + ) } as lhs), {expression_desc = (Bin((Plus as op), {expression_desc = Array_index( @@ -97533,12 +97493,6 @@ and expression_desc cxt (level:int) f x : cxt = handle parens.. *) -> - let aux cxt f vid i = - let cxt = vident cxt f vid in - P.string f "["; - P.string f (Int32.to_string i); - P.string f"]"; - cxt in (** TODO: parenthesize when necessary *) (match delta, op with | {expression_desc = Number (Int { i = 1l; _})}, Plus @@ -97546,20 +97500,22 @@ and expression_desc cxt (level:int) f x : cxt = -> P.string f L.plusplus; P.space f ; - aux cxt f i k0 + expression ~level:13 cxt f lhs (* Static index level is 15*) | {expression_desc = Number (Int { i = -1l; _})}, Plus | {expression_desc = Number (Int { i = 1l; _})}, Minus -> P.string f L.minusminus; P.space f ; - aux cxt f i k0 + expression ~level:13 cxt f lhs + | _, _ -> - let cxt = aux cxt f i k0 in + let cxt = expression ~level:13 cxt f lhs in P.space f ; - if op = Plus then P.string f "+=" - else P.string f "-="; + P.string f (if op = Plus then "+=" else "-="); P.space f ; - expression 13 cxt f delta) + expression ~level:13 cxt f delta) + + | Bin (Minus, {expression_desc = Number (Int {i=0l;_} | Float {f = "0."})}, e) (* TODO: Handle multiple cases like @@ -97570,7 +97526,7 @@ and expression_desc cxt (level:int) f x : cxt = -> P.cond_paren_group f (level > 13 ) 1 (fun _ -> P.string f "-" ; - expression 13 cxt f e + expression ~level:13 cxt f e ) | Bin (op, e1, e2) -> let (out, lft, rght) = Js_op_util.op_prec op in @@ -97579,34 +97535,34 @@ and expression_desc cxt (level:int) f x : cxt = (* We are more conservative here, to make the generated code more readable to the user *) P.cond_paren_group f need_paren 1 (fun _ -> - let cxt = expression lft cxt f e1 in + let cxt = expression ~level:lft cxt f e1 in P.space f; P.string f (Js_op_util.op_str op); P.space f; - expression rght cxt f e2) + expression ~level:rght cxt f e2) | String_append (e1, e2) -> let op : Js_op.binop = Plus in let (out, lft, rght) = Js_op_util.op_prec op in let need_paren = level > out || (match op with Lsl | Lsr | Asr -> true | _ -> false) in P.cond_paren_group f need_paren 1 (fun _ -> - let cxt = expression lft cxt f e1 in + let cxt = expression ~level:lft cxt f e1 in P.space f ; P.string f "+"; P.space f; - expression rght cxt f e2) + expression ~level:rght cxt f e2) | Array (el,_) -> (** TODO: simplify for singleton list *) (match el with - | []| [ _ ] -> P.bracket_group f 1 @@ fun _ -> array_element_list cxt f el - | _ -> P.bracket_vgroup f 1 @@ fun _ -> array_element_list cxt f el) + | []| [ _ ] -> P.bracket_group f 1 (fun _ -> array_element_list cxt f el) + | _ -> P.bracket_vgroup f 1 (fun _ -> array_element_list cxt f el)) | Optional_block (e,identity) -> - expression level cxt f + expression ~level cxt f (if identity then e else E.runtime_call Js_runtime_modules.option "some" [e]) | Caml_block(el,_, _, Blk_module fields) -> - expression_desc cxt (level:int) f (Object ( + expression_desc cxt ~level f (Object ( (Ext_list.map_combine fields el Ext_ident.convert))) | Caml_block( el, mutable_flag, tag, tag_info) -> @@ -97622,7 +97578,7 @@ and expression_desc cxt (level:int) f x : cxt = *) if not !Js_config.debug then begin if not (Js_block_runtime.needBlockRuntime tag tag_info) then - expression_desc cxt level f (Array (el, mutable_flag)) + expression_desc cxt ~level f (Array (el, mutable_flag)) else begin pp_block_create f; @@ -97631,7 +97587,7 @@ and expression_desc cxt (level:int) f x : cxt = end else if not (Js_block_runtime.needChromeRuntime tag tag_info) then - expression_desc cxt level f (Array (el, mutable_flag)) + expression_desc cxt ~level f (Array (el, mutable_flag)) else ( match tag_info with @@ -97690,7 +97646,7 @@ and expression_desc cxt (level:int) f x : cxt = | Caml_block_tag e -> P.group f 1 (fun _ -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in P.string f L.dot ; P.string f L.tag ; cxt) @@ -97698,13 +97654,13 @@ and expression_desc cxt (level:int) f x : cxt = | String_index (e,p) -> P.cond_paren_group f (level > 15) 1 (fun _ -> - P.group f 1 @@ fun _ -> - let cxt = expression 15 cxt f e in - P.bracket_group f 1 @@ fun _ -> - expression 0 cxt f p ) + P.group f 1 (fun _ -> + let cxt = expression ~level:15 cxt f e in + P.bracket_group f 1 (fun _ -> + expression ~level:0 cxt f p ))) | Static_index (e, s,_) -> P.cond_paren_group f (level > 15) 1 (fun _ -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in Js_dump_property.property_access f s ; (* See [ .obj_of_exports] maybe in the ast level we should have @@ -97715,24 +97671,23 @@ and expression_desc cxt (level:int) f x : cxt = | Length (e, _) -> (** Todo: check parens *) P.cond_paren_group f (level > 15) 1 (fun _ -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in P.string f L.dot; P.string f L.length; cxt) | New (e, el) -> P.cond_paren_group f (level > 15) 1 (fun _ -> - P.group f 1 @@ fun _ -> - P.string f L.new_; - P.space f; - let cxt = expression 16 cxt f e in - P.paren_group f 1 @@ fun _ -> - match el with - | Some el -> arguments cxt f el - | None -> cxt) + P.group f 1 ( fun _ -> + P.string f L.new_; + P.space f; + let cxt = expression ~level:16 cxt f e in + P.paren_group f 1 (fun _ -> + match el with + | Some el -> arguments cxt f el + | None -> cxt))) | Cond (e, e1, e2) -> - let action () = - (* P.group f 1 @@ fun _ -> *) - let cxt = expression 3 cxt f e in + let action () = + let cxt = expression ~level:3 cxt f e in P.space f; P.string f L.question; P.space f; @@ -97740,15 +97695,13 @@ and expression_desc cxt (level:int) f x : cxt = [level 1] is correct, however to make nice indentation , force nested conditional to be parenthesized *) - let cxt = P.group f 1 (fun _ -> expression 3 cxt f e1) in - (* let cxt = (P.group f 1 @@ fun _ -> expression 1 cxt f e1) in *) + let cxt = P.group f 1 (fun _ -> expression ~level:3 cxt f e1) in + P.space f; P.string f L.colon; P.space f ; - (* idem *) - P.group f 1 @@ fun _ -> expression 3 cxt f e2 - (* P.group f 1 @@ fun _ -> expression 1 cxt f e2 *) + P.group f 1 (fun _ -> expression ~level:3 cxt f e2) in if level > 2 then P.paren_vgroup f 1 action else action () @@ -97757,8 +97710,8 @@ and expression_desc cxt (level:int) f x : cxt = | [] -> P.string f "{ }" ; cxt | _ -> let action () = - P.brace_vgroup f 1 @@ fun _ -> - property_name_and_value_list cxt f lst in + P.brace_vgroup f 1 (fun _ -> + property_name_and_value_list cxt f lst) in if level > 1 then (* #1946 object literal is easy to be interpreted as block statement @@ -97770,18 +97723,19 @@ and expression_desc cxt (level:int) f x : cxt = P.paren_group f 1 action else action () -and property_name_and_value_list cxt f l = +and property_name_and_value_list cxt f (l : J.property_map) = iter_lst cxt f l (fun cxt f (pn,e) -> Js_dump_property.property_key f pn ; P.string f L.colon; P.space f; - expression 1 cxt f e + expression ~level:1 cxt f e ) comma_nl -and array_element_list cxt f el : cxt = - iter_lst cxt f el (fun cxt f e -> expression 1 cxt f e ) comma_nl + +and array_element_list cxt f (el : E.t list) : cxt = + iter_lst cxt f el (expression ~level:1) comma_nl -and arguments cxt f l : cxt = - iter_lst cxt f l (fun cxt f e -> expression 1 cxt f e) comma_sp +and arguments cxt f (l : E.t list) : cxt = + iter_lst cxt f l (expression ~level:1) comma_sp and variable_declaration top cxt f (variable : J.variable_declaration) : cxt = @@ -97813,7 +97767,7 @@ and variable_declaration top cxt f acxt | _, _ -> let cxt = pp_var_assign cxt f name in - let cxt = expression 1 cxt f e in + let cxt = expression ~level:1 cxt f e in semi f; cxt @@ -97860,7 +97814,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = if exp_need_paren e then P.paren_group f 1 else P.group f 0 - ) (fun _ -> expression 0 cxt f e ) in + ) (fun _ -> expression ~level:0 cxt f e ) in semi f; cxt | Block b -> (* No braces needed here *) @@ -97874,7 +97828,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = | If (e, s1, s2) -> (* TODO: always brace those statements *) P.string f L.if_; P.space f; - let cxt = P.paren_group f 1 (fun _ -> expression 0 cxt f e) in + let cxt = P.paren_group f 1 (fun _ -> expression ~level:0 cxt f e) in P.space f; let cxt = block cxt f s1 in (match s2 with @@ -97912,7 +97866,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = cxt | _ -> P.string f L.while_; - let cxt = P.paren_group f 1 (fun _ -> expression 0 cxt f e) in + let cxt = P.paren_group f 1 (fun _ -> expression ~level:0 cxt f e) in P.space f ; cxt in @@ -97922,59 +97876,59 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = end | ForRange (for_ident_expression, finish, id, direction, s, env) -> let action cxt = - P.vgroup f 0 @@ fun _ -> - let cxt = P.group f 0 @@ fun _ -> - (* The only place that [semi] may have semantics here *) - P.string f L.for_ ; - P.paren_group f 1 @@ fun _ -> - let cxt, new_id = - match for_ident_expression, finish.expression_desc with - | Some ident_expression , (Number _ | Var _ ) -> - let cxt = pp_var_assign cxt f id in - expression 0 cxt f ident_expression, None - | Some ident_expression, _ -> - let cxt = pp_var_assign cxt f id in - let cxt = expression 1 cxt f ident_expression in - P.space f ; - comma f; - let id = Ext_ident.create (Ident.name id ^ "_finish") in - let cxt = Ext_pp_scope.ident cxt f id in - P.space f ; - P.string f L.eq; - P.space f; - expression 1 cxt f finish, Some id - | None, (Number _ | Var _) -> - cxt, None - | None , _ -> - let id = Ext_ident.create (Ident.name id ^ "_finish") in - let cxt = pp_var_assign cxt f id in - expression 15 cxt f finish, Some id in - semi f ; - P.space f; - let cxt = Ext_pp_scope.ident cxt f id in - P.space f; - let right_prec = - match direction with - | Upto -> - let (_,_,right) = Js_op_util.op_prec Le in - P.string f L.le; - right - | Downto -> - let (_,_,right) = Js_op_util.op_prec Ge in - P.string f L.ge ; - right - in - P.space f ; - let cxt = - expression right_prec cxt f - (match new_id with - | Some i -> E.var i - | None -> finish) in - semi f; - P.space f; - pp_direction f direction; - Ext_pp_scope.ident cxt f id in - block cxt f s in + P.vgroup f 0 ( fun _ -> + let cxt = P.group f 0 (fun _ -> + (* The only place that [semi] may have semantics here *) + P.string f L.for_ ; + P.paren_group f 1 ( fun _ -> + let cxt, new_id = + match for_ident_expression, finish.expression_desc with + | Some ident_expression , (Number _ | Var _ ) -> + let cxt = pp_var_assign cxt f id in + expression ~level:0 cxt f ident_expression, None + | Some ident_expression, _ -> + let cxt = pp_var_assign cxt f id in + let cxt = expression ~level:1 cxt f ident_expression in + P.space f ; + comma f; + let id = Ext_ident.create (Ident.name id ^ "_finish") in + let cxt = Ext_pp_scope.ident cxt f id in + P.space f ; + P.string f L.eq; + P.space f; + expression ~level:1 cxt f finish, Some id + | None, (Number _ | Var _) -> + cxt, None + | None , _ -> + let id = Ext_ident.create (Ident.name id ^ "_finish") in + let cxt = pp_var_assign cxt f id in + expression ~level:15 cxt f finish, Some id in + semi f ; + P.space f; + let cxt = Ext_pp_scope.ident cxt f id in + P.space f; + let right_prec = + match direction with + | Upto -> + let (_,_,right) = Js_op_util.op_prec Le in + P.string f L.le; + right + | Downto -> + let (_,_,right) = Js_op_util.op_prec Ge in + P.string f L.ge ; + right + in + P.space f ; + let cxt = + expression ~level:right_prec cxt f + (match new_id with + | Some i -> E.var i + | None -> finish) in + semi f; + P.space f; + pp_direction f direction; + Ext_pp_scope.ident cxt f id)) in + block cxt f s ) in let lexical = Js_closure.get_lexical_scope env in if Ident_set.is_empty lexical then action cxt @@ -98015,7 +97969,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = return_sp f ; (* P.string f "return ";(\* ASI -- when there is a comment*\) *) P.group f return_indent (fun _ -> - let cxt = expression 0 cxt f e in + let cxt = expression ~level:0 cxt f e in semi f; cxt) (* There MUST be a space between the return and its @@ -98024,27 +97978,26 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = | Int_switch (e, cc, def) -> P.string f L.switch; P.space f; - let cxt = P.paren_group f 1 (fun _ -> expression 0 cxt f e) in + let cxt = P.paren_group f 1 (fun _ -> expression ~level:0 cxt f e) in P.space f; - P.brace_vgroup f 1 @@ fun _ -> - let cxt = loop_case_clauses cxt f (fun f i -> P.string f (string_of_int i) ) cc in - (match def with - | None -> cxt - | Some def -> - P.group f 1 @@ fun _ -> - P.string f L.default; - P.string f L.colon; - P.newline f; - statement_list false cxt f def - ) + P.brace_vgroup f 1 (fun _ -> + let cxt = loop_case_clauses cxt f (fun f i -> P.string f (string_of_int i) ) cc in + match def with + | None -> cxt + | Some def -> + P.group f 1 (fun _ -> + P.string f L.default; + P.string f L.colon; + P.newline f; + statement_list false cxt f def)) | String_switch (e, cc, def) -> P.string f L.switch; P.space f; - let cxt = P.paren_group f 1 @@ fun _ -> expression 0 cxt f e in + let cxt = P.paren_group f 1 (fun _ -> expression ~level:0 cxt f e) in P.space f; P.brace_vgroup f 1 (fun _ -> - let cxt = loop_case_clauses cxt f (fun f i -> Js_dump_string.pp_string f i ) cc in + let cxt = loop_case_clauses cxt f Js_dump_string.pp_string cc in match def with | None -> cxt | Some def -> @@ -98057,37 +98010,38 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = P.string f L.throw; P.space f ; P.group f throw_indent (fun _ -> - let cxt = expression 0 cxt f e in + let cxt = expression ~level:0 cxt f e in semi f ; cxt) (* There must be a space between the return and its argument. A line return would not work *) | Try (b, ctch, fin) -> - P.vgroup f 0 @@ fun _-> - P.string f L.try_; - P.space f ; - let cxt = block cxt f b in - let cxt = - match ctch with - | None -> - cxt - | Some (i, b) -> - P.newline f; - P.string f "catch ("; - let cxt = Ext_pp_scope.ident cxt f i in - P.string f ")"; - block cxt f b in - match fin with - | None -> cxt - | Some b -> - P.group f 1 (fun _ -> - P.string f L.finally; - P.space f; - block cxt f b) + P.vgroup f 0 ( + fun _-> + P.string f L.try_; + P.space f ; + let cxt = block cxt f b in + let cxt = + match ctch with + | None -> + cxt + | Some (i, b) -> + P.newline f; + P.string f "catch ("; + let cxt = Ext_pp_scope.ident cxt f i in + P.string f ")"; + block cxt f b in + match fin with + | None -> cxt + | Some b -> + P.group f 1 (fun _ -> + P.string f L.finally; + P.space f; + block cxt f b)) -and function_body cxt f b = +and function_body (cxt : cxt) f (b : J.block) : unit = match b with - | [] -> cxt + | [] -> () | [s] -> begin match s.statement_desc with | If (bool, @@ -98096,9 +98050,9 @@ and function_body cxt f b = statement_desc = Return {return_value = {expression_desc = Undefined}} }]) -> - statement false cxt f {s with statement_desc = If(bool,then_, [])} + ignore (statement false cxt f {s with statement_desc = If(bool,then_, [])} : cxt) | _ -> - statement false cxt f s + ignore (statement false cxt f s : cxt) end | s :: r -> let cxt = statement false cxt f s in @@ -98112,7 +98066,7 @@ and statement_list top cxt f b = (fun f -> P.newline f ; P.force_newline f ) else P.newline ) - (* (fun f -> P.newline f ; if top then P.force_newline f ) *) + and block cxt f b = (* This one is for '{' *) @@ -98121,29 +98075,19 @@ and block cxt f b = -(* let program f cxt ( x : J.program ) = - let () = P.force_newline f in - let cxt = statement_list true cxt f x.block in - let () = P.force_newline f in - Js_dump_import_export.exports cxt f x.exports *) - -(* let dump_program (x : J.program) oc = - ignore (program (P.from_channel oc) Ext_pp_scope.empty x ) *) - -let string_of_block block - = +let string_of_block (block : J.block) = let buffer = Buffer.create 50 in let f = P.from_buffer buffer in - let _scope = statement_list true Ext_pp_scope.empty f block in + let _ : cxt = statement_list true Ext_pp_scope.empty f block in P.flush f (); Buffer.contents buffer -let string_of_expression e = +let string_of_expression (e : J.expression) = let buffer = Buffer.create 50 in let f = P.from_buffer buffer in - let _scope = expression 0 Ext_pp_scope.empty f e in + let _ : cxt = expression ~level:0 Ext_pp_scope.empty f e in P.flush f (); Buffer.contents buffer diff --git a/lib/4.06.1/whole_compiler.ml b/lib/4.06.1/whole_compiler.ml index 5f95eb3392..58062a2ecd 100644 --- a/lib/4.06.1/whole_compiler.ml +++ b/lib/4.06.1/whole_compiler.ml @@ -99700,10 +99700,10 @@ let comma_idents (cxt: cxt) f ls = comma let pp_paren_params - (inner_cxt : cxt) (f : Ext_pp.t) - (lexical : Ident.t list) : unit = + (inner_cxt : cxt) (f : Ext_pp.t) + (lexical : Ident.t list) : unit = P.string f L.lparen; - ignore @@ comma_idents inner_cxt f lexical; + let _ : cxt = comma_idents inner_cxt f lexical in P.string f L.rparen (** Print as underscore for unused vars, may not be @@ -99825,7 +99825,7 @@ let is_var (b : J.expression) a = let rec try_optimize_curry cxt f len function_id = Curry_gen.pp_optimize_curry f len ; - P.paren_group f 1 (fun _ -> expression 1 cxt f function_id ) + P.paren_group f 1 (fun _ -> expression ~level:1 cxt f function_id ) and pp_function is_method @@ -99882,11 +99882,7 @@ and pp_function is_method if the function does not capture any variable, then the context is empty *) let inner_cxt = Ext_pp_scope.sub_scope outer_cxt set_env in - - - (* (if not @@ Js_fun_env.is_empty env then *) - (* pp_comment f (Some (Js_fun_env.to_string env))) ; *) - let param_body () = + let param_body () : unit = if is_method then match l with | [] -> assert false @@ -99895,7 +99891,7 @@ and pp_function is_method formal_parameter_list inner_cxt f 1 arguments env ) in P.space f ; - ignore @@ P.brace_vgroup f 1 (fun _ -> + P.brace_vgroup f 1 (fun _ -> let cxt = if Js_fun_env.get_unused env 0 then cxt else pp_var_assign_this cxt f this in @@ -99905,7 +99901,8 @@ and pp_function is_method let cxt = P.paren_group f 1 (fun _ -> formal_parameter_list inner_cxt f 0 l env ) in P.space f ; - ignore @@ P.brace_vgroup f 1 (fun _ -> function_body cxt f b ) in + P.brace_vgroup f 1 (fun _ -> function_body cxt f b ) + in let lexical : Ident_set.t = Js_fun_env.get_lexical_scope env in let enclose lexical return = let handle lexical = @@ -99921,7 +99918,7 @@ and pp_function is_method P.space f ; param_body ()) | Name_non_top x -> - ignore @@ pp_var_assign inner_cxt f x ; + ignore (pp_var_assign inner_cxt f x : cxt ); P.string f L.function_; P.space f ; param_body (); @@ -99929,7 +99926,7 @@ and pp_function is_method | Name_top x -> P.string f L.function_; P.space f ; - ignore (Ext_pp_scope.ident inner_cxt f x); + ignore (Ext_pp_scope.ident inner_cxt f x : cxt); param_body ()) else (* print as @@ -99942,7 +99939,7 @@ and pp_function is_method match name with | No_name -> () | Name_non_top name | Name_top name-> - ignore @@ pp_var_assign inner_cxt f name + ignore (pp_var_assign inner_cxt f name : cxt) ) ; P.string f L.lparen; @@ -100025,12 +100022,12 @@ and vident cxt f (v : J.vident) = Js_dump_property.property_access f name ; cxt - -and expression l cxt f (exp : J.expression) : cxt = +(* The higher the level, the more likely that inner has to add parens *) +and expression ~level:l cxt f (exp : J.expression) : cxt = pp_comment_option f exp.comment ; - expression_desc cxt l f exp.expression_desc + expression_desc cxt ~level:l f exp.expression_desc -and expression_desc cxt (level:int) f x : cxt = +and expression_desc cxt ~(level:int) f x : cxt = match x with | Null -> P.string f L.null; cxt @@ -100042,9 +100039,9 @@ and expression_desc cxt (level:int) f x : cxt = bool f b ; cxt | Seq (e1, e2) -> P.cond_paren_group f (level > 0) 1 (fun () -> - let cxt = expression 0 cxt f e1 in + let cxt = expression ~level:0 cxt f e1 in comma_sp f; - expression 0 cxt f e2 ) + expression ~level:0 cxt f e2 ) | Fun (method_, l, b, env) -> (* TODO: dump for comments *) pp_function method_ cxt f false l b env (* TODO: @@ -100063,7 +100060,7 @@ and expression_desc cxt (level:int) f x : cxt = match info, el with | {arity = Full }, _ | _, [] -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in P.paren_group f 1 (fun _ -> arguments cxt f el ) | _ , _ -> @@ -100081,27 +100078,27 @@ and expression_desc cxt (level:int) f x : cxt = end)) | FlatCall(e,el) -> P.group f 1 (fun _ -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in P.string f L.dot; P.string f L.apply; P.paren_group f 1 (fun _ -> P.string f L.null; comma_sp f ; - expression 1 cxt f el + expression ~level:1 cxt f el ) ) | Char_to_int e -> (match e.expression_desc with | String_index (a,b) -> P.group f 1 (fun _ -> - let cxt = expression 15 cxt f a in + let cxt = expression ~level:15 cxt f a in P.string f L.dot; P.string f L.char_code_at; - P.paren_group f 1 (fun _ -> expression 0 cxt f b); + P.paren_group f 1 (fun _ -> expression ~level:0 cxt f b); ) | _ -> P.group f 1 (fun _ -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in P.string f L.dot; P.string f L.char_code_at; P.string f "(0)"; @@ -100173,7 +100170,7 @@ and expression_desc cxt (level:int) f x : cxt = cxt | Is_null_or_undefined e -> P.cond_paren_group f (level > 0) 1 (fun _ -> - let cxt = expression 1 cxt f e in + let cxt = expression ~level:1 cxt f e in P.space f ; P.string f "=="; P.space f ; @@ -100182,54 +100179,17 @@ and expression_desc cxt (level:int) f x : cxt = | Js_not e -> P.cond_paren_group f (level > 13) 1 (fun _ -> P.string f "!" ; - expression 13 cxt f e + expression ~level:13 cxt f e ) | Typeof e -> P.string f "typeof"; P.space f; - expression 13 cxt f e - | Bin (Eq, {expression_desc = Var i }, - {expression_desc = - ( - Bin( - (Plus as op), {expression_desc = Var j}, delta) - | Bin( - (Plus as op), delta, {expression_desc = Var j}) - | Bin( - (Minus as op), {expression_desc = Var j}, delta) - ) - }) - when Js_op_util.same_vident i j -> - (* TODO: parenthesize when necessary *) - begin match delta, op with - | {expression_desc = Number (Int { i = 1l; _})}, Plus - (* TODO: float 1. instead, - since in JS, ++ is a float operation - *) - | {expression_desc = Number (Int { i = -1l; _})}, Minus - -> - P.string f L.plusplus; - P.space f ; - vident cxt f i - - | {expression_desc = Number (Int { i = -1l; _})}, Plus - | {expression_desc = Number (Int { i = 1l; _})}, Minus - -> - P.string f L.minusminus; - P.space f ; - vident cxt f i; - | _, _ -> - let cxt = vident cxt f i in - P.space f ; - if op = Plus then P.string f "+=" - else P.string f "-="; - P.space f ; - expression 13 cxt f delta - end - | Bin (Eq, {expression_desc = Array_index({expression_desc = Var i; _}, + expression ~level:13 cxt f e + + | Bin (Eq, ({expression_desc = Array_index({expression_desc = Var i; _}, {expression_desc = Number (Int {i = k0 })} - ) }, + ) } as lhs), {expression_desc = (Bin((Plus as op), {expression_desc = Array_index( @@ -100262,12 +100222,6 @@ and expression_desc cxt (level:int) f x : cxt = handle parens.. *) -> - let aux cxt f vid i = - let cxt = vident cxt f vid in - P.string f "["; - P.string f (Int32.to_string i); - P.string f"]"; - cxt in (** TODO: parenthesize when necessary *) (match delta, op with | {expression_desc = Number (Int { i = 1l; _})}, Plus @@ -100275,20 +100229,22 @@ and expression_desc cxt (level:int) f x : cxt = -> P.string f L.plusplus; P.space f ; - aux cxt f i k0 + expression ~level:13 cxt f lhs (* Static index level is 15*) | {expression_desc = Number (Int { i = -1l; _})}, Plus | {expression_desc = Number (Int { i = 1l; _})}, Minus -> P.string f L.minusminus; P.space f ; - aux cxt f i k0 + expression ~level:13 cxt f lhs + | _, _ -> - let cxt = aux cxt f i k0 in + let cxt = expression ~level:13 cxt f lhs in P.space f ; - if op = Plus then P.string f "+=" - else P.string f "-="; + P.string f (if op = Plus then "+=" else "-="); P.space f ; - expression 13 cxt f delta) + expression ~level:13 cxt f delta) + + | Bin (Minus, {expression_desc = Number (Int {i=0l;_} | Float {f = "0."})}, e) (* TODO: Handle multiple cases like @@ -100299,7 +100255,7 @@ and expression_desc cxt (level:int) f x : cxt = -> P.cond_paren_group f (level > 13 ) 1 (fun _ -> P.string f "-" ; - expression 13 cxt f e + expression ~level:13 cxt f e ) | Bin (op, e1, e2) -> let (out, lft, rght) = Js_op_util.op_prec op in @@ -100308,34 +100264,34 @@ and expression_desc cxt (level:int) f x : cxt = (* We are more conservative here, to make the generated code more readable to the user *) P.cond_paren_group f need_paren 1 (fun _ -> - let cxt = expression lft cxt f e1 in + let cxt = expression ~level:lft cxt f e1 in P.space f; P.string f (Js_op_util.op_str op); P.space f; - expression rght cxt f e2) + expression ~level:rght cxt f e2) | String_append (e1, e2) -> let op : Js_op.binop = Plus in let (out, lft, rght) = Js_op_util.op_prec op in let need_paren = level > out || (match op with Lsl | Lsr | Asr -> true | _ -> false) in P.cond_paren_group f need_paren 1 (fun _ -> - let cxt = expression lft cxt f e1 in + let cxt = expression ~level:lft cxt f e1 in P.space f ; P.string f "+"; P.space f; - expression rght cxt f e2) + expression ~level:rght cxt f e2) | Array (el,_) -> (** TODO: simplify for singleton list *) (match el with - | []| [ _ ] -> P.bracket_group f 1 @@ fun _ -> array_element_list cxt f el - | _ -> P.bracket_vgroup f 1 @@ fun _ -> array_element_list cxt f el) + | []| [ _ ] -> P.bracket_group f 1 (fun _ -> array_element_list cxt f el) + | _ -> P.bracket_vgroup f 1 (fun _ -> array_element_list cxt f el)) | Optional_block (e,identity) -> - expression level cxt f + expression ~level cxt f (if identity then e else E.runtime_call Js_runtime_modules.option "some" [e]) | Caml_block(el,_, _, Blk_module fields) -> - expression_desc cxt (level:int) f (Object ( + expression_desc cxt ~level f (Object ( (Ext_list.map_combine fields el Ext_ident.convert))) | Caml_block( el, mutable_flag, tag, tag_info) -> @@ -100351,7 +100307,7 @@ and expression_desc cxt (level:int) f x : cxt = *) if not !Js_config.debug then begin if not (Js_block_runtime.needBlockRuntime tag tag_info) then - expression_desc cxt level f (Array (el, mutable_flag)) + expression_desc cxt ~level f (Array (el, mutable_flag)) else begin pp_block_create f; @@ -100360,7 +100316,7 @@ and expression_desc cxt (level:int) f x : cxt = end else if not (Js_block_runtime.needChromeRuntime tag tag_info) then - expression_desc cxt level f (Array (el, mutable_flag)) + expression_desc cxt ~level f (Array (el, mutable_flag)) else ( match tag_info with @@ -100419,7 +100375,7 @@ and expression_desc cxt (level:int) f x : cxt = | Caml_block_tag e -> P.group f 1 (fun _ -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in P.string f L.dot ; P.string f L.tag ; cxt) @@ -100427,13 +100383,13 @@ and expression_desc cxt (level:int) f x : cxt = | String_index (e,p) -> P.cond_paren_group f (level > 15) 1 (fun _ -> - P.group f 1 @@ fun _ -> - let cxt = expression 15 cxt f e in - P.bracket_group f 1 @@ fun _ -> - expression 0 cxt f p ) + P.group f 1 (fun _ -> + let cxt = expression ~level:15 cxt f e in + P.bracket_group f 1 (fun _ -> + expression ~level:0 cxt f p ))) | Static_index (e, s,_) -> P.cond_paren_group f (level > 15) 1 (fun _ -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in Js_dump_property.property_access f s ; (* See [ .obj_of_exports] maybe in the ast level we should have @@ -100444,24 +100400,23 @@ and expression_desc cxt (level:int) f x : cxt = | Length (e, _) -> (** Todo: check parens *) P.cond_paren_group f (level > 15) 1 (fun _ -> - let cxt = expression 15 cxt f e in + let cxt = expression ~level:15 cxt f e in P.string f L.dot; P.string f L.length; cxt) | New (e, el) -> P.cond_paren_group f (level > 15) 1 (fun _ -> - P.group f 1 @@ fun _ -> - P.string f L.new_; - P.space f; - let cxt = expression 16 cxt f e in - P.paren_group f 1 @@ fun _ -> - match el with - | Some el -> arguments cxt f el - | None -> cxt) + P.group f 1 ( fun _ -> + P.string f L.new_; + P.space f; + let cxt = expression ~level:16 cxt f e in + P.paren_group f 1 (fun _ -> + match el with + | Some el -> arguments cxt f el + | None -> cxt))) | Cond (e, e1, e2) -> - let action () = - (* P.group f 1 @@ fun _ -> *) - let cxt = expression 3 cxt f e in + let action () = + let cxt = expression ~level:3 cxt f e in P.space f; P.string f L.question; P.space f; @@ -100469,15 +100424,13 @@ and expression_desc cxt (level:int) f x : cxt = [level 1] is correct, however to make nice indentation , force nested conditional to be parenthesized *) - let cxt = P.group f 1 (fun _ -> expression 3 cxt f e1) in - (* let cxt = (P.group f 1 @@ fun _ -> expression 1 cxt f e1) in *) + let cxt = P.group f 1 (fun _ -> expression ~level:3 cxt f e1) in + P.space f; P.string f L.colon; P.space f ; - (* idem *) - P.group f 1 @@ fun _ -> expression 3 cxt f e2 - (* P.group f 1 @@ fun _ -> expression 1 cxt f e2 *) + P.group f 1 (fun _ -> expression ~level:3 cxt f e2) in if level > 2 then P.paren_vgroup f 1 action else action () @@ -100486,8 +100439,8 @@ and expression_desc cxt (level:int) f x : cxt = | [] -> P.string f "{ }" ; cxt | _ -> let action () = - P.brace_vgroup f 1 @@ fun _ -> - property_name_and_value_list cxt f lst in + P.brace_vgroup f 1 (fun _ -> + property_name_and_value_list cxt f lst) in if level > 1 then (* #1946 object literal is easy to be interpreted as block statement @@ -100499,18 +100452,19 @@ and expression_desc cxt (level:int) f x : cxt = P.paren_group f 1 action else action () -and property_name_and_value_list cxt f l = +and property_name_and_value_list cxt f (l : J.property_map) = iter_lst cxt f l (fun cxt f (pn,e) -> Js_dump_property.property_key f pn ; P.string f L.colon; P.space f; - expression 1 cxt f e + expression ~level:1 cxt f e ) comma_nl -and array_element_list cxt f el : cxt = - iter_lst cxt f el (fun cxt f e -> expression 1 cxt f e ) comma_nl + +and array_element_list cxt f (el : E.t list) : cxt = + iter_lst cxt f el (expression ~level:1) comma_nl -and arguments cxt f l : cxt = - iter_lst cxt f l (fun cxt f e -> expression 1 cxt f e) comma_sp +and arguments cxt f (l : E.t list) : cxt = + iter_lst cxt f l (expression ~level:1) comma_sp and variable_declaration top cxt f (variable : J.variable_declaration) : cxt = @@ -100542,7 +100496,7 @@ and variable_declaration top cxt f acxt | _, _ -> let cxt = pp_var_assign cxt f name in - let cxt = expression 1 cxt f e in + let cxt = expression ~level:1 cxt f e in semi f; cxt @@ -100589,7 +100543,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = if exp_need_paren e then P.paren_group f 1 else P.group f 0 - ) (fun _ -> expression 0 cxt f e ) in + ) (fun _ -> expression ~level:0 cxt f e ) in semi f; cxt | Block b -> (* No braces needed here *) @@ -100603,7 +100557,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = | If (e, s1, s2) -> (* TODO: always brace those statements *) P.string f L.if_; P.space f; - let cxt = P.paren_group f 1 (fun _ -> expression 0 cxt f e) in + let cxt = P.paren_group f 1 (fun _ -> expression ~level:0 cxt f e) in P.space f; let cxt = block cxt f s1 in (match s2 with @@ -100641,7 +100595,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = cxt | _ -> P.string f L.while_; - let cxt = P.paren_group f 1 (fun _ -> expression 0 cxt f e) in + let cxt = P.paren_group f 1 (fun _ -> expression ~level:0 cxt f e) in P.space f ; cxt in @@ -100651,59 +100605,59 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = end | ForRange (for_ident_expression, finish, id, direction, s, env) -> let action cxt = - P.vgroup f 0 @@ fun _ -> - let cxt = P.group f 0 @@ fun _ -> - (* The only place that [semi] may have semantics here *) - P.string f L.for_ ; - P.paren_group f 1 @@ fun _ -> - let cxt, new_id = - match for_ident_expression, finish.expression_desc with - | Some ident_expression , (Number _ | Var _ ) -> - let cxt = pp_var_assign cxt f id in - expression 0 cxt f ident_expression, None - | Some ident_expression, _ -> - let cxt = pp_var_assign cxt f id in - let cxt = expression 1 cxt f ident_expression in - P.space f ; - comma f; - let id = Ext_ident.create (Ident.name id ^ "_finish") in - let cxt = Ext_pp_scope.ident cxt f id in - P.space f ; - P.string f L.eq; - P.space f; - expression 1 cxt f finish, Some id - | None, (Number _ | Var _) -> - cxt, None - | None , _ -> - let id = Ext_ident.create (Ident.name id ^ "_finish") in - let cxt = pp_var_assign cxt f id in - expression 15 cxt f finish, Some id in - semi f ; - P.space f; - let cxt = Ext_pp_scope.ident cxt f id in - P.space f; - let right_prec = - match direction with - | Upto -> - let (_,_,right) = Js_op_util.op_prec Le in - P.string f L.le; - right - | Downto -> - let (_,_,right) = Js_op_util.op_prec Ge in - P.string f L.ge ; - right - in - P.space f ; - let cxt = - expression right_prec cxt f - (match new_id with - | Some i -> E.var i - | None -> finish) in - semi f; - P.space f; - pp_direction f direction; - Ext_pp_scope.ident cxt f id in - block cxt f s in + P.vgroup f 0 ( fun _ -> + let cxt = P.group f 0 (fun _ -> + (* The only place that [semi] may have semantics here *) + P.string f L.for_ ; + P.paren_group f 1 ( fun _ -> + let cxt, new_id = + match for_ident_expression, finish.expression_desc with + | Some ident_expression , (Number _ | Var _ ) -> + let cxt = pp_var_assign cxt f id in + expression ~level:0 cxt f ident_expression, None + | Some ident_expression, _ -> + let cxt = pp_var_assign cxt f id in + let cxt = expression ~level:1 cxt f ident_expression in + P.space f ; + comma f; + let id = Ext_ident.create (Ident.name id ^ "_finish") in + let cxt = Ext_pp_scope.ident cxt f id in + P.space f ; + P.string f L.eq; + P.space f; + expression ~level:1 cxt f finish, Some id + | None, (Number _ | Var _) -> + cxt, None + | None , _ -> + let id = Ext_ident.create (Ident.name id ^ "_finish") in + let cxt = pp_var_assign cxt f id in + expression ~level:15 cxt f finish, Some id in + semi f ; + P.space f; + let cxt = Ext_pp_scope.ident cxt f id in + P.space f; + let right_prec = + match direction with + | Upto -> + let (_,_,right) = Js_op_util.op_prec Le in + P.string f L.le; + right + | Downto -> + let (_,_,right) = Js_op_util.op_prec Ge in + P.string f L.ge ; + right + in + P.space f ; + let cxt = + expression ~level:right_prec cxt f + (match new_id with + | Some i -> E.var i + | None -> finish) in + semi f; + P.space f; + pp_direction f direction; + Ext_pp_scope.ident cxt f id)) in + block cxt f s ) in let lexical = Js_closure.get_lexical_scope env in if Ident_set.is_empty lexical then action cxt @@ -100744,7 +100698,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = return_sp f ; (* P.string f "return ";(\* ASI -- when there is a comment*\) *) P.group f return_indent (fun _ -> - let cxt = expression 0 cxt f e in + let cxt = expression ~level:0 cxt f e in semi f; cxt) (* There MUST be a space between the return and its @@ -100753,27 +100707,26 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = | Int_switch (e, cc, def) -> P.string f L.switch; P.space f; - let cxt = P.paren_group f 1 (fun _ -> expression 0 cxt f e) in + let cxt = P.paren_group f 1 (fun _ -> expression ~level:0 cxt f e) in P.space f; - P.brace_vgroup f 1 @@ fun _ -> - let cxt = loop_case_clauses cxt f (fun f i -> P.string f (string_of_int i) ) cc in - (match def with - | None -> cxt - | Some def -> - P.group f 1 @@ fun _ -> - P.string f L.default; - P.string f L.colon; - P.newline f; - statement_list false cxt f def - ) + P.brace_vgroup f 1 (fun _ -> + let cxt = loop_case_clauses cxt f (fun f i -> P.string f (string_of_int i) ) cc in + match def with + | None -> cxt + | Some def -> + P.group f 1 (fun _ -> + P.string f L.default; + P.string f L.colon; + P.newline f; + statement_list false cxt f def)) | String_switch (e, cc, def) -> P.string f L.switch; P.space f; - let cxt = P.paren_group f 1 @@ fun _ -> expression 0 cxt f e in + let cxt = P.paren_group f 1 (fun _ -> expression ~level:0 cxt f e) in P.space f; P.brace_vgroup f 1 (fun _ -> - let cxt = loop_case_clauses cxt f (fun f i -> Js_dump_string.pp_string f i ) cc in + let cxt = loop_case_clauses cxt f Js_dump_string.pp_string cc in match def with | None -> cxt | Some def -> @@ -100786,37 +100739,38 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = P.string f L.throw; P.space f ; P.group f throw_indent (fun _ -> - let cxt = expression 0 cxt f e in + let cxt = expression ~level:0 cxt f e in semi f ; cxt) (* There must be a space between the return and its argument. A line return would not work *) | Try (b, ctch, fin) -> - P.vgroup f 0 @@ fun _-> - P.string f L.try_; - P.space f ; - let cxt = block cxt f b in - let cxt = - match ctch with - | None -> - cxt - | Some (i, b) -> - P.newline f; - P.string f "catch ("; - let cxt = Ext_pp_scope.ident cxt f i in - P.string f ")"; - block cxt f b in - match fin with - | None -> cxt - | Some b -> - P.group f 1 (fun _ -> - P.string f L.finally; - P.space f; - block cxt f b) + P.vgroup f 0 ( + fun _-> + P.string f L.try_; + P.space f ; + let cxt = block cxt f b in + let cxt = + match ctch with + | None -> + cxt + | Some (i, b) -> + P.newline f; + P.string f "catch ("; + let cxt = Ext_pp_scope.ident cxt f i in + P.string f ")"; + block cxt f b in + match fin with + | None -> cxt + | Some b -> + P.group f 1 (fun _ -> + P.string f L.finally; + P.space f; + block cxt f b)) -and function_body cxt f b = +and function_body (cxt : cxt) f (b : J.block) : unit = match b with - | [] -> cxt + | [] -> () | [s] -> begin match s.statement_desc with | If (bool, @@ -100825,9 +100779,9 @@ and function_body cxt f b = statement_desc = Return {return_value = {expression_desc = Undefined}} }]) -> - statement false cxt f {s with statement_desc = If(bool,then_, [])} + ignore (statement false cxt f {s with statement_desc = If(bool,then_, [])} : cxt) | _ -> - statement false cxt f s + ignore (statement false cxt f s : cxt) end | s :: r -> let cxt = statement false cxt f s in @@ -100841,7 +100795,7 @@ and statement_list top cxt f b = (fun f -> P.newline f ; P.force_newline f ) else P.newline ) - (* (fun f -> P.newline f ; if top then P.force_newline f ) *) + and block cxt f b = (* This one is for '{' *) @@ -100850,29 +100804,19 @@ and block cxt f b = -(* let program f cxt ( x : J.program ) = - let () = P.force_newline f in - let cxt = statement_list true cxt f x.block in - let () = P.force_newline f in - Js_dump_import_export.exports cxt f x.exports *) - -(* let dump_program (x : J.program) oc = - ignore (program (P.from_channel oc) Ext_pp_scope.empty x ) *) - -let string_of_block block - = +let string_of_block (block : J.block) = let buffer = Buffer.create 50 in let f = P.from_buffer buffer in - let _scope = statement_list true Ext_pp_scope.empty f block in + let _ : cxt = statement_list true Ext_pp_scope.empty f block in P.flush f (); Buffer.contents buffer -let string_of_expression e = +let string_of_expression (e : J.expression) = let buffer = Buffer.create 50 in let f = P.from_buffer buffer in - let _scope = expression 0 Ext_pp_scope.empty f e in + let _ : cxt = expression ~level:0 Ext_pp_scope.empty f e in P.flush f (); Buffer.contents buffer diff --git a/lib/js/caml_float.js b/lib/js/caml_float.js index e289c0cb14..7205cab4c5 100644 --- a/lib/js/caml_float.js +++ b/lib/js/caml_float.js @@ -43,15 +43,15 @@ function caml_ldexp_float(x, exp) { var x$prime = x; var exp$prime = exp; if (exp$prime > 1023) { - exp$prime -= 1023; + exp$prime = exp$prime - 1023; x$prime = x$prime * Math.pow(2, 1023); if (exp$prime > 1023) { - exp$prime -= 1023; + exp$prime = exp$prime - 1023; x$prime = x$prime * Math.pow(2, 1023); } } else if (exp$prime < -1023) { - exp$prime += 1023; + exp$prime = exp$prime + 1023; x$prime = x$prime * Math.pow(2, -1023); } return x$prime * Math.pow(2, exp$prime); @@ -70,7 +70,7 @@ function caml_frexp_float(x) { x$prime = x$prime * Math.pow(2, -exp); if (x$prime < 0.5) { x$prime = x$prime * 2; - exp -= 1; + exp = exp - 1; } if (neg) { x$prime = -x$prime; diff --git a/lib/js/caml_int64.js b/lib/js/caml_int64.js index 3ce85d11df..d700cf6614 100644 --- a/lib/js/caml_int64.js +++ b/lib/js/caml_int64.js @@ -245,9 +245,9 @@ function mul(_this, _other) { c32 = c32 + (c16 >>> 16) + a32 * b00; c48 = (c32 >>> 16); c32 = (c32 & 65535) + a16 * b16; - c48 += (c32 >>> 16); + c48 = c48 + (c32 >>> 16); c32 = (c32 & 65535) + a00 * b32; - c48 += (c32 >>> 16); + c48 = c48 + (c32 >>> 16); c32 = c32 & 65535; c48 = c48 + (a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48) & 65535; var hi = c32 | (c48 << 16); @@ -437,7 +437,7 @@ function div(_self, _other) { var approxRes = of_float(approx$1); var approxRem = mul(approxRes, other); while(approxRem[/* hi */0] < 0 || gt(approxRem, rem$1)) { - approx$1 -= delta; + approx$1 = approx$1 - delta; approxRes = of_float(approx$1); approxRem = mul(approxRes, other); };