]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitacLib.ml
- changed moo representation in MatitaTypes.status, no longer a string list, but...
[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 MatitaTypes
29
30 (** {2 Initialization} *)
31
32 let paths_to_search_in = ref [];;
33 let quiet_compilation = ref false;;
34 let dont_delete_baseuri = ref false;;
35
36 let add_l l = fun s -> l := s :: !l ;;
37 let set_b b = fun () -> b := true;;
38
39 let arg_spec = [
40   "-I", Arg.String (add_l paths_to_search_in), 
41     "<path> Adds path to the list of searched paths for the include command";
42   "-q", Arg.Unit (set_b quiet_compilation), "Turn off verbose compilation";
43   "-preserve", Arg.Unit (set_b dont_delete_baseuri),
44      "Turns off automatic baseuri cleaning"
45 ]
46 let usage =
47   sprintf "MatitaC v%s\nUsage: matitac [option ...] file\nOptions:"
48     BuildTimeConf.version
49
50 let status = ref None
51
52 let run_script is eval_function  =
53   let status = 
54     match !status with
55     | None -> assert false
56     | Some s -> s
57   in
58   let slash_n_RE = Pcre.regexp "\\n" in
59   let cb = 
60     if !quiet_compilation then 
61       fun _ _ -> () 
62     else 
63       fun status stm ->
64         (* dump_status status; *)
65         let stm = GrafiteAstPp.pp_statement stm in
66         let stm = Pcre.replace ~rex:slash_n_RE stm in
67         let stm = 
68           if String.length stm > 50 then
69             String.sub stm 0 50 ^ " ..."
70           else
71             stm
72         in
73         MatitaLog.debug ("Executing: ``" ^ stm ^ "''")
74   in
75   try
76     eval_function status is cb
77   with
78   | MatitaEngine.Drop  
79   | End_of_file
80   | CicNotationParser.Parse_error _ as exn -> raise exn
81   | exn -> 
82       MatitaLog.error (MatitaExcPp.to_string exn);
83       raise exn
84
85 let fname () =
86   let acc = ref [] in
87   let add_script fname = acc := fname :: !acc in
88   Arg.parse arg_spec add_script usage;
89   match !acc with
90   | [x] -> x
91   | _ -> print_endline usage; exit 1
92
93 let pp_ocaml_mode () = 
94   MatitaLog.message "";
95   MatitaLog.message "                      ** Entering Ocaml mode ** ";
96   MatitaLog.message "";
97   MatitaLog.message "Type 'go ();;' to enter an interactive matitac";
98   MatitaLog.message ""
99   
100 let clean_exit n =
101  let opt_exit =
102   function
103      None -> ()
104    | Some n -> exit n
105  in
106   match !status with
107      None -> opt_exit n
108    | Some status ->
109       try
110        let baseuri = MatitaTypes.get_string_option !status "baseuri" in
111        MatitacleanLib.clean_baseuris ~verbose:false [baseuri];
112        opt_exit n
113       with MatitaTypes.Option_error("baseuri", "not found") ->
114        (* no baseuri ==> nothing to clean yet *)
115        opt_exit n
116   
117 let rec interactive_loop () = 
118  let str = Stream.of_channel stdin in
119   try
120     run_script str 
121       (MatitaEngine.eval_from_stream_greedy ~include_paths:!paths_to_search_in)
122   with 
123   | MatitaEngine.Drop -> pp_ocaml_mode ()
124   | Sys.Break -> MatitaLog.error "user break!"; interactive_loop ()
125   | MatitaTypes.Command_error _ -> interactive_loop ()
126   | End_of_file ->
127      print_newline ();
128      clean_exit (Some 0)
129   | CicNotationParser.Parse_error (floc,err) ->
130      let (x, y) = CicNotationPt.loc_of_floc floc in
131      MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
132      interactive_loop ()
133   | exn -> MatitaLog.error (Printexc.to_string exn); interactive_loop ()
134
135 let go () =
136   Helm_registry.load_from BuildTimeConf.matita_conf;
137   CicNotation.load_notation BuildTimeConf.core_notation_script;
138   Http_getter.init ();
139   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
140   MatitaDb.create_owner_environment ();
141   CicEnvironment.set_trust (* environment trust *)
142     (let trust = Helm_registry.get_bool "matita.environment_trust" in
143      fun _ -> trust);
144   status := Some (ref (Lazy.force MatitaEngine.initial_status));
145   Sys.catch_break true;
146   interactive_loop ()
147
148 let dump_moo_to_file file moo =
149  let os = open_out (MatitaMisc.obj_file_of_script file) in
150  let output s = output_string os s in
151  output "(* GENERATED FILE: DO NOT EDIT! *)\n\n";
152  List.iter
153   (fun cmd -> output (GrafiteAstPp.pp_command cmd ^ "\n"))
154   (List.rev moo);
155  close_out os
156   
157 let main ~mode = 
158   Helm_registry.load_from BuildTimeConf.matita_conf;
159   CicNotation.load_notation BuildTimeConf.core_notation_script;
160   Http_getter.init ();
161   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
162   MatitaDb.create_owner_environment ();
163   CicEnvironment.set_trust (* environment trust *)
164     (let trust = Helm_registry.get_bool "matita.environment_trust" in
165      fun _ -> trust);
166   status := Some (ref (Lazy.force MatitaEngine.initial_status));
167   Sys.catch_break true;
168   let origcb = MatitaLog.get_log_callback () in
169   let newcb tag s =
170         match tag with
171         | `Debug | `Message -> ()
172         | `Warning | `Error -> origcb tag s
173   in
174   let fname = fname () in
175   if !quiet_compilation then
176     MatitaLog.set_log_callback newcb;
177   try
178     let time = Unix.time () in
179     if !quiet_compilation then
180       origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
181     else
182       MatitaLog.message (sprintf "execution of %s started:" fname);
183     let is =
184       Stream.of_channel
185         (match fname with
186         | "stdin" -> stdin
187         | fname -> open_in fname)
188     in
189     run_script is 
190       (MatitaEngine.eval_from_stream 
191         ~include_paths:!paths_to_search_in
192         ~clean_baseuri:(not !dont_delete_baseuri));
193     let elapsed = Unix.time () -. time in
194     let tm = Unix.gmtime elapsed in
195     let sec = 
196       if tm.Unix.tm_sec > 0 then  (string_of_int tm.Unix.tm_sec ^ "''") else "" 
197     in
198     let min = 
199       if tm.Unix.tm_min > 0 then  (string_of_int tm.Unix.tm_min ^ "' ") else "" 
200     in
201     let hou = 
202       if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else "" 
203     in
204     let proof_status,moo_content_rev = 
205       match !status with
206       | Some s -> !s.proof_status, !s.moo_content_rev
207       | None -> assert false
208     in
209     if proof_status <> MatitaTypes.No_proof then
210      begin
211       MatitaLog.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        dump_moo_to_file fname moo_content_rev;
218        MatitaLog.message 
219          (sprintf "execution of %s completed in %s." fname (hou^min^sec));
220        exit 0
221      end
222   with 
223   | Sys.Break ->
224       MatitaLog.error "user break!";
225       if mode = `COMPILER then
226         clean_exit (Some ~-1)
227       else
228         pp_ocaml_mode ()
229   | MatitaEngine.Drop ->
230       if mode = `COMPILER then 
231         clean_exit (Some 1)
232       else 
233         pp_ocaml_mode ()
234   | CicNotationParser.Parse_error (floc,err) ->
235      let (x, y) = CicNotationPt.loc_of_floc floc in
236      MatitaLog.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   | _ ->
242      if mode = `COMPILER then 
243        clean_exit (Some 3)
244      else 
245        pp_ocaml_mode ()