From 5062c73fc29f6b955e39898ce488ae56a8db3707 Mon Sep 17 00:00:00 2001
From: Thomas Wood <thomas.wood09@imperial.ac.uk>
Date: Thu, 20 Oct 2016 12:11:50 +0200
Subject: [PATCH] Refactor generator stdlib initialisation

ml2js wasn't including . in the include paths by default. Add flag for
explicitly indicating stdlib location.
---
 generator/main.ml    | 24 ++++++++++++++----------
 generator/mytools.ml |  2 ++
 generator/params.ml  |  3 ---
 3 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/generator/main.ml b/generator/main.ml
index f3a2e7d..b6dde0e 100644
--- a/generator/main.ml
+++ b/generator/main.ml
@@ -1,4 +1,3 @@
-
 open Params
 open Format
 open Mytools
@@ -12,6 +11,7 @@ open Mytools
 (*#########################################################################*)
 
 let ppf = Format.std_formatter
+let stdlib_path = ref "stdlib_ml"
 
 (* err_formatter *)
 
@@ -21,18 +21,21 @@ let ppf = Format.std_formatter
 
 let tool_name = "ml2js"
 
+(** Configures the compilers load paths from the commandline and stdlib *)
+(* FIXME: Relative stdlib dir should be absolute or findlib derived
+ * we're manually specified using -I for now... *)
 let init_path () =
-  Config.load_path :=
-    "stdlib_ml" :: List.rev (Config.standard_library :: !Clflags.include_dirs);
+  (* Compmisc.init_path false; (* to use this, lots of parameters need tweaking *)*)
+  Config.load_path := "" :: List.rev_append !Clflags.include_dirs [!stdlib_path];
   Env.reset_cache ()
 
 (** Return the initial environment in which compilation proceeds. *)
 let initial_env () =
-  try
-    let env = Env.initial_unsafe_string in
-    Env.open_pers_signature "Stdlib" env
-  with Not_found ->
-    Misc.fatal_error "cannot open stdlib"
+  Clflags.nopervasives := true;
+  (* Stdlib module name, instead of Pervasives *)
+  add_to_list Compenv.implicit_modules "Stdlib";
+  Compmisc.initial_env ()
+
 
 (** Analysis of an implementation file.
  *  ppf: error printer
@@ -43,8 +46,8 @@ let process_implementation_file ppf sourcefile oprefix =
   init_path ();
   let modulename = Compenv.module_of_filename ppf sourcefile oprefix in
   Env.set_unit_name modulename;
-  let env = initial_env () in
   try
+    let env = initial_env () in
     let parsetree = Pparse.parse_implementation ~tool_name ppf sourcefile in
     if !Clflags.dump_source then fprintf ppf "%a@." Pprintast.structure parsetree;
     let typing = Typemod.type_implementation sourcefile oprefix modulename env parsetree in
@@ -59,7 +62,8 @@ let _ =
 
    let files = ref [] in
    Arg.parse
-     [ ("-I", Arg.String (add_to_list Clflags.include_dirs), "includes a directory where to look for interface files");
+     [ ("-stdlib", Arg.Set_string stdlib_path, "path to look for Stdlib (defaults to 'stdlib_ml')");
+       ("-I", Arg.String (add_to_list Clflags.include_dirs), "includes a directory where to look for interface files");
        ("-o", Arg.String (fun s -> Clflags.output_name := Some s), "set the output file");
        ("-debug", Arg.Set debug, "trace the various steps");
        ("-dsource", Arg.Set Clflags.dump_source, "dump source after ppx");
diff --git a/generator/mytools.ml b/generator/mytools.ml
index ac221d1..3f41fc5 100644
--- a/generator/mytools.ml
+++ b/generator/mytools.ml
@@ -119,6 +119,8 @@ let list_split3 l =
    let l3 = List.map (fun (_,_,x) -> x) l in
    (l1,l2,l3)
 
+let add_to_list li s =
+  li := s :: !li
 
 (**************************************************************)
 (** String manipulation functions *)
diff --git a/generator/params.ml b/generator/params.ml
index 565d948..db41f33 100644
--- a/generator/params.ml
+++ b/generator/params.ml
@@ -2,9 +2,6 @@ let debug = ref false
 
 let (~~) f x y = f y x
 
-let add_to_list li s =
-  li := s :: !li
-
 (****************************************************************)
 (* MODES *)
 
-- 
GitLab