]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitacLib.ml
ocaml 3.09 transition
[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 status = ref None
33
34 let run_script is eval_function  =
35   let status = 
36     match !status with
37     | None -> assert false
38     | Some s -> s
39   in
40   let slash_n_RE = Pcre.regexp "\\n" in
41   let cb = 
42     if Helm_registry.get_bool "matita.quiet" then 
43       (fun _ _ -> ())
44     else 
45       (fun status stm ->
46         (* dump_status status; *)
47         let stm = GrafiteAstPp.pp_statement stm in
48         let stm = Pcre.replace ~rex:slash_n_RE stm in
49         let stm = 
50           if String.length stm > 50 then
51             String.sub stm 0 50 ^ " ..."
52           else
53             stm
54         in
55         MatitaLog.debug ("Executing: ``" ^ stm ^ "''"))
56   in
57   try
58     eval_function status is cb
59   with
60   | MatitaEngine.Drop  
61   | End_of_file
62   | CicNotationParser.Parse_error _ as exn -> raise exn
63   | exn -> 
64       MatitaLog.error (MatitaExcPp.to_string exn);
65       raise exn
66
67 let fname () =
68   match Helm_registry.get_list Helm_registry.string "matita.args" with
69   | [x] -> x
70   | _ -> MatitaInit.die_usage ()
71
72 let pp_ocaml_mode () = 
73   MatitaLog.message "";
74   MatitaLog.message "                      ** Entering Ocaml mode ** ";
75   MatitaLog.message "";
76   MatitaLog.message "Type 'go ();;' to enter an interactive matitac";
77   MatitaLog.message ""
78   
79 let clean_exit n =
80  let opt_exit =
81   function
82      None -> ()
83    | Some n -> exit n
84  in
85   match !status with
86      None -> opt_exit n
87    | Some status ->
88       try
89        let baseuri = MatitaTypes.get_string_option !status "baseuri" in
90        MatitacleanLib.clean_baseuris ~verbose:false [baseuri];
91        opt_exit n
92       with MatitaTypes.Option_error("baseuri", "not found") ->
93        (* no baseuri ==> nothing to clean yet *)
94        opt_exit n
95   
96 let rec interactive_loop () = 
97   let str = Ulexing.from_utf8_channel stdin in
98   try
99     run_script str 
100       (MatitaEngine.eval_from_stream_greedy
101       ~include_paths:(Helm_registry.get_list Helm_registry.string
102         "matita.includes"))
103   with 
104   | MatitaEngine.Drop -> pp_ocaml_mode ()
105   | Sys.Break -> MatitaLog.error "user break!"; interactive_loop ()
106   | MatitaTypes.Command_error _ -> interactive_loop ()
107   | End_of_file ->
108      print_newline ();
109      clean_exit (Some 0)
110   | CicNotationParser.Parse_error (floc,err) ->
111      let (x, y) = CicNotationPt.loc_of_floc floc in
112      MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
113      interactive_loop ()
114   | exn -> MatitaLog.error (Printexc.to_string exn); interactive_loop ()
115
116 let go () =
117   Helm_registry.load_from BuildTimeConf.matita_conf;
118   CicNotation.load_notation BuildTimeConf.core_notation_script;
119   Http_getter.init ();
120   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
121   MatitaDb.create_owner_environment ();
122   CicEnvironment.set_trust (* environment trust *)
123     (let trust = Helm_registry.get_bool "matita.environment_trust" in
124      fun _ -> trust);
125   status := Some (ref (Lazy.force MatitaEngine.initial_status));
126   Sys.catch_break true;
127   interactive_loop ()
128
129 let main ~mode = 
130   MatitaInit.initialize_all ();
131   (* must be called after init since args are set by cmdline parsing *)
132   let fname = fname () in
133   status := Some (ref (Lazy.force MatitaEngine.initial_status));
134   Sys.catch_break true;
135   let origcb = MatitaLog.get_log_callback () in
136   let newcb tag s =
137     match tag with
138     | `Debug | `Message -> ()
139     | `Warning | `Error -> origcb tag s
140   in
141   if Helm_registry.get_bool "matita.quiet" then
142     MatitaLog.set_log_callback newcb;
143   let matita_debug = Helm_registry.get_bool "matita.debug" in
144   try
145     let time = Unix.time () in
146     if Helm_registry.get_bool "matita.quiet" then
147       origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
148     else
149       MatitaLog.message (sprintf "execution of %s started:" fname);
150     let is =
151       Ulexing.from_utf8_channel
152         (match fname with
153         | "stdin" -> stdin
154         | fname -> open_in fname)
155     in
156     run_script is 
157       (MatitaEngine.eval_from_stream 
158         ~include_paths:(Helm_registry.get_list Helm_registry.string
159           "matita.includes")
160         ~clean_baseuri:(not (Helm_registry.get_bool "matita.preserve")));
161     let elapsed = Unix.time () -. time in
162     let tm = Unix.gmtime elapsed in
163     let sec = 
164       if tm.Unix.tm_sec > 0 then (string_of_int tm.Unix.tm_sec ^ "''") else "" 
165     in
166     let min = 
167       if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min ^ "' ") else "" 
168     in
169     let hou = 
170       if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else ""
171     in
172     let proof_status,moo_content_rev = 
173       match !status with
174       | Some s -> !s.proof_status, !s.moo_content_rev
175       | None -> assert false
176     in
177     if proof_status <> MatitaTypes.No_proof then
178      begin
179       MatitaLog.error
180        "there are still incomplete proofs at the end of the script";
181       clean_exit (Some 2)
182      end
183     else
184      begin
185        let moo_fname = MatitacleanLib.obj_file_of_script fname in
186        MatitaMoo.save_moo moo_fname moo_content_rev;
187        MatitaLog.message 
188          (sprintf "execution of %s completed in %s." fname (hou^min^sec));
189        exit 0
190      end
191   with 
192   | Sys.Break ->
193       MatitaLog.error "user break!";
194       if mode = `COMPILER then
195         clean_exit (Some ~-1)
196       else
197         pp_ocaml_mode ()
198   | MatitaEngine.Drop ->
199       if mode = `COMPILER then 
200         clean_exit (Some 1)
201       else 
202         pp_ocaml_mode ()
203   | CicNotationParser.Parse_error (floc,err) ->
204      let (x, y) = CicNotationPt.loc_of_floc floc in
205      MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
206      if mode = `COMPILER then
207        clean_exit (Some 1)
208      else
209        pp_ocaml_mode ()
210   | exn ->
211       if matita_debug then raise exn;
212       if mode = `COMPILER then 
213         clean_exit (Some 3)
214       else 
215         pp_ocaml_mode ()
216