Skip to content

Commit

Permalink
refactor: add labels to Melstd.List (#1233)
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro authored Nov 24, 2024
1 parent 53685c6 commit 72fdd45
Show file tree
Hide file tree
Showing 19 changed files with 102 additions and 97 deletions.
2 changes: 1 addition & 1 deletion bin/melc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ let anonymous =
else
begin
if !Js_config.syntax_only then begin
List.rev_iter rev_args (fun filename ->
List.rev_iter rev_args ~f:(fun filename ->
begin
(* Clflags.reset_dump_state (); *)
(* Warnings.reset (); *)
Expand Down
4 changes: 2 additions & 2 deletions jscomp/common/lam_constant.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ let rec eq_approx (x : t) (y : t) =
| Const_block (ix, _, ixs) -> (
match y with
| Const_block (iy, _, iys) ->
ix = iy && List.for_all2_no_exn ixs iys eq_approx
ix = iy && List.for_all2_no_exn ixs iys ~f:eq_approx
| _ -> false)
| Const_float_array ixs -> (
match y with
| Const_float_array iys -> List.for_all2_no_exn ixs iys String.equal
| Const_float_array iys -> List.for_all2_no_exn ixs iys ~f:String.equal
| _ -> false)
| Const_some ix -> (
match y with Const_some iy -> eq_approx ix iy | _ -> false)
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/js_analyzer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,10 @@ let rec eq_expression ({ expression_desc = x0; _ } : J.expression)
| Number (Uint _) ->
false

and eq_expression_list xs ys = List.for_all2_no_exn xs ys eq_expression
and eq_expression_list xs ys = List.for_all2_no_exn xs ys ~f:eq_expression

and eq_block (xs : J.block) (ys : J.block) =
List.for_all2_no_exn xs ys eq_statement
List.for_all2_no_exn xs ys ~f:eq_statement

and eq_statement ({ statement_desc = x0; _ } : J.statement)
({ statement_desc = y0; _ } : J.statement) =
Expand Down
10 changes: 5 additions & 5 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ and pp_function ~return_unit ~is_method cxt ~fn_state (l : Ident.t list)
it can be optimized in to either [u] or [Curry.__n(u)]
*)
(not is_method)
&& List.for_all2_no_exn ls l is_var
&& List.for_all2_no_exn ls l ~f:is_var
&&
match v with
(* This check is needed to avoid some edge cases
Expand Down Expand Up @@ -776,14 +776,14 @@ and expression_desc cxt ~(level : int) x : cxt =
| Caml_block { fields = el; tag_info = Blk_module fields; _ } ->
expression_desc cxt ~level
(Object
(List.map_combine fields el (fun x -> Js_op.Lit (Ident.convert x))))
(List.map_combine fields el ~f:(fun x -> Js_op.Lit (Ident.convert x))))
(*name convention of Record is slight different from modules*)
| Caml_block { fields = el; mutable_flag; tag_info = Blk_record fields; _ } ->
if block_has_all_int_fields fields then
expression_desc cxt ~level (Array { items = el; mutable_flag })
else
expression_desc cxt ~level
(Object (List.map_combine_array fields el (fun i -> Js_op.Lit i)))
(Object (List.map_combine_array fields el ~f:(fun i -> Js_op.Lit i)))
| Caml_block { fields = el; tag_info = Blk_poly_var; _ } -> (
match el with
| [ { expression_desc = Str name; _ }; value ] ->
Expand All @@ -802,9 +802,9 @@ and expression_desc cxt ~(level : int) x : cxt =
let objs =
let tails =
List.map_combine_array_append p.fields el
(if !Js_config.debug then [ (Js_op.Symbol_name, E.str p.name) ]
~init:(if !Js_config.debug then [ (Js_op.Symbol_name, E.str p.name) ]
else [])
(fun i -> Js_op.Lit i)
~f:(fun i -> Js_op.Lit i)
in
let as_value =
Lam_constant_convert.modifier ~name:p.name p.attributes
Expand Down
6 changes: 3 additions & 3 deletions jscomp/core/js_dump_import_export.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let rev_iter_inter lst f inter =
| [] -> ()
| [ a ] -> f a
| a :: rest ->
List.rev_iter rest (fun x ->
List.rev_iter rest ~f:(fun x ->
f x;
inter ());
f a
Expand Down Expand Up @@ -113,7 +113,7 @@ let requires cxt f modules =
~init:(cxt, []) modules
in
P.at_least_two_lines f;
List.rev_iter reversed_list (fun (s, file, default) ->
List.rev_iter reversed_list ~f:(fun (s, file, default) ->
P.string f L.const;
P.space f;
P.string f s;
Expand All @@ -140,7 +140,7 @@ let imports cxt f modules =
~init:(cxt, []) modules
in
P.at_least_two_lines f;
List.rev_iter reversed_list (fun (s, file, default) ->
List.rev_iter reversed_list ~f:(fun (s, file, default) ->
P.string f L.import;
P.space f;
if default then (
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/js_dump_program.ml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ let pp_deps_program =
(* This is empty module, it won't be referred anywhere *)
else
let comments, program = extract_file_comments program in
List.rev_iter comments (fun comment ->
List.rev_iter comments ~f:(fun comment ->
P.string f comment;
P.newline f);
let output_dir = Filename.dirname output_prefix in
Expand Down
12 changes: 6 additions & 6 deletions jscomp/core/lam.ml
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ let inner_map (l : t) (f : t -> X.t) : X.t =
Lmutlet (id, arg, body)
| Lletrec (decl, body) ->
let body = f body in
let decl = List.map_snd decl f in
let decl = List.map_snd decl ~f in
Lletrec (decl, body)
| Lglobal_module _ -> (l : X.t)
| Lprim { args; primitive; loc } ->
Expand All @@ -208,8 +208,8 @@ let inner_map (l : t) (f : t -> X.t) : X.t =
sw_names;
} ) ->
let arg = f arg in
let sw_consts = List.map_snd sw_consts f in
let sw_blocks = List.map_snd sw_blocks f in
let sw_consts = List.map_snd sw_consts ~f in
let sw_blocks = List.map_snd sw_blocks ~f in
let sw_failaction = Option.map f sw_failaction in
Lswitch
( arg,
Expand All @@ -223,7 +223,7 @@ let inner_map (l : t) (f : t -> X.t) : X.t =
} )
| Lstringswitch (arg, cases, default) ->
let arg = f arg in
let cases = List.map_snd cases f in
let cases = List.map_snd cases ~f in
let default = Option.map f default in
Lstringswitch (arg, cases, default)
| Lstaticraise (id, args) ->
Expand Down Expand Up @@ -413,7 +413,7 @@ let rec eq_approx (l1 : t) (l2 : t) =
| Lstringswitch (arg2, patterns2, default2) ->
eq_approx arg arg2 && eq_option default default2
&& List.for_all2_no_exn patterns patterns2
(fun ((k : string), v) (k2, v2) -> k = k2 && eq_approx v v2)
~f:(fun ((k : string), v) (k2, v2) -> k = k2 && eq_approx v v2)
| _ -> false)
| Lfunction _
| Llet (_, _, _, _)
Expand All @@ -432,7 +432,7 @@ and eq_option l1 l2 =
| None -> l2 = None
| Some l1 -> ( match l2 with Some l2 -> eq_approx l1 l2 | None -> false)

and eq_approx_list ls ls1 = List.for_all2_no_exn ls ls1 eq_approx
and eq_approx_list ls ls1 = List.for_all2_no_exn ls ls1 ~f:eq_approx

let switch lam (lam_switch : lambda_switch) : t =
match lam with
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/lam_arity.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let equal (x : t) y =
| Arity_info (xs, a) -> (
match y with
| Arity_info (ys, b) ->
a = b && List.for_all2_no_exn xs ys (fun x y -> x = y)
a = b && List.for_all2_no_exn xs ys ~f:(fun x y -> x = y)
| Arity_na -> false)

let pp = Format.fprintf
Expand Down
6 changes: 3 additions & 3 deletions jscomp/core/lam_bounded_vars.ml
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,16 @@ let rewrite (map : _ Ident.Hash.t) (lam : Lam.t) : Lam.t =
let l = aux l in
Lam.switch l
{
sw_consts = List.map_snd sw_consts aux;
sw_blocks = List.map_snd sw_blocks aux;
sw_consts = List.map_snd sw_consts ~f:aux;
sw_blocks = List.map_snd sw_blocks ~f:aux;
sw_consts_full;
sw_blocks_full;
sw_failaction = option_map sw_failaction;
sw_names;
}
| Lstringswitch (l, sw, d) ->
let l = aux l in
Lam.stringswitch l (List.map_snd sw aux) (option_map d)
Lam.stringswitch l (List.map_snd sw ~f:aux) (option_map d)
| Lstaticraise (i, ls) -> Lam.staticraise i (List.map ~f:aux ls)
| Ltrywith (l1, v, l2) ->
let l1 = aux l1 in
Expand Down
11 changes: 6 additions & 5 deletions jscomp/core/lam_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ let morph_declare_to_assign (cxt : Lam_compile_context.t) k =

let group_apply cases callback =
List.concat_map
~f:(fun group -> List.map_last group callback)
(List.stable_group cases (fun (_, lam) (_, lam1) -> Lam.eq_approx lam lam1))
~f:(fun group -> List.map_last group ~f:callback)
(List.stable_group cases ~equal:(fun (_, lam) (_, lam1) ->
Lam.eq_approx lam lam1))
(* TODO:
for expression generation,
name, should_return is not needed,
Expand Down Expand Up @@ -1244,7 +1245,7 @@ and compile_send (meth_kind : Lam_compat.meth_kind) (met : Lam.t) (obj : Lam.t)
(args : Lam.t list) (lambda_cxt : Lam_compile_context.t) =
let new_cxt = { lambda_cxt with continuation = NeedValue Not_tail } in
match
List.split_map (met :: obj :: args) (fun x ->
List.split_map (met :: obj :: args) ~f:(fun x ->
match x with
| Lprim { primitive = Pccall { prim_name; _ }; args = []; _ }
(* nullary external call*) ->
Expand Down Expand Up @@ -1743,7 +1744,7 @@ and compile_prim (prim_info : Lam.prim_info)
if args = [] then ([], [])
else
let new_cxt = { lambda_cxt with continuation = NeedValue Not_tail } in
List.split_map args (fun x ->
List.split_map args ~f:(fun x ->
match compile_lambda new_cxt x with
| { block; value = Some b; _ } -> (block, b)
| { value = None; _ } -> assert false)
Expand Down Expand Up @@ -1836,7 +1837,7 @@ and compile_prim (prim_info : Lam.prim_info)
if args = [] then ([], [])
else
let new_cxt = { lambda_cxt with continuation = NeedValue Not_tail } in
List.split_map args (fun x ->
List.split_map args ~f:(fun x ->
match compile_lambda new_cxt x with
| { block; value = Some b; _ } -> (block, b)
| { value = None; _ } -> assert false)
Expand Down
6 changes: 3 additions & 3 deletions jscomp/core/lam_compile_context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ let no_static_raise_in_handler (x : handler) : bool =
let add_jmps (m : jmp_table) (exit_id : Ident.t) (code_table : handler list) :
jmp_table * (int * Lam.t) list =
let map, handlers =
List.fold_left_with_offset code_table (m, [])
(HandlerMap.cardinal m + 1)
(fun { label; handler; bindings } (acc, handlers) order_id ->
List.fold_left_with_offset code_table ~init:(m, [])
~off:(HandlerMap.cardinal m + 1)
~f:(fun { label; handler; bindings } (acc, handlers) order_id ->
( HandlerMap.add acc label { exit_id; bindings; order_id },
(order_id, handler) :: handlers ))
in
Expand Down
10 changes: 5 additions & 5 deletions jscomp/core/lam_convert.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ let convert (exports : Ident.Set.t) (lam : Lambda.lambda) :
| Lswitch (e, s, _loc) -> convert_switch e s
| Lstringswitch (e, cases, default, _) ->
Lam.stringswitch (convert_aux e)
(List.map_snd cases convert_aux)
(List.map_snd cases ~f:convert_aux)
(Option.map convert_aux default)
| Lstaticraise (id, []) ->
Lam.staticraise (Hash_int.find_default exit_map id id) []
Expand Down Expand Up @@ -914,7 +914,7 @@ let convert (exports : Ident.Set.t) (lam : Lambda.lambda) :
ap_args = args;
_;
}
when List.for_all2_no_exn inner_args params lam_is_var
when List.for_all2_no_exn inner_args params ~f:lam_is_var
&& List.length_larger_than_n inner_args args 1 ->
Lam.prim ~primitive ~args:(args @ [ x ])
(Debuginfo.Scoped_location.to_location outer_loc)
Expand Down Expand Up @@ -944,7 +944,7 @@ let convert (exports : Ident.Set.t) (lam : Lambda.lambda) :
sw_names;
_;
} -> (
let sw_consts = List.map_snd sw_consts convert_aux in
let sw_consts = List.map_snd sw_consts ~f:convert_aux in
match happens_to_be_diff sw_consts sw_names with
| Some 0l -> e
| Some i ->
Expand All @@ -965,9 +965,9 @@ let convert (exports : Ident.Set.t) (lam : Lambda.lambda) :
Lam.switch e
{
sw_consts_full = List.length_ge s.sw_consts s.sw_numconsts;
sw_consts = List.map_snd s.sw_consts convert_aux;
sw_consts = List.map_snd s.sw_consts ~f:convert_aux;
sw_blocks_full = List.length_ge s.sw_blocks s.sw_numblocks;
sw_blocks = List.map_snd s.sw_blocks convert_aux;
sw_blocks = List.map_snd s.sw_blocks ~f:convert_aux;
sw_failaction = Option.map convert_aux s.sw_failaction;
sw_names = s.sw_names;
}
Expand Down
9 changes: 5 additions & 4 deletions jscomp/core/lam_pass_alpha_conversion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ let alpha_conversion (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
| Llet (str, v, l1, l2) -> Lam.let_ str v (simpl l1) (simpl l2)
| Lmutlet (v, l1, l2) -> Lam.mutlet v (simpl l1) (simpl l2)
| Lletrec (bindings, body) ->
let bindings = List.map_snd bindings simpl in
let bindings = List.map_snd bindings ~f:simpl in
Lam.letrec bindings (simpl body)
| Lglobal_module _ -> lam
| Lprim { primitive = Pjs_fn_make len as primitive; args = [ arg ]; loc }
Expand Down Expand Up @@ -89,15 +89,16 @@ let alpha_conversion (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
} ) ->
Lam.switch (simpl l)
{
sw_consts = List.map_snd sw_consts simpl;
sw_blocks = List.map_snd sw_blocks simpl;
sw_consts = List.map_snd sw_consts ~f:simpl;
sw_blocks = List.map_snd sw_blocks ~f:simpl;
sw_consts_full;
sw_blocks_full;
sw_failaction = Option.map simpl sw_failaction;
sw_names;
}
| Lstringswitch (l, sw, d) ->
Lam.stringswitch (simpl l) (List.map_snd sw simpl) (Option.map simpl d)
Lam.stringswitch (simpl l) (List.map_snd sw ~f:simpl)
(Option.map simpl d)
| Lstaticraise (i, ls) -> Lam.staticraise i (List.map ~f:simpl ls)
| Lstaticcatch (l1, ids, l2) -> Lam.staticcatch (simpl l1) ids (simpl l2)
| Ltrywith (l1, v, l2) -> Lam.try_ (simpl l1) v (simpl l2)
Expand Down
11 changes: 6 additions & 5 deletions jscomp/core/lam_pass_deep_flatten.ml
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,16 @@ let deep_flatten =
match eliminate_tuple id body Map_int.empty with
| Some (tuple_mapping, body) ->
flatten
(List.fold_left_with_offset args accux 0 (fun arg acc i ->
(List.fold_left_with_offset args ~init:accux ~off:0
~f:(fun arg acc i ->
match Map_int.find_opt tuple_mapping i with
| None -> Lam_group.nop_cons arg acc
| Some key -> Lam_group.single kind key arg :: acc))
body
| None -> flatten (Single (kind, id, res) :: accux) body)
| _ -> flatten (Single (kind, id, res) :: accux) body)
| Lletrec (bind_args, body) ->
flatten (Recursive (List.map_snd bind_args aux) :: acc) body
flatten (Recursive (List.map_snd bind_args ~f:aux) :: acc) body
| Lsequence (l, r) ->
let res, l = flatten acc l in
flatten (Lam_group.nop_cons res l) r
Expand Down Expand Up @@ -303,15 +304,15 @@ let deep_flatten =
} ) ->
Lam.switch (aux l)
{
sw_consts = List.map_snd sw_consts aux;
sw_blocks = List.map_snd sw_blocks aux;
sw_consts = List.map_snd sw_consts ~f:aux;
sw_blocks = List.map_snd sw_blocks ~f:aux;
sw_consts_full;
sw_blocks_full;
sw_failaction = Option.map aux sw_failaction;
sw_names;
}
| Lstringswitch (l, sw, d) ->
Lam.stringswitch (aux l) (List.map_snd sw aux) (Option.map aux d)
Lam.stringswitch (aux l) (List.map_snd sw ~f:aux) (Option.map aux d)
| Lstaticraise (i, ls) -> Lam.staticraise i (List.map ~f:aux ls)
| Lstaticcatch (l1, ids, l2) -> Lam.staticcatch (aux l1) ids (aux l2)
| Ltrywith (l1, v, l2) -> Lam.try_ (aux l1) v (aux l2)
Expand Down
9 changes: 5 additions & 4 deletions jscomp/core/lam_pass_exits.ml
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,15 @@ let subst_helper ~try_depth (subst : subst_tbl)
| Llet (kind, v, l1, l2) -> Lam.let_ kind v (simplif l1) (simplif l2)
| Lmutlet (v, l1, l2) -> Lam.mutlet v (simplif l1) (simplif l2)
| Lletrec (bindings, body) ->
Lam.letrec (List.map_snd bindings simplif) (simplif body)
Lam.letrec (List.map_snd bindings ~f:simplif) (simplif body)
| Lglobal_module _ -> lam
| Lprim { primitive; args; loc } ->
let args = List.map ~f:simplif args in
Lam.prim ~primitive ~args loc
| Lswitch (l, sw) ->
let new_l = simplif l in
let new_consts = List.map_snd sw.sw_consts simplif in
let new_blocks = List.map_snd sw.sw_blocks simplif in
let new_consts = List.map_snd sw.sw_consts ~f:simplif in
let new_blocks = List.map_snd sw.sw_blocks ~f:simplif in
let new_fail = Option.map simplif sw.sw_failaction in
Lam.switch new_l
{
Expand All @@ -228,7 +228,8 @@ let subst_helper ~try_depth (subst : subst_tbl)
sw_failaction = new_fail;
}
| Lstringswitch (l, sw, d) ->
Lam.stringswitch (simplif l) (List.map_snd sw simplif)
Lam.stringswitch (simplif l)
(List.map_snd sw ~f:simplif)
(Option.map simplif d)
| Ltrywith (l1, v, l2) ->
incr try_depth;
Expand Down
Loading

0 comments on commit 72fdd45

Please sign in to comment.