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 let pp_ast_statement =
31 GrafiteAstPp.pp_statement ~term_pp:CicNotationPp.pp_term
32 ~lazy_term_pp:CicNotationPp.pp_term ~obj_pp:CicNotationPp.pp_obj
34 (** {2 Initialization} *)
36 let grafite_status = (ref None : GrafiteTypes.status option ref)
37 let lexicon_status = (ref None : LexiconEngine.status option ref)
39 let run_script is eval_function =
40 let lexicon_status',grafite_status' =
41 match !lexicon_status,!grafite_status with
42 | Some ss, Some s -> ss,s
45 let slash_n_RE = Pcre.regexp "\\n" in
47 if Helm_registry.get_bool "matita.quiet" then
50 (fun grafite_status stm ->
51 (* dump_status grafite_status; *)
52 let stm = pp_ast_statement stm in
53 let stm = Pcre.replace ~rex:slash_n_RE stm in
55 if String.length stm > 50 then
56 String.sub stm 0 50 ^ " ..."
60 HLog.debug ("Executing: ``" ^ stm ^ "''"))
63 let lexicon_status'', grafite_status'' =
64 eval_function lexicon_status' grafite_status' is cb
66 lexicon_status := Some lexicon_status'';
67 grafite_status := Some grafite_status''
71 | CicNotationParser.Parse_error _ as exn -> raise exn
73 HLog.error (snd (MatitaExcPp.to_string exn));
77 match Helm_registry.get_list Helm_registry.string "matita.args" with
79 | _ -> MatitaInit.die_usage ()
81 let pp_ocaml_mode () =
83 HLog.message " ** Entering Ocaml mode ** ";
85 HLog.message "Type 'go ();;' to enter an interactive matitac";
94 match !grafite_status with
96 | Some grafite_status ->
98 let baseuri = GrafiteTypes.get_string_option grafite_status "baseuri" in
99 let basedir = Helm_registry.get "matita.basedir" in
100 LibraryClean.clean_baseuris ~basedir ~verbose:false [baseuri];
102 with GrafiteTypes.Option_error("baseuri", "not found") ->
103 (* no baseuri ==> nothing to clean yet *)
106 let rec interactive_loop () =
107 let str = Ulexing.from_utf8_channel stdin in
110 (MatitaEngine.eval_from_stream ~prompt:true
111 ~include_paths:(Helm_registry.get_list Helm_registry.string
114 | GrafiteEngine.Drop -> pp_ocaml_mode ()
115 | Sys.Break -> HLog.error "user break!"; interactive_loop ()
116 | GrafiteTypes.Command_error _ -> interactive_loop ()
120 | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
121 let (x, y) = HExtlib.loc_of_floc floc in
122 HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
124 | exn -> HLog.error (Printexc.to_string exn); interactive_loop ()
127 Helm_registry.load_from BuildTimeConf.matita_conf;
129 MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
130 LibraryDb.create_owner_environment ();
131 CicEnvironment.set_trust (* environment trust *)
132 (let trust = Helm_registry.get_bool "matita.environment_trust" in
135 Helm_registry.get_list Helm_registry.string "matita.includes" in
136 grafite_status := Some (GrafiteSync.init ());
138 Some (CicNotation2.load_notation ~include_paths
139 BuildTimeConf.core_notation_script);
140 Sys.catch_break true;
144 MatitaInit.initialize_all ();
145 (* must be called after init since args are set by cmdline parsing *)
146 let fname = fname () 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;
154 let origcb = HLog.get_log_callback () in
157 | `Debug | `Message -> ()
158 | `Warning | `Error -> origcb tag s
160 if Helm_registry.get_bool "matita.quiet" then
161 HLog.set_log_callback newcb;
162 let matita_debug = Helm_registry.get_bool "matita.debug" in
164 let time = Unix.time () in
165 if Helm_registry.get_bool "matita.quiet" then
166 origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
168 HLog.message (sprintf "execution of %s started:" fname);
170 Ulexing.from_utf8_channel
173 | fname -> open_in fname) in
175 Helm_registry.get_list Helm_registry.string "matita.includes" in
178 (MatitaEngine.eval_from_stream ~include_paths
179 ~clean_baseuri:(not (Helm_registry.get_bool "matita.preserve")))
180 with End_of_file -> ());
181 let elapsed = Unix.time () -. time in
182 let tm = Unix.gmtime elapsed in
184 if tm.Unix.tm_sec > 0 then (string_of_int tm.Unix.tm_sec ^ "''") else ""
187 if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min ^ "' ") else ""
190 if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else ""
192 let proof_status,moo_content_rev,metadata,lexicon_content_rev =
193 match !lexicon_status,!grafite_status with
195 s.proof_status, s.moo_content_rev, ss.LexiconEngine.metadata,
196 ss.LexiconEngine.lexicon_content_rev
197 | _,_ -> assert false
199 if proof_status <> GrafiteTypes.No_proof then
202 "there are still incomplete proofs at the end of the script";
207 let basedir = Helm_registry.get "matita.basedir" in
209 DependenciesParser.baseuri_of_script ~include_paths fname in
210 let moo_fname = LibraryMisc.obj_file_of_baseuri ~basedir ~baseuri in
211 let lexicon_fname= LibraryMisc.lexicon_file_of_baseuri ~basedir ~baseuri in
213 LibraryMisc.metadata_file_of_baseuri ~basedir ~baseuri
215 GrafiteMarshal.save_moo moo_fname moo_content_rev;
216 LibraryNoDb.save_metadata metadata_fname metadata;
217 LexiconMarshal.save_lexicon lexicon_fname lexicon_content_rev;
219 (sprintf "execution of %s completed in %s." fname (hou^min^sec));
224 HLog.error "user break!";
225 if mode = `COMPILER then
226 clean_exit (Some ~-1)
229 | GrafiteEngine.Drop ->
230 if mode = `COMPILER then
234 | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
235 let (x, y) = HExtlib.loc_of_floc floc in
236 HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
237 if mode = `COMPILER then
242 if matita_debug then raise exn;
243 if mode = `COMPILER then