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 exception AttemptToInsertAnAlias
32 let pp_ast_statement =
33 GrafiteAstPp.pp_statement ~term_pp:CicNotationPp.pp_term
34 ~lazy_term_pp:CicNotationPp.pp_term ~obj_pp:CicNotationPp.pp_obj
36 (** {2 Initialization} *)
38 let grafite_status = (ref None : GrafiteTypes.status option ref)
39 let lexicon_status = (ref None : LexiconEngine.status option ref)
41 let run_script is eval_function =
42 let lexicon_status',grafite_status' =
43 match !lexicon_status,!grafite_status with
44 | Some ss, Some s -> ss,s
47 let slash_n_RE = Pcre.regexp "\\n" in
49 if Helm_registry.get_bool "matita.quiet" then
52 (fun grafite_status stm ->
53 (* dump_status grafite_status; *)
54 let stm = pp_ast_statement stm in
55 let stm = Pcre.replace ~rex:slash_n_RE stm in
57 if String.length stm > 50 then
58 String.sub stm 0 50 ^ " ..."
62 HLog.debug ("Executing: ``" ^ stm ^ "''"))
65 let grafite_status'', lexicon_status'' =
66 match eval_function lexicon_status' grafite_status' is cb with
69 | (s,Some _)::_ -> raise AttemptToInsertAnAlias
71 lexicon_status := Some lexicon_status'';
72 grafite_status := Some grafite_status''
76 | CicNotationParser.Parse_error _ as exn -> raise exn
78 HLog.error (snd (MatitaExcPp.to_string exn));
82 match Helm_registry.get_list Helm_registry.string "matita.args" with
84 | _ -> MatitaInit.die_usage ()
86 let pp_ocaml_mode () =
88 HLog.message " ** Entering Ocaml mode ** ";
90 HLog.message "Type 'go ();;' to enter an interactive matitac";
99 match !grafite_status with
101 | Some grafite_status ->
103 let baseuri = GrafiteTypes.get_string_option grafite_status "baseuri" in
104 let basedir = Helm_registry.get "matita.basedir" in
105 LibraryClean.clean_baseuris ~basedir ~verbose:false [baseuri];
107 with GrafiteTypes.Option_error("baseuri", "not found") ->
108 (* no baseuri ==> nothing to clean yet *)
111 let rec interactive_loop () =
112 let str = Ulexing.from_utf8_channel stdin in
115 (MatitaEngine.eval_from_stream ~first_statement_only:false ~prompt:true
116 ~include_paths:(Helm_registry.get_list Helm_registry.string
119 | GrafiteEngine.Drop -> pp_ocaml_mode ()
120 | Sys.Break -> HLog.error "user break!"; interactive_loop ()
121 | GrafiteTypes.Command_error _ -> interactive_loop ()
125 | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
126 let (x, y) = HExtlib.loc_of_floc floc in
127 HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
129 | exn -> HLog.error (Printexc.to_string exn); interactive_loop ()
132 Helm_registry.load_from BuildTimeConf.matita_conf;
134 MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
135 LibraryDb.create_owner_environment ();
136 CicEnvironment.set_trust (* environment trust *)
137 (let trust = Helm_registry.get_bool "matita.environment_trust" in
140 Helm_registry.get_list Helm_registry.string "matita.includes" in
141 grafite_status := Some (GrafiteSync.init ());
143 Some (CicNotation2.load_notation ~include_paths
144 BuildTimeConf.core_notation_script);
145 Sys.catch_break true;
149 MatitaInit.initialize_all ();
150 (* must be called after init since args are set by cmdline parsing *)
151 let fname = fname () in
153 Helm_registry.get_list Helm_registry.string "matita.includes" in
154 grafite_status := Some (GrafiteSync.init ());
156 Some (CicNotation2.load_notation ~include_paths
157 BuildTimeConf.core_notation_script);
158 Sys.catch_break true;
159 let origcb = HLog.get_log_callback () in
162 | `Debug | `Message -> ()
163 | `Warning | `Error -> origcb tag s
165 if Helm_registry.get_bool "matita.quiet" then
166 HLog.set_log_callback newcb;
167 let matita_debug = Helm_registry.get_bool "matita.debug" in
169 let time = Unix.time () in
170 if Helm_registry.get_bool "matita.quiet" then
171 origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
173 HLog.message (sprintf "execution of %s started:" fname);
175 Ulexing.from_utf8_channel
178 | fname -> open_in fname) in
180 Helm_registry.get_list Helm_registry.string "matita.includes" in
183 (MatitaEngine.eval_from_stream ~first_statement_only:false ~include_paths
184 ~clean_baseuri:(not (Helm_registry.get_bool "matita.preserve")))
185 with End_of_file -> ());
186 let elapsed = Unix.time () -. time in
187 let tm = Unix.gmtime elapsed in
188 let sec = string_of_int tm.Unix.tm_sec ^ "''" in
190 if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min ^ "' ") else ""
193 if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else ""
195 let proof_status,moo_content_rev,metadata,lexicon_content_rev =
196 match !lexicon_status,!grafite_status with
198 s.proof_status, s.moo_content_rev, ss.LexiconEngine.metadata,
199 ss.LexiconEngine.lexicon_content_rev
200 | _,_ -> assert false
202 if proof_status <> GrafiteTypes.No_proof then
205 "there are still incomplete proofs at the end of the script";
210 let basedir = Helm_registry.get "matita.basedir" in
212 DependenciesParser.baseuri_of_script ~include_paths fname in
213 let moo_fname = LibraryMisc.obj_file_of_baseuri ~basedir ~baseuri in
214 let lexicon_fname= LibraryMisc.lexicon_file_of_baseuri ~basedir ~baseuri in
216 LibraryMisc.metadata_file_of_baseuri ~basedir ~baseuri
218 GrafiteMarshal.save_moo moo_fname moo_content_rev;
219 LibraryNoDb.save_metadata metadata_fname metadata;
220 LexiconMarshal.save_lexicon lexicon_fname lexicon_content_rev;
222 (sprintf "execution of %s completed in %s." fname (hou^min^sec));
227 HLog.error "user break!";
228 if mode = `COMPILER then
229 clean_exit (Some ~-1)
232 | GrafiteEngine.Drop ->
233 if mode = `COMPILER then
237 | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
238 let (x, y) = HExtlib.loc_of_floc floc in
239 HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
240 if mode = `COMPILER then
245 if matita_debug then raise exn;
246 if mode = `COMPILER then