]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitacLib.ml
merged cic_notation with matita: good luck!
[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   | CicNotationParser.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   | CicNotationParser.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) = 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   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   CicNotation.load_notation BuildTimeConf.core_notation_script;
153   Http_getter.init ();
154   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
155   MatitaDb.create_owner_environment ();
156   status := Some (ref (Lazy.force MatitaEngine.initial_status));
157   Sys.catch_break true;
158   let origcb = MatitaLog.get_log_callback () in
159   let newcb tag s =
160         match tag with
161         | `Debug | `Message -> ()
162         | `Warning | `Error -> origcb tag s
163   in
164   let fname = fname () in
165   if !quiet_compilation then
166     MatitaLog.set_log_callback newcb;
167   try
168     let time = Unix.time () in
169     if !quiet_compilation then
170       origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
171     else
172       MatitaLog.message (sprintf "execution of %s started:" fname);
173     let is =
174       Stream.of_channel
175         (match fname with
176         | "stdin" -> stdin
177         | fname -> open_in fname)
178     in
179     run_script is 
180       (MatitaEngine.eval_from_stream ~include_paths:!paths_to_search_in);
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 = 
193       match !status with
194       | Some s -> !s.proof_status, !s.moo_content_rev
195       | None -> assert false
196     in
197     if proof_status <> MatitaTypes.No_proof then
198      begin
199       MatitaLog.error
200        "there are still incomplete proofs at the end of the script";
201       clean_exit (Some 2)
202      end
203     else
204      begin
205        dump_moo_to_file fname moo_content_rev;
206        MatitaLog.message 
207          (sprintf "execution of %s completed in %s." fname (hou^min^sec));
208        exit 0
209      end
210   with 
211   | Sys.Break ->
212       MatitaLog.error "user break!";
213       if mode = `COMPILER then
214         clean_exit (Some ~-1)
215       else
216         pp_ocaml_mode ()
217   | MatitaEngine.Drop ->
218       if mode = `COMPILER then 
219         clean_exit (Some 1)
220       else 
221         pp_ocaml_mode ()
222   | CicNotationParser.Parse_error (floc,err) ->
223      let (x, y) = CicNotationPt.loc_of_floc floc in
224      MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
225      if mode = `COMPILER then
226        clean_exit (Some 1)
227      else
228        pp_ocaml_mode ()
229   | _ ->
230      if mode = `COMPILER then 
231        clean_exit (Some 3)
232      else 
233        pp_ocaml_mode ()