]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitacLib.ml
Big commit to let Ferruccio try the merge_coercion patch.
[helm.git] / helm / 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 open Printf
27
28 open GrafiteTypes
29
30 (** {2 Initialization} *)
31
32 let status = ref None
33
34 let run_script is eval_function  =
35   let status = 
36     match !status with
37     | None -> assert false
38     | Some s -> s
39   in
40   let slash_n_RE = Pcre.regexp "\\n" in
41   let cb = 
42     if Helm_registry.get_bool "matita.quiet" then 
43       (fun _ _ -> ())
44     else 
45       (fun status stm ->
46         (* dump_status status; *)
47         let stm = GrafiteAstPp.pp_statement stm in
48         let stm = Pcre.replace ~rex:slash_n_RE stm in
49         let stm = 
50           if String.length stm > 50 then
51             String.sub stm 0 50 ^ " ..."
52           else
53             stm
54         in
55         HLog.debug ("Executing: ``" ^ stm ^ "''"))
56   in
57   try
58     eval_function status is cb
59   with
60   | GrafiteEngine.Drop  
61   | End_of_file
62   | CicNotationParser.Parse_error _ as exn -> raise exn
63   | exn -> 
64       HLog.error (snd (MatitaExcPp.to_string exn));
65       raise exn
66
67 let fname () =
68   match Helm_registry.get_list Helm_registry.string "matita.args" with
69   | [x] -> x
70   | _ -> MatitaInit.die_usage ()
71
72 let pp_ocaml_mode () = 
73   HLog.message "";
74   HLog.message "                      ** Entering Ocaml mode ** ";
75   HLog.message "";
76   HLog.message "Type 'go ();;' to enter an interactive matitac";
77   HLog.message ""
78   
79 let clean_exit n =
80  let opt_exit =
81   function
82      None -> ()
83    | Some n -> exit n
84  in
85   match !status with
86      None -> opt_exit n
87    | Some status ->
88       try
89        let baseuri = GrafiteTypes.get_string_option !status "baseuri" in
90        let basedir = Helm_registry.get "matita.basedir" in
91        LibraryClean.clean_baseuris ~basedir ~verbose:false [baseuri];
92        opt_exit n
93       with GrafiteTypes.Option_error("baseuri", "not found") ->
94        (* no baseuri ==> nothing to clean yet *)
95        opt_exit n
96   
97 let rec interactive_loop () = 
98   let str = Ulexing.from_utf8_channel stdin in
99   try
100     run_script str 
101       (MatitaEngine.eval_from_stream ~prompt:true
102       ~include_paths:(Helm_registry.get_list Helm_registry.string
103         "matita.includes"))
104   with 
105   | GrafiteEngine.Drop -> pp_ocaml_mode ()
106   | Sys.Break -> HLog.error "user break!"; interactive_loop ()
107   | GrafiteTypes.Command_error _ -> interactive_loop ()
108   | End_of_file ->
109      print_newline ();
110      clean_exit (Some 0)
111   | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
112      let (x, y) = HExtlib.loc_of_floc floc in
113      HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
114      interactive_loop ()
115   | exn -> HLog.error (Printexc.to_string exn); interactive_loop ()
116
117 let go () =
118   Helm_registry.load_from BuildTimeConf.matita_conf;
119   CicNotation2.load_notation BuildTimeConf.core_notation_script;
120   Http_getter.init ();
121   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
122   LibraryDb.create_owner_environment ();
123   CicEnvironment.set_trust (* environment trust *)
124     (let trust = Helm_registry.get_bool "matita.environment_trust" in
125      fun _ -> trust);
126   status := Some (ref (MatitaSync.init ()));
127   Sys.catch_break true;
128   interactive_loop ()
129
130 let main ~mode = 
131   MatitaInit.initialize_all ();
132   (* must be called after init since args are set by cmdline parsing *)
133   let fname = fname () in
134   status := Some (ref (MatitaSync.init ()));
135   Sys.catch_break true;
136   let origcb = HLog.get_log_callback () in
137   let newcb tag s =
138     match tag with
139     | `Debug | `Message -> ()
140     | `Warning | `Error -> origcb tag s
141   in
142   if Helm_registry.get_bool "matita.quiet" then
143     HLog.set_log_callback newcb;
144   let matita_debug = Helm_registry.get_bool "matita.debug" in
145   try
146     let time = Unix.time () in
147     if Helm_registry.get_bool "matita.quiet" then
148       origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
149     else
150       HLog.message (sprintf "execution of %s started:" fname);
151     let is =
152       Ulexing.from_utf8_channel
153         (match fname with
154         | "stdin" -> stdin
155         | fname -> open_in fname) in
156     let include_paths =
157      Helm_registry.get_list Helm_registry.string "matita.includes" in
158     (try
159       run_script is 
160        (MatitaEngine.eval_from_stream ~include_paths
161          ~clean_baseuri:(not (Helm_registry.get_bool "matita.preserve")))
162      with End_of_file -> ());
163     let elapsed = Unix.time () -. time in
164     let tm = Unix.gmtime elapsed in
165     let sec = 
166       if tm.Unix.tm_sec > 0 then (string_of_int tm.Unix.tm_sec ^ "''") else "" 
167     in
168     let min = 
169       if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min ^ "' ") else "" 
170     in
171     let hou = 
172       if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else ""
173     in
174     let proof_status,moo_content_rev,metadata,status = 
175       match !status with
176       | Some s -> !s.proof_status, !s.moo_content_rev, !s.metadata, !s
177       | None -> assert false
178     in
179     if proof_status <> GrafiteTypes.No_proof then
180      begin
181       HLog.error
182        "there are still incomplete proofs at the end of the script";
183       clean_exit (Some 2)
184      end
185     else
186      begin
187        let basedir = Helm_registry.get "matita.basedir" in
188        let baseuri = GrafiteParserMisc.baseuri_of_script ~include_paths fname in
189        let moo_fname = LibraryMisc.obj_file_of_baseuri ~basedir ~baseuri in
190        let metadata_fname = LibraryMisc.metadata_file_of_baseuri ~basedir ~baseuri in
191        GrafiteMarshal.save_moo moo_fname moo_content_rev;
192        LibraryNoDb.save_metadata metadata_fname metadata;
193        HLog.message 
194          (sprintf "execution of %s completed in %s." fname (hou^min^sec));
195        exit 0
196      end
197   with 
198   | Sys.Break ->
199       HLog.error "user break!";
200       if mode = `COMPILER then
201         clean_exit (Some ~-1)
202       else
203         pp_ocaml_mode ()
204   | GrafiteEngine.Drop ->
205       if mode = `COMPILER then 
206         clean_exit (Some 1)
207       else 
208         pp_ocaml_mode ()
209   | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
210      let (x, y) = HExtlib.loc_of_floc floc in
211      HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
212      if mode = `COMPILER then
213        clean_exit (Some 1)
214      else
215        pp_ocaml_mode ()
216   | exn ->
217       if matita_debug then raise exn;
218       if mode = `COMPILER then 
219         clean_exit (Some 3)
220       else 
221         pp_ocaml_mode ()
222