]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitacLib.ml
test branch
[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 (* $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_bool "matita.quiet" 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        let basedir = Helm_registry.get "matita.basedir" in
107        LibraryClean.clean_baseuris ~basedir ~verbose:false [baseuri];
108        opt_exit n
109       with GrafiteTypes.Option_error("baseuri", "not found") ->
110        (* no baseuri ==> nothing to clean yet *)
111        opt_exit n
112   
113 let rec interactive_loop () = 
114   let str = Ulexing.from_utf8_channel stdin in
115   try
116     run_script str 
117       (MatitaEngine.eval_from_stream ~first_statement_only:false ~prompt:true
118       ~include_paths:(Helm_registry.get_list Helm_registry.string
119         "matita.includes"))
120   with 
121   | GrafiteEngine.Drop -> pp_ocaml_mode ()
122   | GrafiteEngine.Macro (floc,_) ->
123      let x, y = HExtlib.loc_of_floc floc in
124       HLog.error
125        (sprintf "A macro has been found in a script at %d-%d" x y);
126       interactive_loop ()
127   | Sys.Break -> HLog.error "user break!"; interactive_loop ()
128   | GrafiteTypes.Command_error _ -> interactive_loop ()
129   | End_of_file ->
130      print_newline ();
131      clean_exit (Some 0)
132   | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
133      let x, y = HExtlib.loc_of_floc floc in
134       HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
135       interactive_loop ()
136   | exn -> HLog.error (Printexc.to_string exn); interactive_loop ()
137
138 let go () =
139   Helm_registry.load_from BuildTimeConf.matita_conf;
140   Http_getter.init ();
141   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
142   LibraryDb.create_owner_environment ();
143   CicEnvironment.set_trust (* environment trust *)
144     (let trust = Helm_registry.get_bool "matita.environment_trust" in
145      fun _ -> trust);
146   let include_paths =
147    Helm_registry.get_list Helm_registry.string "matita.includes" in
148   grafite_status := Some (GrafiteSync.init ());
149   lexicon_status :=
150    Some (CicNotation2.load_notation ~include_paths
151     BuildTimeConf.core_notation_script);
152   Sys.catch_break true;
153   interactive_loop ()
154
155 let main ~mode = 
156   MatitaInit.initialize_all ();
157   (* must be called after init since args are set by cmdline parsing *)
158   let fname = fname () in
159   let include_paths =
160    Helm_registry.get_list Helm_registry.string "matita.includes" in
161   grafite_status := Some (GrafiteSync.init ());
162   lexicon_status :=
163    Some (CicNotation2.load_notation ~include_paths
164     BuildTimeConf.core_notation_script);
165   Sys.catch_break true;
166   let origcb = HLog.get_log_callback () in
167   let newcb tag s =
168     match tag with
169     | `Debug | `Message -> ()
170     | `Warning | `Error -> origcb tag s
171   in
172   if Helm_registry.get_bool "matita.quiet" then
173     HLog.set_log_callback newcb;
174   let matita_debug = Helm_registry.get_bool "matita.debug" in
175   try
176     let time = Unix.time () in
177     if Helm_registry.get_bool "matita.quiet" then
178       origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
179     else
180       HLog.message (sprintf "execution of %s started:" fname);
181     let is =
182       Ulexing.from_utf8_channel
183         (match fname with
184         | "stdin" -> stdin
185         | fname -> open_in fname) in
186     let include_paths =
187      Helm_registry.get_list Helm_registry.string "matita.includes" in
188     (try
189       run_script is 
190        (MatitaEngine.eval_from_stream ~first_statement_only:false ~include_paths
191          ~clean_baseuri:(not (Helm_registry.get_bool "matita.preserve")))
192      with End_of_file -> ());
193     let elapsed = Unix.time () -. time in
194     let tm = Unix.gmtime elapsed in
195     let sec = string_of_int tm.Unix.tm_sec ^ "''" in
196     let min = 
197       if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min ^ "' ") else "" 
198     in
199     let hou = 
200       if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else ""
201     in
202     let proof_status,moo_content_rev,metadata,lexicon_content_rev = 
203       match !lexicon_status,!grafite_status with
204       | Some ss, Some s ->
205          s.proof_status, s.moo_content_rev, ss.LexiconEngine.metadata,
206           ss.LexiconEngine.lexicon_content_rev
207       | _,_ -> assert false
208     in
209     if proof_status <> GrafiteTypes.No_proof then
210      begin
211       HLog.error
212        "there are still incomplete proofs at the end of the script";
213       clean_exit (Some 2)
214      end
215     else
216      begin
217        let basedir = Helm_registry.get "matita.basedir" in
218        let baseuri =
219         DependenciesParser.baseuri_of_script ~include_paths fname in
220        let moo_fname = LibraryMisc.obj_file_of_baseuri ~basedir ~baseuri in
221        let lexicon_fname= LibraryMisc.lexicon_file_of_baseuri ~basedir ~baseuri in
222        let metadata_fname =
223         LibraryMisc.metadata_file_of_baseuri ~basedir ~baseuri
224        in
225        GrafiteMarshal.save_moo moo_fname moo_content_rev;
226        LibraryNoDb.save_metadata metadata_fname metadata;
227        LexiconMarshal.save_lexicon lexicon_fname lexicon_content_rev;
228        HLog.message 
229          (sprintf "execution of %s completed in %s." fname (hou^min^sec));
230        exit 0
231      end
232   with 
233   | Sys.Break ->
234       HLog.error "user break!";
235       if mode = `COMPILER then
236         clean_exit (Some ~-1)
237       else
238         pp_ocaml_mode ()
239   | GrafiteEngine.Drop ->
240       if mode = `COMPILER then 
241         clean_exit (Some 1)
242       else 
243         pp_ocaml_mode ()
244   | GrafiteEngine.Macro (floc,_) ->
245      let x, y = HExtlib.loc_of_floc floc in
246       HLog.error
247        (sprintf "A macro has been found in a script at %d-%d" x y);
248       if mode = `COMPILER then 
249         clean_exit (Some 1)
250       else 
251         pp_ocaml_mode ()
252   | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
253      let (x, y) = HExtlib.loc_of_floc floc in
254      HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
255      if mode = `COMPILER then
256        clean_exit (Some 1)
257      else
258        pp_ocaml_mode ()
259   | exn ->
260       if matita_debug then raise exn;
261       if mode = `COMPILER then 
262         clean_exit (Some 3)
263       else 
264         pp_ocaml_mode ()
265