]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitacLib.ml
Huge reorganization of matita and ocaml.
[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 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
33
34 (** {2 Initialization} *)
35
36 let grafite_status = (ref None : GrafiteTypes.status option ref)
37 let lexicon_status = (ref None : LexiconEngine.status option ref)
38
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
43     | _,_ -> assert false
44   in
45   let slash_n_RE = Pcre.regexp "\\n" in
46   let cb = 
47     if Helm_registry.get_bool "matita.quiet" then 
48       (fun _ _ -> ())
49     else 
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
54         let stm =
55           if String.length stm > 50 then
56             String.sub stm 0 50 ^ " ..."
57           else
58             stm
59         in
60         HLog.debug ("Executing: ``" ^ stm ^ "''"))
61   in
62   try
63    let lexicon_status'', grafite_status'' =
64     eval_function lexicon_status' grafite_status' is cb
65    in
66     lexicon_status := Some lexicon_status'';
67     grafite_status := Some grafite_status''
68   with
69   | GrafiteEngine.Drop  
70   | End_of_file
71   | CicNotationParser.Parse_error _ as exn -> raise exn
72   | exn -> 
73       HLog.error (snd (MatitaExcPp.to_string exn));
74       raise exn
75
76 let fname () =
77   match Helm_registry.get_list Helm_registry.string "matita.args" with
78   | [x] -> x
79   | _ -> MatitaInit.die_usage ()
80
81 let pp_ocaml_mode () = 
82   HLog.message "";
83   HLog.message "                      ** Entering Ocaml mode ** ";
84   HLog.message "";
85   HLog.message "Type 'go ();;' to enter an interactive matitac";
86   HLog.message ""
87   
88 let clean_exit n =
89  let opt_exit =
90   function
91      None -> ()
92    | Some n -> exit n
93  in
94   match !grafite_status with
95      None -> opt_exit n
96    | Some grafite_status ->
97       try
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];
101        opt_exit n
102       with GrafiteTypes.Option_error("baseuri", "not found") ->
103        (* no baseuri ==> nothing to clean yet *)
104        opt_exit n
105   
106 let rec interactive_loop () = 
107   let str = Ulexing.from_utf8_channel stdin in
108   try
109     run_script str 
110       (MatitaEngine.eval_from_stream ~prompt:true
111       ~include_paths:(Helm_registry.get_list Helm_registry.string
112         "matita.includes"))
113   with 
114   | GrafiteEngine.Drop -> pp_ocaml_mode ()
115   | Sys.Break -> HLog.error "user break!"; interactive_loop ()
116   | GrafiteTypes.Command_error _ -> interactive_loop ()
117   | End_of_file ->
118      print_newline ();
119      clean_exit (Some 0)
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);
123      interactive_loop ()
124   | exn -> HLog.error (Printexc.to_string exn); interactive_loop ()
125
126 let go () =
127   Helm_registry.load_from BuildTimeConf.matita_conf;
128   Http_getter.init ();
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
133      fun _ -> trust);
134   let include_paths =
135    Helm_registry.get_list Helm_registry.string "matita.includes" in
136   grafite_status := Some (GrafiteSync.init ());
137   lexicon_status :=
138    Some (CicNotation2.load_notation ~include_paths
139     BuildTimeConf.core_notation_script);
140   Sys.catch_break true;
141   interactive_loop ()
142
143 let main ~mode = 
144   MatitaInit.initialize_all ();
145   (* must be called after init since args are set by cmdline parsing *)
146   let fname = fname () in
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   let origcb = HLog.get_log_callback () in
155   let newcb tag s =
156     match tag with
157     | `Debug | `Message -> ()
158     | `Warning | `Error -> origcb tag s
159   in
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
163   try
164     let time = Unix.time () in
165     if Helm_registry.get_bool "matita.quiet" then
166       origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
167     else
168       HLog.message (sprintf "execution of %s started:" fname);
169     let is =
170       Ulexing.from_utf8_channel
171         (match fname with
172         | "stdin" -> stdin
173         | fname -> open_in fname) in
174     let include_paths =
175      Helm_registry.get_list Helm_registry.string "matita.includes" in
176     (try
177       run_script is 
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
183     let sec = 
184       if tm.Unix.tm_sec > 0 then (string_of_int tm.Unix.tm_sec ^ "''") else "" 
185     in
186     let min = 
187       if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min ^ "' ") else "" 
188     in
189     let hou = 
190       if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else ""
191     in
192     let proof_status,moo_content_rev,metadata,lexicon_content_rev = 
193       match !lexicon_status,!grafite_status with
194       | Some ss, Some s ->
195          s.proof_status, s.moo_content_rev, ss.LexiconEngine.metadata,
196           ss.LexiconEngine.lexicon_content_rev
197       | _,_ -> assert false
198     in
199     if proof_status <> GrafiteTypes.No_proof then
200      begin
201       HLog.error
202        "there are still incomplete proofs at the end of the script";
203       clean_exit (Some 2)
204      end
205     else
206      begin
207        let basedir = Helm_registry.get "matita.basedir" in
208        let baseuri =
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
212        let metadata_fname =
213         LibraryMisc.metadata_file_of_baseuri ~basedir ~baseuri
214        in
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;
218        HLog.message 
219          (sprintf "execution of %s completed in %s." fname (hou^min^sec));
220        exit 0
221      end
222   with 
223   | Sys.Break ->
224       HLog.error "user break!";
225       if mode = `COMPILER then
226         clean_exit (Some ~-1)
227       else
228         pp_ocaml_mode ()
229   | GrafiteEngine.Drop ->
230       if mode = `COMPILER then 
231         clean_exit (Some 1)
232       else 
233         pp_ocaml_mode ()
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
238        clean_exit (Some 1)
239      else
240        pp_ocaml_mode ()
241   | exn ->
242       if matita_debug then raise exn;
243       if mode = `COMPILER then 
244         clean_exit (Some 3)
245       else 
246         pp_ocaml_mode ()
247