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 (** If set to true matitac wont catch top level exception, so that backtrace
33 * could be inspected setting OCAMLRUNPARAM=b. This flag is enabled passing
36 let paths_to_search_in = ref [];;
37 let quiet_compilation = ref false;;
38 let dont_delete_baseuri = ref false;;
40 let add_l l = fun s -> l := s :: !l ;;
44 "-I", Arg.String (add_l paths_to_search_in),
45 "<path> Adds path to the list of searched paths for the include command";
46 "-q", Arg.Set quiet_compilation, "Turn off verbose compilation";
47 "-preserve", Arg.Set dont_delete_baseuri,
48 "Turns off automatic baseuri cleaning";
51 if BuildTimeConf.debug then
52 [ "-debug", Arg.Set debug,
53 "Do not catch top-level exception (useful for backtrace inspection)"; ]
56 std_arg_spec @ debug_arg_spec
59 sprintf "MatitaC v%s\nUsage: matitac [option ...] file\nOptions:"
64 let run_script is eval_function =
67 | None -> assert false
70 let slash_n_RE = Pcre.regexp "\\n" in
72 if !quiet_compilation then
76 (* dump_status status; *)
77 let stm = GrafiteAstPp.pp_statement stm in
78 let stm = Pcre.replace ~rex:slash_n_RE stm in
80 if String.length stm > 50 then
81 String.sub stm 0 50 ^ " ..."
85 MatitaLog.debug ("Executing: ``" ^ stm ^ "''")
88 eval_function status is cb
92 | CicNotationParser.Parse_error _ as exn -> raise exn
94 MatitaLog.error (MatitaExcPp.to_string exn);
99 let add_script fname = acc := fname :: !acc in
100 Arg.parse arg_spec add_script usage;
103 | _ -> print_endline usage; exit 1
105 let pp_ocaml_mode () =
106 MatitaLog.message "";
107 MatitaLog.message " ** Entering Ocaml mode ** ";
108 MatitaLog.message "";
109 MatitaLog.message "Type 'go ();;' to enter an interactive matitac";
122 let baseuri = MatitaTypes.get_string_option !status "baseuri" in
123 MatitacleanLib.clean_baseuris ~verbose:false [baseuri];
125 with MatitaTypes.Option_error("baseuri", "not found") ->
126 (* no baseuri ==> nothing to clean yet *)
129 let rec interactive_loop () =
130 let str = Ulexing.from_utf8_channel stdin in
133 (MatitaEngine.eval_from_stream_greedy ~include_paths:!paths_to_search_in)
135 | MatitaEngine.Drop -> pp_ocaml_mode ()
136 | Sys.Break -> MatitaLog.error "user break!"; interactive_loop ()
137 | MatitaTypes.Command_error _ -> interactive_loop ()
141 | CicNotationParser.Parse_error (floc,err) ->
142 let (x, y) = CicNotationPt.loc_of_floc floc in
143 MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
145 | exn -> MatitaLog.error (Printexc.to_string exn); interactive_loop ()
148 Helm_registry.load_from BuildTimeConf.matita_conf;
149 CicNotation.load_notation BuildTimeConf.core_notation_script;
151 MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
152 MatitaDb.create_owner_environment ();
153 CicEnvironment.set_trust (* environment trust *)
154 (let trust = Helm_registry.get_bool "matita.environment_trust" in
156 status := Some (ref (Lazy.force MatitaEngine.initial_status));
157 Sys.catch_break true;
161 MatitaInit.initialize_all ();
162 status := Some (ref (Lazy.force MatitaEngine.initial_status));
163 Sys.catch_break true;
164 let origcb = MatitaLog.get_log_callback () in
167 | `Debug | `Message -> ()
168 | `Warning | `Error -> origcb tag s
170 let fname = fname () in
171 if !quiet_compilation then
172 MatitaLog.set_log_callback newcb;
174 let time = Unix.time () in
175 if !quiet_compilation then
176 origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
178 MatitaLog.message (sprintf "execution of %s started:" fname);
180 Ulexing.from_utf8_channel
183 | fname -> open_in fname)
186 (MatitaEngine.eval_from_stream
187 ~include_paths:!paths_to_search_in
188 ~clean_baseuri:(not !dont_delete_baseuri));
189 let elapsed = Unix.time () -. time in
190 let tm = Unix.gmtime elapsed in
192 if tm.Unix.tm_sec > 0 then (string_of_int tm.Unix.tm_sec ^ "''") else ""
195 if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min ^ "' ") else ""
198 if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else ""
200 let proof_status,moo_content_rev =
202 | Some s -> !s.proof_status, !s.moo_content_rev
203 | None -> assert false
205 if proof_status <> MatitaTypes.No_proof then
208 "there are still incomplete proofs at the end of the script";
213 let moo_fname = MatitaMisc.obj_file_of_script fname in
214 MatitaMoo.save_moo moo_fname moo_content_rev;
216 (sprintf "execution of %s completed in %s." fname (hou^min^sec));
221 MatitaLog.error "user break!";
222 if mode = `COMPILER then
223 clean_exit (Some ~-1)
226 | MatitaEngine.Drop ->
227 if mode = `COMPILER then
231 | CicNotationParser.Parse_error (floc,err) ->
232 let (x, y) = CicNotationPt.loc_of_floc floc in
233 MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
234 if mode = `COMPILER then
239 if !debug then raise exn;
240 if mode = `COMPILER then