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/
32 exception AttemptToInsertAnAlias
34 let pp_ast_statement =
35 GrafiteAstPp.pp_statement ~term_pp:CicNotationPp.pp_term
36 ~lazy_term_pp:CicNotationPp.pp_term ~obj_pp:CicNotationPp.pp_obj
38 (** {2 Initialization} *)
40 let grafite_status = (ref None : GrafiteTypes.status option ref)
41 let lexicon_status = (ref None : LexiconEngine.status option ref)
43 let run_script is eval_function =
44 let lexicon_status',grafite_status' =
45 match !lexicon_status,!grafite_status with
46 | Some ss, Some s -> ss,s
49 let slash_n_RE = Pcre.regexp "\\n" in
51 if Helm_registry.get_int "matita.verbosity" < 1 then
54 (fun grafite_status stm ->
55 (* dump_status grafite_status; *)
56 let stm = pp_ast_statement stm in
57 let stm = Pcre.replace ~rex:slash_n_RE stm in
59 if String.length stm > 50 then
60 String.sub stm 0 50 ^ " ..."
64 HLog.debug ("Executing: ``" ^ stm ^ "''"))
67 let grafite_status'', lexicon_status'' =
68 match eval_function lexicon_status' grafite_status' is cb with
71 | (s,Some _)::_ -> raise AttemptToInsertAnAlias
73 lexicon_status := Some lexicon_status'';
74 grafite_status := Some grafite_status''
78 | CicNotationParser.Parse_error _ as exn -> raise exn
80 HLog.error (snd (MatitaExcPp.to_string exn));
84 match Helm_registry.get_list Helm_registry.string "matita.args" with
86 | _ -> MatitaInit.die_usage ()
88 let pp_ocaml_mode () =
90 HLog.message " ** Entering Ocaml mode ** ";
92 HLog.message "Type 'go ();;' to enter an interactive matitac";
101 match !grafite_status with
103 | Some grafite_status ->
105 let baseuri = GrafiteTypes.get_string_option grafite_status "baseuri" in
106 LibraryClean.clean_baseuris ~verbose:false [baseuri];
108 with GrafiteTypes.Option_error("baseuri", "not found") ->
109 (* no baseuri ==> nothing to clean yet *)
112 let rec interactive_loop () =
113 let str = Ulexing.from_utf8_channel stdin in
116 (MatitaEngine.eval_from_stream ~first_statement_only:false ~prompt:true
117 ~include_paths:(Helm_registry.get_list Helm_registry.string
120 | GrafiteEngine.Drop -> pp_ocaml_mode ()
121 | GrafiteEngine.Macro (floc,_) ->
122 let x, y = HExtlib.loc_of_floc floc in
124 (sprintf "A macro has been found in a script at %d-%d" x y);
126 | Sys.Break -> HLog.error "user break!"; interactive_loop ()
127 | GrafiteTypes.Command_error _ -> interactive_loop ()
131 | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
132 let x, y = HExtlib.loc_of_floc floc in
133 HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
135 | exn -> HLog.error (Printexc.to_string exn); interactive_loop ()
138 Helm_registry.load_from BuildTimeConf.matita_conf;
140 MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
141 LibraryDb.create_owner_environment ();
142 CicEnvironment.set_trust (* environment trust *)
144 Helm_registry.get_opt_default Helm_registry.get_bool
145 ~default:true "matita.environment_trust" in
148 Helm_registry.get_list Helm_registry.string "matita.includes" in
149 grafite_status := Some (GrafiteSync.init ());
151 Some (CicNotation2.load_notation ~include_paths
152 BuildTimeConf.core_notation_script);
153 Sys.catch_break true;
157 MatitaInit.initialize_all ();
158 (* must be called after init since args are set by cmdline parsing *)
159 let fname = fname () in
160 let system_mode = Helm_registry.get_bool "matita.system" in
162 Helm_registry.get_list Helm_registry.string "matita.includes" in
163 grafite_status := Some (GrafiteSync.init ());
165 Some (CicNotation2.load_notation ~include_paths
166 BuildTimeConf.core_notation_script);
167 Sys.catch_break true;
168 let origcb = HLog.get_log_callback () in
169 let origcb t s = origcb t ((if system_mode then "[S] " else "") ^ s) in
172 | `Debug | `Message -> ()
173 | `Warning | `Error -> origcb tag s
175 if Helm_registry.get_int "matita.verbosity" < 1 then
176 HLog.set_log_callback newcb;
177 let matita_debug = Helm_registry.get_bool "matita.debug" in
179 let time = Unix.time () in
180 if Helm_registry.get_int "matita.verbosity" < 1 then
181 origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
183 HLog.message (sprintf "execution of %s started:" fname);
185 Ulexing.from_utf8_channel
188 | fname -> open_in fname) in
190 Helm_registry.get_list Helm_registry.string "matita.includes" in
193 (MatitaEngine.eval_from_stream ~first_statement_only:false ~include_paths
194 ~clean_baseuri:(not (Helm_registry.get_bool "matita.preserve")))
195 with End_of_file -> ());
196 let elapsed = Unix.time () -. time in
197 let tm = Unix.gmtime elapsed in
198 let sec = string_of_int tm.Unix.tm_sec ^ "''" in
200 if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min ^ "' ") else ""
203 if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else ""
205 let proof_status,moo_content_rev,metadata,lexicon_content_rev =
206 match !lexicon_status,!grafite_status with
208 s.proof_status, s.moo_content_rev, ss.LexiconEngine.metadata,
209 ss.LexiconEngine.lexicon_content_rev
210 | _,_ -> assert false
212 if proof_status <> GrafiteTypes.No_proof then
215 "there are still incomplete proofs at the end of the script";
221 DependenciesParser.baseuri_of_script ~include_paths fname in
223 LibraryMisc.obj_file_of_baseuri ~baseuri ~writable:true
226 LibraryMisc.lexicon_file_of_baseuri ~baseuri ~writable:true
229 LibraryMisc.metadata_file_of_baseuri ~baseuri ~writable:true
231 GrafiteMarshal.save_moo moo_fname moo_content_rev;
232 LibraryNoDb.save_metadata metadata_fname metadata;
233 LexiconMarshal.save_lexicon lexicon_fname lexicon_content_rev;
235 (sprintf "execution of %s completed in %s." fname (hou^min^sec));
240 HLog.error "user break!";
241 if mode = `COMPILER then
242 clean_exit (Some ~-1)
245 | GrafiteEngine.Drop ->
246 if mode = `COMPILER then
250 | GrafiteEngine.Macro (floc,_) ->
251 let x, y = HExtlib.loc_of_floc floc in
253 (sprintf "A macro has been found in a script at %d-%d" x y);
254 if mode = `COMPILER then
258 | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
259 let (x, y) = HExtlib.loc_of_floc floc in
260 HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
261 if mode = `COMPILER then
266 if matita_debug then raise exn;
267 if mode = `COMPILER then