Skip to content

Commit

Permalink
refactor: move Js_pp from Melstd to core (#1292)
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro authored Jan 13, 2025
1 parent 13ac5d1 commit f2ada94
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 29 deletions.
2 changes: 0 additions & 2 deletions jscomp/core/js_dump.mli
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
*)
(* Authors: Jérôme Vouillon, Hongbo Zhang *)

open Import

val statements :
top:bool ->
scope:Js_pp.Scope.t ->
Expand Down
2 changes: 0 additions & 2 deletions jscomp/core/js_dump_program.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

open Import

val dump_program :
output_dir:string ->
package_info:Js_packages_info.t ->
Expand Down
2 changes: 0 additions & 2 deletions jscomp/core/js_dump_property.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

open Import

val property_access : Js_pp.t -> string -> unit
val property_key : J.property_name -> string
2 changes: 0 additions & 2 deletions jscomp/core/js_dump_string.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

open Import

(* Make sure the escaped string conforms to
JS lexing convention *)
val escape_to_string : string -> string
Expand Down
38 changes: 20 additions & 18 deletions jscomp/melstd/js_pp.ml → jscomp/core/js_pp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

open Import

module L = struct
let space = " "
let indent_str = " "
Expand Down Expand Up @@ -199,28 +201,28 @@ let current_line t = t.line
let current_column t = t.column

module Scope = struct
type t = int Map_int.t Map_string.t
type t = int Map_int.t String.Map.t

(* -- "name" --> int map -- stamp --> index suffix *)
let empty : t = Map_string.empty
let empty : t = String.Map.empty

let rec print fmt v =
Format.fprintf fmt "@[<v>{";
Map_string.iter v (fun k m ->
String.Map.iter v (fun k m ->
Format.fprintf fmt "%s: @[%a@],@ " k print_int_map m);
Format.fprintf fmt "}@]"

and print_int_map fmt m =
Map_int.iter m (fun k v -> Format.fprintf fmt "%d - %d" k v)

let add_ident ~mangled:name (stamp : int) (cxt : t) : int * t =
match Map_string.find_opt cxt name with
| None -> (0, Map_string.add cxt name (Map_int.add Map_int.empty stamp 0))
match String.Map.find_opt cxt name with
| None -> (0, String.Map.add cxt name (Map_int.add Map_int.empty stamp 0))
| Some imap -> (
match Map_int.find_opt imap stamp with
| None ->
let v = Map_int.cardinal imap in
(v, Map_string.add cxt name (Map_int.add imap stamp v))
(v, String.Map.add cxt name (Map_int.add imap stamp v))
| Some i -> (i, cxt))

(*
Expand Down Expand Up @@ -255,34 +257,34 @@ module Scope = struct
Here we can guarantee that if mangled name and stamp are not all the same
they can not have a collision *)
let str_of_ident (cxt : t) (id : Ident.t) : string * t =
match Mel_ident.Mangled.of_ident id with
match Ident.Mangled.of_ident id with
| Reserved name -> (name, cxt)
| Mangled name ->
let i, new_cxt = add_ident ~mangled:name (Mel_ident.stamp id) cxt in
let i, new_cxt = add_ident ~mangled:name (Ident.stamp id) cxt in
((if i == 0 then name else Printf.sprintf "%s$%d" name i), new_cxt)

let ident (cxt : t) f (id : Ident.t) : t =
let str, cxt = str_of_ident cxt id in
string f str;
cxt

let merge (cxt : t) (set : Set_ident.t) =
Set_ident.fold set cxt (fun ident acc ->
let merge (cxt : t) (set : Ident.Set.t) =
Ident.Set.fold set cxt (fun ident acc ->
snd
(add_ident
~mangled:(Mel_ident.convert (Ident.name ident))
(Mel_ident.stamp ident) acc))
~mangled:(Ident.convert (Ident.name ident))
(Ident.stamp ident) acc))

(* Assume that all idents are already in [scope]
so both [param/0] and [param/1] are in idents, we don't need
update twice, once is enough *)
let sub_scope (scope : t) (idents : Set_ident.t) : t =
Set_ident.fold idents empty (fun id acc ->
let sub_scope (scope : t) (idents : Ident.Set.t) : t =
Ident.Set.fold idents empty (fun id acc ->
let name = Ident.name id in
let mangled = Mel_ident.convert name in
match Map_string.find_exn scope mangled with
let mangled = Ident.convert name in
match String.Map.find_exn scope mangled with
| exception Not_found -> assert false
| imap ->
if Map_string.mem acc mangled then acc
else Map_string.add acc mangled imap)
if String.Map.mem acc mangled then acc
else String.Map.add acc mangled imap)
end
6 changes: 4 additions & 2 deletions jscomp/melstd/js_pp.mli → jscomp/core/js_pp.mli
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
}
*)

open Import

type t

module Scope : sig
Expand All @@ -42,8 +44,8 @@ module Scope : sig

val empty : t
val print : Format.formatter -> t -> unit
val sub_scope : t -> Set_ident.t -> t
val merge : t -> Set_ident.t -> t
val sub_scope : t -> Ident.Set.t -> t
val merge : t -> Ident.Set.t -> t
val str_of_ident : t -> Ident.t -> string * t
val ident : t -> pp -> Ident.t -> t
end
Expand Down
1 change: 0 additions & 1 deletion jscomp/melstd/melstd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ module Module_system = Module_system
module Ordered_hash_map_local_ident = Ordered_hash_map_local_ident
module Polyvariant = Polyvariant
module Paths = Paths
module Js_pp = Js_pp
module Scc = Scc
module String = String
module Vec = Vec
Expand Down

0 comments on commit f2ada94

Please sign in to comment.