1 (* Copyright (C) 2004-2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
30 (** {2 Initialization} *)
32 let paths_to_search_in = ref [];;
33 let quiet_compilation = ref false;;
35 let add_l l = fun s -> l := s :: !l ;;
36 let set_b b = fun () -> b := true;;
39 "-I", Arg.String (add_l paths_to_search_in),
40 "<path> Adds path to the list of searched paths for the include command";
41 "-q", Arg.Unit (set_b quiet_compilation), "Turn off verbose compilation"
44 sprintf "MatitaC v%s\nUsage: matitac [option ...] file\nOptions:"
49 let run_script is eval_function =
52 | None -> assert false
55 let slash_n_RE = Pcre.regexp "\\n" in
57 if !quiet_compilation then
61 (* dump_status status; *)
62 let stm = TacticAstPp.pp_statement stm in
63 let stm = Pcre.replace ~rex:slash_n_RE stm in
65 if String.length stm > 50 then
66 String.sub stm 0 50 ^ " ..."
70 MatitaLog.debug ("Executing: ``" ^ stm ^ "''")
73 eval_function status is cb
76 | CicTextualParser2.Parse_error _ as exn -> raise exn
78 MatitaLog.error (MatitaExcPp.to_string exn);
83 let add_script fname = acc := fname :: !acc in
84 Arg.parse arg_spec add_script usage;
87 | _ -> prerr_endline usage; exit 1
89 let pp_ocaml_mode () =
91 MatitaLog.message " ** Entering Ocaml mode ** ";
93 MatitaLog.message "Type 'go ();;' to enter an interactive matitac";
106 let baseuri = MatitaTypes.get_string_option !status "baseuri" in
107 MatitacleanLib.clean_baseuris ~verbose:false [baseuri];
109 with MatitaTypes.Option_error("baseuri", "not found") ->
110 (* no baseuri ==> nothing to clean yet *)
113 let rec interactive_loop () =
114 let str = Stream.of_channel stdin in
117 (MatitaEngine.eval_from_stream_greedy ~include_paths:!paths_to_search_in)
119 | MatitaEngine.Drop -> pp_ocaml_mode ()
120 | Sys.Break -> MatitaLog.error "user break!"; interactive_loop ()
121 | MatitaTypes.Command_error _ -> interactive_loop ()
122 | CicTextualParser2.Parse_error (floc,err) ->
124 if Stream.peek str = None then
130 let (x, y) = CicAst.loc_of_floc floc in
131 MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
133 | exn -> MatitaLog.error (Printexc.to_string exn); interactive_loop ()
136 Helm_registry.load_from BuildTimeConf.matita_conf;
138 MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
139 status := Some (ref (Lazy.force MatitaEngine.initial_status));
140 Sys.catch_break true;
143 let dump_moo_to_file file moo =
144 let os = open_out (MatitaMisc.obj_file_of_script file) in
145 let output s = output_string os s in
146 output "(* GENERATED FILE: DO NOT EDIT! *)\n\n";
147 List.iter output (List.rev moo);
151 Helm_registry.load_from BuildTimeConf.matita_conf;
153 MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
154 MatitaDb.create_owner_environment ();
155 status := Some (ref (Lazy.force MatitaEngine.initial_status));
156 Sys.catch_break true;
157 let origcb = MatitaLog.get_log_callback () in
160 | `Debug | `Message -> ()
161 | `Warning | `Error -> origcb tag s
163 let fname = fname () in
164 if !quiet_compilation then
165 MatitaLog.set_log_callback newcb;
167 let time = Unix.time () in
168 if !quiet_compilation then
169 origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
171 MatitaLog.message (sprintf "execution of %s started:" fname);
176 | fname -> open_in fname)
179 (MatitaEngine.eval_from_stream ~include_paths:!paths_to_search_in);
180 let elapsed = Unix.time () -. time in
181 let tm = Unix.gmtime elapsed in
183 if tm.Unix.tm_sec > 0 then (string_of_int tm.Unix.tm_sec ^ "''") else ""
186 if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min ^ "' ") else ""
189 if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else ""
191 let proof_status,moo_content_rev =
193 | Some s -> !s.proof_status, !s.moo_content_rev
194 | None -> assert false
196 if proof_status <> MatitaTypes.No_proof then
199 "there are still incomplete proofs at the end of the script";
204 dump_moo_to_file fname moo_content_rev;
206 (sprintf "execution of %s completed in %s." fname (hou^min^sec));
211 MatitaLog.error "user break!";
212 if mode = `COMPILER then
213 clean_exit (Some ~-1)
216 | MatitaEngine.Drop ->
217 if mode = `COMPILER then
221 | CicTextualParser2.Parse_error (floc,err) ->
222 let (x, y) = CicAst.loc_of_floc floc in
223 MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
224 if mode = `COMPILER then
229 if mode = `COMPILER then