]> matita.cs.unibo.it Git - helm.git/blob - matita/matitacLib.ml
snapshot for release
[helm.git] / matita / matitacLib.ml
1 (* Copyright (C) 2004-2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (* $Id$ *)
27
28 open Printf
29
30 open GrafiteTypes
31
32 exception AttemptToInsertAnAlias
33
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
37
38 (** {2 Initialization} *)
39
40 let grafite_status = (ref None : GrafiteTypes.status option ref)
41 let lexicon_status = (ref None : LexiconEngine.status option ref)
42
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
47     | _,_ -> assert false
48   in
49   let slash_n_RE = Pcre.regexp "\\n" in
50   let cb = 
51     if Helm_registry.get_int "matita.verbosity" < 1 then 
52       (fun _ _ -> ())
53     else 
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
58         let stm =
59           if String.length stm > 50 then
60             String.sub stm 0 50 ^ " ..."
61           else
62             stm
63         in
64         HLog.debug ("Executing: ``" ^ stm ^ "''"))
65   in
66   try
67    let grafite_status'', lexicon_status'' =
68     match eval_function lexicon_status' grafite_status' is cb with
69        [] -> assert false
70      | (s,None)::_ -> s
71      | (s,Some _)::_ -> raise AttemptToInsertAnAlias
72    in
73     lexicon_status := Some lexicon_status'';
74     grafite_status := Some grafite_status''
75   with
76   | GrafiteEngine.Drop  
77   | End_of_file
78   | CicNotationParser.Parse_error _ as exn -> raise exn
79   | exn -> 
80       HLog.error (snd (MatitaExcPp.to_string exn));
81       raise exn
82
83 let fname () =
84   match Helm_registry.get_list Helm_registry.string "matita.args" with
85   | [x] -> x
86   | _ -> MatitaInit.die_usage ()
87
88 let pp_ocaml_mode () = 
89   HLog.message "";
90   HLog.message "                      ** Entering Ocaml mode ** ";
91   HLog.message "";
92   HLog.message "Type 'go ();;' to enter an interactive matitac";
93   HLog.message ""
94   
95 let clean_exit n =
96  let opt_exit =
97   function
98      None -> ()
99    | Some n -> exit n
100  in
101   match !grafite_status with
102      None -> opt_exit n
103    | Some grafite_status ->
104       try
105        let baseuri = GrafiteTypes.get_string_option grafite_status "baseuri" in
106        LibraryClean.clean_baseuris ~verbose:false [baseuri];
107        opt_exit n
108       with GrafiteTypes.Option_error("baseuri", "not found") ->
109        (* no baseuri ==> nothing to clean yet *)
110        opt_exit n
111   
112 let rec interactive_loop () = 
113   let str = Ulexing.from_utf8_channel stdin in
114   try
115     run_script str 
116       (MatitaEngine.eval_from_stream ~first_statement_only:false ~prompt:true
117       ~include_paths:(Helm_registry.get_list Helm_registry.string
118         "matita.includes"))
119   with 
120   | GrafiteEngine.Drop -> pp_ocaml_mode ()
121   | GrafiteEngine.Macro (floc,_) ->
122      let x, y = HExtlib.loc_of_floc floc in
123       HLog.error
124        (sprintf "A macro has been found in a script at %d-%d" x y);
125       interactive_loop ()
126   | Sys.Break -> HLog.error "user break!"; interactive_loop ()
127   | GrafiteTypes.Command_error _ -> interactive_loop ()
128   | End_of_file ->
129      print_newline ();
130      clean_exit (Some 0)
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);
134       interactive_loop ()
135   | exn -> HLog.error (Printexc.to_string exn); interactive_loop ()
136
137 let go () =
138   Helm_registry.load_from BuildTimeConf.matita_conf;
139   Http_getter.init ();
140   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
141   LibraryDb.create_owner_environment ();
142   CicEnvironment.set_trust (* environment trust *)
143     (let trust =
144       Helm_registry.get_opt_default Helm_registry.get_bool
145         ~default:true "matita.environment_trust" in
146      fun _ -> trust);
147   let include_paths =
148    Helm_registry.get_list Helm_registry.string "matita.includes" in
149   grafite_status := Some (GrafiteSync.init ());
150   lexicon_status :=
151    Some (CicNotation2.load_notation ~include_paths
152     BuildTimeConf.core_notation_script);
153   Sys.catch_break true;
154   interactive_loop ()
155
156 let pp_times fname bench_mode rc big_bang = 
157   if bench_mode then
158     begin
159       let { Unix.tms_utime = u ; Unix.tms_stime = s} = Unix.times () in
160       let r = Unix.gettimeofday () -. big_bang in
161       let extra = try Sys.getenv "DO_TESTS_EXTRA" with Not_found -> "" in
162       let cc = 
163         if Str.string_match (Str.regexp "opt$") Sys.argv.(0) 0 then 
164           "matitac.opt" 
165         else 
166           "matitac" 
167       in
168       let rc = if rc then "OK" else "FAIL" in
169       let times = 
170         let fmt t = 
171           let seconds = int_of_float t in
172           let cents = int_of_float ((t -. floor t) *. 100.0) in
173           let minutes = seconds / 60 in
174           let seconds = seconds mod 60 in
175           Printf.sprintf "%2dm%02d.%02ds" minutes seconds cents
176         in
177         Printf.sprintf "%s %s %s" (fmt r) (fmt u) (fmt s)
178       in
179       let s = 
180         Printf.sprintf "%s\t%-30s   %s\t%s\t%s" cc fname rc times extra 
181       in
182       print_endline s;
183       flush stdout
184     end
185 ;;
186
187 let main ~mode = 
188   let big_bang = Unix.gettimeofday () in
189   MatitaInit.initialize_all ();
190   (* must be called after init since args are set by cmdline parsing *)
191   let fname = fname () in
192   let system_mode =  Helm_registry.get_bool "matita.system" in
193   let bench_mode =  Helm_registry.get_bool "matita.bench" in
194   if bench_mode then
195     Helm_registry.set_int "matita.verbosity" 0;
196   let include_paths =
197    Helm_registry.get_list Helm_registry.string "matita.includes" in
198   grafite_status := Some (GrafiteSync.init ());
199   lexicon_status :=
200    Some (CicNotation2.load_notation ~include_paths
201     BuildTimeConf.core_notation_script);
202   Sys.catch_break true;
203   let origcb = HLog.get_log_callback () in
204   let origcb t s = origcb t ((if system_mode then "[S] " else "") ^ s) in
205   let newcb tag s =
206     match tag with
207     | `Debug | `Message -> ()
208     | `Warning | `Error -> origcb tag s
209   in
210   if Helm_registry.get_int "matita.verbosity" < 1 then
211     HLog.set_log_callback newcb;
212   if bench_mode then
213     begin
214     HLog.set_log_callback (fun _ _ -> ());
215     let out = open_out "/dev/null" in
216     Unix.dup2 (Unix.descr_of_out_channel out) (Unix.descr_of_out_channel stderr)
217     end;
218   let matita_debug = Helm_registry.get_bool "matita.debug" in
219   try
220     let time = Unix.time () in
221     if Helm_registry.get_int "matita.verbosity" < 1 && not bench_mode then
222       origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
223     else
224       HLog.message (sprintf "execution of %s started:" fname);
225     let is =
226       Ulexing.from_utf8_channel
227         (match fname with
228         | "stdin" -> stdin
229         | fname -> open_in fname) in
230     let include_paths =
231      Helm_registry.get_list Helm_registry.string "matita.includes" in
232     (try
233       run_script is 
234        (MatitaEngine.eval_from_stream ~first_statement_only:false ~include_paths
235          ~clean_baseuri:(not (Helm_registry.get_bool "matita.preserve")))
236      with End_of_file -> ());
237     let elapsed = Unix.time () -. time in
238     let tm = Unix.gmtime elapsed in
239     let sec = string_of_int tm.Unix.tm_sec ^ "''" in
240     let min = 
241       if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min ^ "' ") else "" 
242     in
243     let hou = 
244       if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else ""
245     in
246     let proof_status,moo_content_rev,metadata,lexicon_content_rev = 
247       match !lexicon_status,!grafite_status with
248       | Some ss, Some s ->
249          s.proof_status, s.moo_content_rev, ss.LexiconEngine.metadata,
250           ss.LexiconEngine.lexicon_content_rev
251       | _,_ -> assert false
252     in
253     if proof_status <> GrafiteTypes.No_proof then
254      begin
255       HLog.error
256        "there are still incomplete proofs at the end of the script";
257       pp_times fname bench_mode true big_bang;
258       clean_exit (Some 2)
259      end
260     else
261      begin
262        let baseuri =
263         DependenciesParser.baseuri_of_script ~include_paths fname in
264        let moo_fname = 
265          LibraryMisc.obj_file_of_baseuri ~baseuri ~writable:true 
266        in
267        let lexicon_fname= 
268          LibraryMisc.lexicon_file_of_baseuri ~baseuri ~writable:true 
269        in
270        let metadata_fname =
271         LibraryMisc.metadata_file_of_baseuri ~baseuri ~writable:true
272        in
273        GrafiteMarshal.save_moo moo_fname moo_content_rev;
274        LibraryNoDb.save_metadata metadata_fname metadata;
275        LexiconMarshal.save_lexicon lexicon_fname lexicon_content_rev;
276        HLog.message 
277          (sprintf "execution of %s completed in %s." fname (hou^min^sec));
278        pp_times fname bench_mode true big_bang;
279        exit 0
280      end
281   with 
282   | Sys.Break ->
283       HLog.error "user break!";
284       pp_times fname bench_mode false big_bang;
285       if mode = `COMPILER then
286         clean_exit (Some ~-1)
287       else
288         pp_ocaml_mode ()
289   | GrafiteEngine.Drop ->
290       if mode = `COMPILER then 
291         begin
292           pp_times fname bench_mode false big_bang;
293           clean_exit (Some 1)
294         end
295       else 
296         pp_ocaml_mode ()
297   | GrafiteEngine.Macro (floc,_) ->
298      let x, y = HExtlib.loc_of_floc floc in
299       HLog.error
300        (sprintf "A macro has been found in a script at %d-%d" x y);
301       if mode = `COMPILER then 
302         begin
303           pp_times fname bench_mode false big_bang;
304           clean_exit (Some 1)
305         end
306       else 
307         pp_ocaml_mode ()
308   | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
309      let (x, y) = HExtlib.loc_of_floc floc in
310      HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
311      if mode = `COMPILER then
312        begin
313          pp_times fname bench_mode false big_bang;
314          clean_exit (Some 1)
315        end
316      else
317        pp_ocaml_mode ()
318   | exn ->
319       if matita_debug then raise exn;
320       if mode = `COMPILER then 
321         begin
322           pp_times fname bench_mode false big_bang;
323           clean_exit (Some 3)
324         end
325       else 
326         pp_ocaml_mode ()
327