]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitacLib.ml
Ctrl^D in matitatop fixed (was broken by the new parser of Zack and Luca)
[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 = GrafiteAstPp.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   | End_of_file
77   | CicNotationParser.Parse_error _ as exn -> raise exn
78   | exn -> 
79       MatitaLog.error (MatitaExcPp.to_string exn);
80       raise exn
81
82 let fname () =
83   let acc = ref [] in
84   let add_script fname = acc := fname :: !acc in
85   Arg.parse arg_spec add_script usage;
86   match !acc with
87   | [x] -> x
88   | _ -> prerr_endline usage; exit 1
89
90 let pp_ocaml_mode () = 
91   MatitaLog.message "";
92   MatitaLog.message "                      ** Entering Ocaml mode ** ";
93   MatitaLog.message "";
94   MatitaLog.message "Type 'go ();;' to enter an interactive matitac";
95   MatitaLog.message ""
96   
97 let clean_exit n =
98  let opt_exit =
99   function
100      None -> ()
101    | Some n -> exit n
102  in
103   match !status with
104      None -> opt_exit n
105    | Some status ->
106       try
107        let baseuri = MatitaTypes.get_string_option !status "baseuri" in
108        MatitacleanLib.clean_baseuris ~verbose:false [baseuri];
109        opt_exit n
110       with MatitaTypes.Option_error("baseuri", "not found") ->
111        (* no baseuri ==> nothing to clean yet *)
112        opt_exit n
113   
114 let rec interactive_loop () = 
115  let str = Stream.of_channel stdin in
116   try
117     run_script str 
118       (MatitaEngine.eval_from_stream_greedy ~include_paths:!paths_to_search_in)
119   with 
120   | MatitaEngine.Drop -> pp_ocaml_mode ()
121   | Sys.Break -> MatitaLog.error "user break!"; interactive_loop ()
122   | MatitaTypes.Command_error _ -> interactive_loop ()
123   | End_of_file ->
124      print_newline ();
125      clean_exit (Some 0)
126   | CicNotationParser.Parse_error (floc,err) ->
127      let (x, y) = CicNotationPt.loc_of_floc floc in
128      MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
129      interactive_loop ()
130   | exn -> MatitaLog.error (Printexc.to_string exn); interactive_loop ()
131
132 let go () =
133   Helm_registry.load_from BuildTimeConf.matita_conf;
134   Http_getter.init ();
135   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
136   status := Some (ref (Lazy.force MatitaEngine.initial_status));
137   Sys.catch_break true;
138   interactive_loop ()
139
140 let dump_moo_to_file file moo =
141  let os = open_out (MatitaMisc.obj_file_of_script file) in
142  let output s = output_string os s in
143  output "(* GENERATED FILE: DO NOT EDIT! *)\n\n";
144  List.iter output (List.rev moo);
145  close_out os
146   
147 let main ~mode = 
148   Helm_registry.load_from BuildTimeConf.matita_conf;
149   CicNotation.load_notation BuildTimeConf.core_notation_script;
150   Http_getter.init ();
151   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
152   MatitaDb.create_owner_environment ();
153   status := Some (ref (Lazy.force MatitaEngine.initial_status));
154   Sys.catch_break true;
155   let origcb = MatitaLog.get_log_callback () in
156   let newcb tag s =
157         match tag with
158         | `Debug | `Message -> ()
159         | `Warning | `Error -> origcb tag s
160   in
161   let fname = fname () in
162   if !quiet_compilation then
163     MatitaLog.set_log_callback newcb;
164   try
165     let time = Unix.time () in
166     if !quiet_compilation then
167       origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
168     else
169       MatitaLog.message (sprintf "execution of %s started:" fname);
170     let is =
171       Stream.of_channel
172         (match fname with
173         | "stdin" -> stdin
174         | fname -> open_in fname)
175     in
176     run_script is 
177       (MatitaEngine.eval_from_stream ~include_paths:!paths_to_search_in);
178     let elapsed = Unix.time () -. time in
179     let tm = Unix.gmtime elapsed in
180     let sec = 
181       if tm.Unix.tm_sec > 0 then  (string_of_int tm.Unix.tm_sec ^ "''") else "" 
182     in
183     let min = 
184       if tm.Unix.tm_min > 0 then  (string_of_int tm.Unix.tm_min ^ "' ") else "" 
185     in
186     let hou = 
187       if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else "" 
188     in
189     let proof_status,moo_content_rev = 
190       match !status with
191       | Some s -> !s.proof_status, !s.moo_content_rev
192       | None -> assert false
193     in
194     if proof_status <> MatitaTypes.No_proof then
195      begin
196       MatitaLog.error
197        "there are still incomplete proofs at the end of the script";
198       clean_exit (Some 2)
199      end
200     else
201      begin
202        dump_moo_to_file fname moo_content_rev;
203        MatitaLog.message 
204          (sprintf "execution of %s completed in %s." fname (hou^min^sec));
205        exit 0
206      end
207   with 
208   | Sys.Break ->
209       MatitaLog.error "user break!";
210       if mode = `COMPILER then
211         clean_exit (Some ~-1)
212       else
213         pp_ocaml_mode ()
214   | MatitaEngine.Drop ->
215       if mode = `COMPILER then 
216         clean_exit (Some 1)
217       else 
218         pp_ocaml_mode ()
219   | CicNotationParser.Parse_error (floc,err) ->
220      let (x, y) = CicNotationPt.loc_of_floc floc in
221      MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
222      if mode = `COMPILER then
223        clean_exit (Some 1)
224      else
225        pp_ocaml_mode ()
226   | _ ->
227      if mode = `COMPILER then 
228        clean_exit (Some 3)
229      else 
230        pp_ocaml_mode ()