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 = GrafiteAstPp.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
77 | CicNotationParser.Parse_error _ as exn -> raise exn
79 MatitaLog.error (MatitaExcPp.to_string exn);
84 let add_script fname = acc := fname :: !acc in
85 Arg.parse arg_spec add_script usage;
88 | _ -> prerr_endline usage; exit 1
90 let pp_ocaml_mode () =
92 MatitaLog.message " ** Entering Ocaml mode ** ";
94 MatitaLog.message "Type 'go ();;' to enter an interactive matitac";
107 let baseuri = MatitaTypes.get_string_option !status "baseuri" in
108 MatitacleanLib.clean_baseuris ~verbose:false [baseuri];
110 with MatitaTypes.Option_error("baseuri", "not found") ->
111 (* no baseuri ==> nothing to clean yet *)
114 let rec interactive_loop () =
115 let str = Stream.of_channel stdin in
118 (MatitaEngine.eval_from_stream_greedy ~include_paths:!paths_to_search_in)
120 | MatitaEngine.Drop -> pp_ocaml_mode ()
121 | Sys.Break -> MatitaLog.error "user break!"; interactive_loop ()
122 | MatitaTypes.Command_error _ -> interactive_loop ()
126 | CicNotationParser.Parse_error (floc,err) ->
127 let (x, y) = CicNotationPt.loc_of_floc floc in
128 MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
130 | exn -> MatitaLog.error (Printexc.to_string exn); interactive_loop ()
133 Helm_registry.load_from BuildTimeConf.matita_conf;
135 MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
136 status := Some (ref (Lazy.force MatitaEngine.initial_status));
137 Sys.catch_break true;
140 let dump_moo_to_file file moo =
141 let os = open_out (MatitaMisc.obj_file_of_script file) in
142 let output s = output_string os s in
143 output "(* GENERATED FILE: DO NOT EDIT! *)\n\n";
144 List.iter output (List.rev moo);
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 status := Some (ref (Lazy.force MatitaEngine.initial_status));
154 Sys.catch_break true;
155 let origcb = MatitaLog.get_log_callback () in
158 | `Debug | `Message -> ()
159 | `Warning | `Error -> origcb tag s
161 let fname = fname () in
162 if !quiet_compilation then
163 MatitaLog.set_log_callback newcb;
165 let time = Unix.time () in
166 if !quiet_compilation then
167 origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
169 MatitaLog.message (sprintf "execution of %s started:" fname);
174 | fname -> open_in fname)
177 (MatitaEngine.eval_from_stream ~include_paths:!paths_to_search_in);
178 let elapsed = Unix.time () -. time in
179 let tm = Unix.gmtime elapsed in
181 if tm.Unix.tm_sec > 0 then (string_of_int tm.Unix.tm_sec ^ "''") else ""
184 if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min ^ "' ") else ""
187 if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else ""
189 let proof_status,moo_content_rev =
191 | Some s -> !s.proof_status, !s.moo_content_rev
192 | None -> assert false
194 if proof_status <> MatitaTypes.No_proof then
197 "there are still incomplete proofs at the end of the script";
202 dump_moo_to_file fname moo_content_rev;
204 (sprintf "execution of %s completed in %s." fname (hou^min^sec));
209 MatitaLog.error "user break!";
210 if mode = `COMPILER then
211 clean_exit (Some ~-1)
214 | MatitaEngine.Drop ->
215 if mode = `COMPILER then
219 | CicNotationParser.Parse_error (floc,err) ->
220 let (x, y) = CicNotationPt.loc_of_floc floc in
221 MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
222 if mode = `COMPILER then
227 if mode = `COMPILER then