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