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