]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitacLib.ml
function to domp moo_content is now exported
[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 arg_spec = [
33 (*   "-opt", Arg...., "set bla bla bla"; *)
34 ]
35 let usage =
36   sprintf "MatitaC v%s\nUsage: matitac [option ...] file\nOptions:"
37     BuildTimeConf.version
38
39 let status = ref None
40
41 let run_script is eval_function  =
42   let status = 
43     match !status with
44     | None -> assert false
45     | Some s -> s
46   in
47   let slash_n_RE = Pcre.regexp "\\n" in
48   let cb status stm = 
49     (* dump_status status; *)
50     let stm = TacticAstPp.pp_statement stm in
51     let stm = Pcre.replace ~rex:slash_n_RE stm in
52     let stm = 
53       if String.length stm > 50 then
54         String.sub stm 0 50 ^ " ..."
55       else
56         stm
57     in
58     MatitaLog.debug ("Executing: ``" ^ stm ^ "''")
59   in
60   try
61     eval_function status is cb
62   with
63   | MatitaEngine.Drop  
64   | CicTextualParser2.Parse_error _ as exn -> raise exn
65   | exn -> 
66       MatitaLog.error (MatitaExcPp.to_string exn);
67       raise exn
68
69 let fname () =
70   let acc = ref [] in
71   let add_script fname = acc := fname :: !acc in
72   Arg.parse arg_spec add_script usage;
73   match !acc with
74   | [x] -> x
75   | _ -> prerr_endline usage; exit 1
76
77 let pp_ocaml_mode () = 
78   MatitaLog.message "";
79   MatitaLog.message "                      ** Entering Ocaml mode ** ";
80   MatitaLog.message "";
81   MatitaLog.message "Type 'go ();;' to enter an interactive matitac";
82   MatitaLog.message ""
83   
84 let clean_exit n =
85  match !status with
86     None -> exit n
87   | Some status ->
88      let baseuri = MatitaTypes.get_string_option !status "baseuri" in
89       MatitacleanLib.clean_baseuris ~verbose:false [baseuri];
90       exit n
91   
92 let rec interactive_loop () = 
93  let str = Stream.of_channel stdin in
94   try
95     run_script str MatitaEngine.eval_from_stream_greedy
96   with 
97   | MatitaEngine.Drop -> pp_ocaml_mode ()
98   | Sys.Break -> MatitaLog.error "user break!"; interactive_loop ()
99   | MatitaTypes.Command_error _ -> interactive_loop ()
100   | CicTextualParser2.Parse_error (floc,err) ->
101      (* check for EOI *)
102      if Stream.peek str = None then
103       clean_exit 0
104      else
105       let (x, y) = CicAst.loc_of_floc floc in
106       MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
107       interactive_loop ()
108   | exn -> MatitaLog.error (Printexc.to_string exn); interactive_loop ()
109
110 let go () =
111   Helm_registry.load_from "matita.conf.xml";
112   Http_getter.init ();
113   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
114   status := Some (ref (Lazy.force MatitaEngine.initial_status));
115   Sys.catch_break true;
116   interactive_loop ()
117
118 let clean_exit n =
119  match !status with
120     None -> exit n
121   | Some status ->
122      let baseuri = MatitaTypes.get_string_option !status "baseuri" in
123       MatitacleanLib.clean_baseuris ~verbose:false [baseuri];
124       exit n
125   
126 let dump_moo_to_file file moo =
127  let os = open_out (MatitaMisc.obj_file_of_script file) in
128  let output s = output_string os s in
129  output "(* GENERATED FILE: DO NOT EDIT! *)\n\n";
130  List.iter output (List.rev moo);
131  close_out os
132   
133 let main ~mode = 
134   Helm_registry.load_from "matita.conf.xml";
135   Http_getter.init ();
136   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
137   status := Some (ref (Lazy.force MatitaEngine.initial_status));
138   Sys.catch_break true;
139   let fname = fname () in
140   try
141     let time = Unix.time () in
142     MatitaLog.message (sprintf "execution of %s started:" fname);
143     let is =
144       Stream.of_channel
145         (match fname with
146         | "stdin" -> stdin
147         | fname -> open_in fname)
148     in
149     run_script is MatitaEngine.eval_from_stream;
150     let elapsed = Unix.time () -. time in
151     let tm = Unix.gmtime elapsed in
152     let sec = 
153       if tm.Unix.tm_sec > 0 then  (string_of_int tm.Unix.tm_sec ^ "''") else "" 
154     in
155     let min = 
156       if tm.Unix.tm_min > 0 then  (string_of_int tm.Unix.tm_min ^ "' ") else "" 
157     in
158     let hou = 
159       if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else "" 
160     in
161     let proof_status,moo_content_rev = 
162       match !status with
163       | Some s -> !s.proof_status, !s.moo_content_rev
164       | None -> assert false
165     in
166     if proof_status <> MatitaTypes.No_proof then
167      begin
168       MatitaLog.error
169        "there are still incomplete proofs at the end of the script";
170       clean_exit 2
171      end
172     else
173      begin
174        dump_moo_to_file fname moo_content_rev;
175        MatitaLog.message 
176          (sprintf "execution of %s completed in %s." fname (hou^min^sec));
177        exit 0
178      end
179   with 
180   | Sys.Break ->
181       MatitaLog.error "user break!";
182       if mode = `COMPILER then
183         clean_exit ~-1
184       else
185         pp_ocaml_mode ()
186   | MatitaEngine.Drop ->
187       if mode = `COMPILER then 
188         clean_exit 1 
189       else 
190         pp_ocaml_mode ()
191   | CicTextualParser2.Parse_error (floc,err) ->
192      let (x, y) = CicAst.loc_of_floc floc in
193      MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
194      if mode = `COMPILER then
195        clean_exit 1
196      else
197        pp_ocaml_mode ()
198   | _ ->
199      if mode = `COMPILER then 
200        clean_exit 3
201      else 
202        pp_ocaml_mode ()