|
23 | 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
|
24 | 24 |
|
25 | 25 |
|
26 |
| -let batch_compile ppf files = |
| 26 | +let module_name_of_file file = |
| 27 | + String.capitalize |
| 28 | + (Filename.chop_extension @@ Filename.basename file) |
| 29 | + |
| 30 | +let build_queue ppf queue (ast_table : _ Ast_extract.t String_map.t) = |
| 31 | + queue |> Queue.iter (fun modname -> |
| 32 | + match String_map.find modname ast_table with |
| 33 | + | {ast_info = Ml(source_file,ast, opref)} |
| 34 | + -> |
| 35 | + Js_implementation.after_parsing_impl ppf source_file |
| 36 | + opref ast |
| 37 | + | {ast_info = Mli (source_file,ast,opref) ; } |
| 38 | + -> |
| 39 | + Js_implementation.after_parsing_sig ppf source_file |
| 40 | + opref ast |
| 41 | + | {ast_info = Ml_mli(source_file1,impl,opref1,source_file2,intf,opref2)} |
| 42 | + -> |
| 43 | + Js_implementation.after_parsing_sig ppf source_file1 opref1 intf ; |
| 44 | + Js_implementation.after_parsing_impl ppf source_file2 opref2 impl |
| 45 | + | exception Not_found -> assert false |
| 46 | + ) |
| 47 | + |
| 48 | +let build_lazy_queue ppf queue (ast_table : _ Ast_extract.t String_map.t) = |
| 49 | + queue |> Queue.iter (fun modname -> |
| 50 | + match String_map.find modname ast_table with |
| 51 | + | {ast_info = Ml(source_file,lazy ast, opref)} |
| 52 | + -> |
| 53 | + Js_implementation.after_parsing_impl ppf source_file |
| 54 | + opref ast |
| 55 | + | {ast_info = Mli (source_file,lazy ast,opref) ; } |
| 56 | + -> |
| 57 | + Js_implementation.after_parsing_sig ppf source_file |
| 58 | + opref ast |
| 59 | + | {ast_info = Ml_mli(source_file1,lazy impl,opref1,source_file2,lazy intf,opref2)} |
| 60 | + -> |
| 61 | + Js_implementation.after_parsing_sig ppf source_file1 opref1 intf ; |
| 62 | + Js_implementation.after_parsing_impl ppf source_file2 opref2 impl |
| 63 | + | exception Not_found -> assert false |
| 64 | + ) |
| 65 | + |
| 66 | +let build_ast_table ppf files parse_implementation parse_interface = |
| 67 | + List.fold_left |
| 68 | + (fun (acc : _ Ast_extract.t String_map.t) |
| 69 | + source_file -> |
| 70 | + match Ocaml_parse.check_suffix source_file with |
| 71 | + | `Ml, opref -> |
| 72 | + let module_name = module_name_of_file source_file in |
| 73 | + begin match String_map.find module_name acc with |
| 74 | + | exception Not_found -> |
| 75 | + String_map.add module_name |
| 76 | + {Ast_extract.ast_info = |
| 77 | + (Ml (source_file, parse_implementation |
| 78 | + ppf source_file, opref)); |
| 79 | + module_name ; |
| 80 | + } acc |
| 81 | + | {ast_info = (Ml (source_file2, _, _) |
| 82 | + | Ml_mli(source_file2, _, _,_,_,_))} -> |
| 83 | + Bs_exception.error |
| 84 | + (Bs_duplicated_module (source_file, source_file2)) |
| 85 | + | {ast_info = Mli (source_file2, intf, opref2)} |
| 86 | + -> |
| 87 | + String_map.add module_name |
| 88 | + {Ast_extract.ast_info = |
| 89 | + Ml_mli (source_file, |
| 90 | + parse_implementation ppf source_file, |
| 91 | + opref, |
| 92 | + source_file2, |
| 93 | + intf, |
| 94 | + opref2 |
| 95 | + ); |
| 96 | + module_name} acc |
| 97 | + end |
| 98 | + | `Mli, opref -> |
| 99 | + let module_name = module_name_of_file source_file in |
| 100 | + begin match String_map.find module_name acc with |
| 101 | + | exception Not_found -> |
| 102 | + String_map.add module_name |
| 103 | + {Ast_extract.ast_info = (Mli (source_file, parse_interface |
| 104 | + ppf source_file, opref)); |
| 105 | + module_name } acc |
| 106 | + | {ast_info = |
| 107 | + (Mli (source_file2, _, _) | |
| 108 | + Ml_mli(_,_,_,source_file2,_,_)) } -> |
| 109 | + Bs_exception.error |
| 110 | + (Bs_duplicated_module (source_file, source_file2)) |
| 111 | + | {ast_info = Ml (source_file2, impl, opref2)} |
| 112 | + -> |
| 113 | + String_map.add module_name |
| 114 | + {Ast_extract.ast_info = |
| 115 | + Ml_mli |
| 116 | + (source_file2, |
| 117 | + impl, |
| 118 | + opref2, |
| 119 | + source_file, |
| 120 | + parse_interface ppf source_file, |
| 121 | + opref |
| 122 | + ); |
| 123 | + module_name} acc |
| 124 | + end |
| 125 | + ) String_map.empty files |
| 126 | + |
| 127 | + |
| 128 | +module String_set = Depend.StringSet |
| 129 | + |
| 130 | + |
| 131 | +let handle_main_file ppf main_file = |
| 132 | + let dirname = Filename.dirname main_file in |
| 133 | + let files = |
| 134 | + Sys.readdir dirname |
| 135 | + |> Ext_array.to_list_f |
| 136 | + (fun source_file -> |
| 137 | + if Ext_string.ends_with source_file ".ml" || |
| 138 | + Ext_string.ends_with source_file ".mli" then |
| 139 | + Some (Filename.concat dirname source_file) |
| 140 | + else None |
| 141 | + ) in |
| 142 | + let ast_table = |
| 143 | + build_ast_table ppf files |
| 144 | + Ocaml_parse.lazy_parse_implementation |
| 145 | + Ocaml_parse.lazy_parse_interface in |
| 146 | + |
| 147 | + let visited = Hashtbl.create 31 in |
| 148 | + let result = Queue.create () in |
| 149 | + let next module_name = |
| 150 | + match String_map.find module_name ast_table with |
| 151 | + | exception _ -> String_set.empty |
| 152 | + | {ast_info = Ml (_, lazy impl, _)} -> |
| 153 | + Ast_extract.read_parse_and_extract Ml_kind impl |
| 154 | + | {ast_info = Mli (_, lazy intf,_)} -> |
| 155 | + Ast_extract.read_parse_and_extract Mli_kind intf |
| 156 | + | {ast_info = Ml_mli(_,lazy impl, _, _, lazy intf, _)} |
| 157 | + -> |
| 158 | + String_set.union |
| 159 | + (Ast_extract.read_parse_and_extract Ml_kind impl) |
| 160 | + (Ast_extract.read_parse_and_extract Mli_kind intf) |
| 161 | + in |
| 162 | + let rec visit visiting path current = |
| 163 | + if String_set.mem current visiting then |
| 164 | + Bs_exception.error (Bs_cyclic_depends (current::path)) |
| 165 | + else |
| 166 | + if not (Hashtbl.mem visited current) |
| 167 | + && String_map.mem current ast_table then |
| 168 | + begin |
| 169 | + String_set.iter |
| 170 | + (visit |
| 171 | + (String_set.add current visiting) |
| 172 | + (current::path)) |
| 173 | + (next current) ; |
| 174 | + Queue.push current result; |
| 175 | + Hashtbl.add visited current (); |
| 176 | + end in |
| 177 | + visit (String_set.empty) [] (module_name_of_file main_file) ; |
| 178 | + if Js_config.get_diagnose () then |
| 179 | + Format.fprintf Format.err_formatter |
| 180 | + "Order: @[%a@]@." |
| 181 | + (Ext_format.pp_print_queue |
| 182 | + ~pp_sep:Format.pp_print_space |
| 183 | + Format.pp_print_string) |
| 184 | + result ; |
| 185 | + build_lazy_queue ppf result ast_table; |
| 186 | + if not (!Clflags.compile_only) then |
| 187 | + Sys.command |
| 188 | + ("node " ^ Filename.chop_extension main_file ^ ".js") |
| 189 | + else 0 |
| 190 | + |
| 191 | +let batch_compile ppf files main_file = |
| 192 | + Compenv.readenv ppf Before_compile; |
| 193 | + Compmisc.init_path false; |
27 | 194 | if files <> [] then
|
28 |
| - begin |
29 |
| - Compenv.readenv ppf Before_compile; |
30 |
| - Compmisc.init_path false; |
31 |
| - let batch_files : (string, Ast_extract.ast) Hashtbl.t = |
32 |
| - Hashtbl.create 31 in |
33 |
| - files |> List.iter begin fun name -> |
34 |
| - match Ocaml_parse.check_suffix name with |
35 |
| - | `Ml, opref -> |
36 |
| - Hashtbl.add batch_files |
37 |
| - name |
38 |
| - (Ml (Ocaml_parse.parse_implementation ppf name, opref) ) |
39 |
| - | `Mli, opref -> |
40 |
| - Hashtbl.add batch_files name |
41 |
| - (Mli (Ocaml_parse.parse_interface ppf name, opref)) |
| 195 | + begin |
| 196 | + let ast_table = |
| 197 | + build_ast_table ppf files |
| 198 | + Ocaml_parse.parse_implementation |
| 199 | + Ocaml_parse.parse_interface in |
| 200 | + build_queue ppf (Ast_extract.sort ast_table) ast_table |
| 201 | + end |
| 202 | + ; |
| 203 | + if String.length main_file <> 0 then |
| 204 | + handle_main_file ppf main_file |
| 205 | + else 0 |
| 206 | + |
42 | 207 |
|
43 |
| - end; |
44 |
| - let stack, mapping = Ast_extract.prepare batch_files in |
45 |
| - stack |> Queue.iter (fun modname -> |
46 |
| - match Hashtbl.find_all mapping modname with |
47 |
| - | [] -> () |
48 |
| - | [sourcefile] -> |
49 |
| - begin match Hashtbl.find batch_files sourcefile with |
50 |
| - | exception _ -> assert false |
51 |
| - | Ml (ast,opref) |
52 |
| - -> |
53 |
| - Js_implementation.after_parsing_impl ppf sourcefile |
54 |
| - opref ast |
55 |
| - | Mli (ast,opref) |
56 |
| - -> |
57 |
| - Js_implementation.after_parsing_sig ppf sourcefile |
58 |
| - opref ast |
59 |
| - end |
60 |
| - | [sourcefile1;sourcefile2] |
61 |
| - -> (* TODO: check duplicated names *) |
62 |
| - begin match Hashtbl.find batch_files sourcefile1 with |
63 |
| - | exception _ -> assert false |
64 |
| - | Mli (ast,opref) -> |
65 |
| - Js_implementation.after_parsing_sig ppf sourcefile1 opref ast ; |
66 |
| - begin match Hashtbl.find batch_files sourcefile2 with |
67 |
| - | Ml (ast,opref) -> |
68 |
| - Js_implementation.after_parsing_impl ppf sourcefile2 |
69 |
| - opref ast ; |
70 |
| - | _ -> assert false |
71 |
| - end |
72 |
| - | Ml (ast0,opref0) -> |
73 |
| - begin match Hashtbl.find batch_files sourcefile2 with |
74 |
| - | Mli (ast,opref) -> |
75 |
| - Js_implementation.after_parsing_sig ppf sourcefile2 opref ast ; |
76 |
| - Js_implementation.after_parsing_impl ppf sourcefile1 |
77 |
| - opref0 ast0 ; |
78 | 208 |
|
79 |
| - | _ -> assert false |
80 |
| - end |
81 |
| - end |
82 |
| - | _ -> assert false |
83 |
| - ) |
84 |
| - end |
| 209 | + |
0 commit comments