]> matita.cs.unibo.it Git - helm.git/blob - matita/matitacLib.ml
ee7a2eae582be1b6b4569650162d13f3cb7561c8
[helm.git] / 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 (* $Id$ *)
27
28 open Printf
29
30 open GrafiteTypes
31
32 exception AttemptToInsertAnAlias
33
34 let out = ref ignore 
35 let set_callback f = out := f
36
37 let slash_n_RE = Pcre.regexp "\\n" ;;
38
39 let pp_ast_statement grafite_status stm =
40   let stm = GrafiteAstPp.pp_statement
41     ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
42     ~term_pp:CicNotationPp.pp_term
43     ~lazy_term_pp:CicNotationPp.pp_term ~obj_pp:(CicNotationPp.pp_obj
44     CicNotationPp.pp_term) stm
45   in
46   let stm = Pcre.replace ~rex:slash_n_RE stm in
47   let stm =
48       if String.length stm > 50 then String.sub stm 0 50 ^ " ..."
49       else stm
50   in
51     HLog.debug ("Executing: ``" ^ stm ^ "''")
52 ;;
53
54 let clean_exit baseuri rc =
55   LibraryClean.clean_baseuris ~verbose:false [baseuri]; rc
56 ;;
57
58 let get_macro_context = function
59    | Some {GrafiteTypes.proof_status = GrafiteTypes.No_proof} -> []
60    | Some status                ->
61       let stack = GrafiteTypes.get_stack status in
62       let goal = Continuationals.Stack.find_goal stack in
63       GrafiteTypes.get_proof_context status goal
64    | None                       -> assert false
65 ;;
66    
67 let pp_times fname rc big_bang = 
68   if not (Helm_registry.get_bool "matita.verbose") then
69     let { Unix.tms_utime = u ; Unix.tms_stime = s} = Unix.times () in
70     let r = Unix.gettimeofday () -. big_bang in
71     let extra = try Sys.getenv "BENCH_EXTRA_TEXT" with Not_found -> "" in
72     let rc = if rc then "\e[0;32mOK\e[0m" else "\e[0;31mFAIL\e[0m" in
73     let times = 
74       let fmt t = 
75         let seconds = int_of_float t in
76         let cents = int_of_float ((t -. floor t) *. 100.0) in
77         let minutes = seconds / 60 in
78         let seconds = seconds mod 60 in
79         Printf.sprintf "%dm%02d.%02ds" minutes seconds cents
80       in
81       Printf.sprintf "%s %s %s" (fmt r) (fmt u) (fmt s)
82     in
83     let s = Printf.sprintf "%-4s %s %s" rc times extra in
84     print_endline s;
85     flush stdout
86 ;;
87
88 let cut prefix s = 
89   let lenp = String.length prefix in
90   let lens = String.length s in
91   assert (lens > lenp);
92   assert (String.sub s 0 lenp = prefix);
93   String.sub s lenp (lens-lenp)
94 ;;
95
96 let rec compile fname =
97   (* initialization, MOVE OUTSIDE *)
98   let matita_debug = Helm_registry.get_bool "matita.debug" in
99   let clean_baseuri = not (Helm_registry.get_bool "matita.preserve") in    
100   let include_paths = 
101     Helm_registry.get_list Helm_registry.string "matita.includes" 
102   in
103   (* sanity checks *)
104   let root,baseuri,fname = Librarian.baseuri_of_script ~include_paths fname in
105   let moo_fname = 
106    LibraryMisc.obj_file_of_baseuri ~must_exist:false ~baseuri ~writable:true
107   in
108   let lexicon_fname= 
109    LibraryMisc.lexicon_file_of_baseuri ~must_exist:false ~baseuri ~writable:true
110   in
111   if Http_getter_storage.is_read_only baseuri then 
112     HLog.error 
113       (Printf.sprintf "uri %s belongs to a read-only repository" baseuri);
114   (* cleanup of previously compiled objects *)
115   if (not (Http_getter_storage.is_empty ~local:true baseuri) ||
116       LibraryClean.db_uris_of_baseuri baseuri <> []) && clean_baseuri
117     then begin
118     HLog.message ("baseuri " ^ baseuri ^ " is not empty");
119     HLog.message ("cleaning baseuri " ^ baseuri);
120     LibraryClean.clean_baseuris [baseuri];
121     assert (Http_getter_storage.is_empty ~local:true baseuri);
122   end;
123   (* create dir for XML files *)
124   if not (Helm_registry.get_opt_default Helm_registry.bool "matita.nodisk"
125             ~default:false) 
126   then
127     HExtlib.mkdir 
128       (Filename.dirname 
129         (Http_getter.filename ~local:true ~writable:true (baseuri ^
130         "foo.con")));
131   (* begin of compilation *)
132   let grafite_status = GrafiteSync.init baseuri in
133   let lexicon_status = 
134     CicNotation2.load_notation ~include_paths:[]
135       BuildTimeConf.core_notation_script 
136   in
137   let big_bang = Unix.gettimeofday () in
138   let time = Unix.time () in
139   HLog.message ("compiling " ^ Filename.basename fname ^ " in " ^ baseuri);
140   if not (Helm_registry.get_bool "matita.verbose") then
141     (let cc = 
142       if Str.string_match(Str.regexp ".*opt$") Sys.argv.(0) 0 then "matitac.opt"
143       else "matitac" 
144     in
145     let s = Printf.sprintf "%s %-35s " cc (cut (root^"/") fname) in
146     print_string s; flush stdout);
147   let buf = Ulexing.from_utf8_channel (open_in fname) in
148   let print_cb =
149     if not (Helm_registry.get_bool "matita.verbose") then (fun _ _ -> ())
150     else pp_ast_statement
151   in
152   try
153     let grafite_status, lexicon_status =
154       match
155        MatitaEngine.eval_from_stream ~first_statement_only:false ~include_paths
156         lexicon_status grafite_status buf print_cb
157       with
158       | [] -> raise End_of_file
159       | ((grafite,lexicon),None)::_ -> grafite, lexicon
160       | (s,Some _)::_ -> raise AttemptToInsertAnAlias
161     in
162     let elapsed = Unix.time () -. time in
163     let proof_status,moo_content_rev,lexicon_content_rev = 
164       grafite_status.proof_status, grafite_status.moo_content_rev, 
165        lexicon_status.LexiconEngine.lexicon_content_rev
166     in
167     if proof_status <> GrafiteTypes.No_proof then
168      (HLog.error
169       "there are still incomplete proofs at the end of the script"; 
170      pp_times fname false big_bang;
171      clean_exit baseuri false)
172     else
173      (if Helm_registry.get_bool "matita.moo" then begin
174         (* FG: we do not generate .moo when dumping .mma files *)
175         GrafiteMarshal.save_moo moo_fname moo_content_rev;
176         LexiconMarshal.save_lexicon lexicon_fname lexicon_content_rev;
177      end;
178      let tm = Unix.gmtime elapsed in
179      let sec = string_of_int tm.Unix.tm_sec ^ "''" in
180      let min = 
181        if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min^"' ") else "" 
182      in
183      let hou = 
184        if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour^"h ") else ""
185      in
186      HLog.message 
187        (sprintf "execution of %s completed in %s." fname (hou^min^sec));
188      pp_times fname true big_bang;
189      true)
190   with 
191   | End_of_file -> HLog.error "End_of_file?!"; clean_exit baseuri false
192   | Sys.Break as exn ->
193      if matita_debug then raise exn;
194      HLog.error "user break!";
195      pp_times fname false big_bang;
196      clean_exit baseuri false
197   | GrafiteEngine.Macro (floc, f) ->
198       (match f (get_macro_context (Some grafite_status)) with 
199       | _, GrafiteAst.Inline (_, style, suri, prefix) ->
200         let str =
201          ApplyTransformation.txt_of_inline_macro style suri prefix
202           ~map_unicode_to_tex:(Helm_registry.get_bool
203             "matita.paste_unicode_as_tex") in
204         !out str;
205         compile fname 
206       | _ ->
207         let x, y = HExtlib.loc_of_floc floc in
208         HLog.error (sprintf "A macro has been found at %d-%d" x y);
209         pp_times fname false big_bang;
210         clean_exit baseuri false)
211   | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
212       let (x, y) = HExtlib.loc_of_floc floc in
213       HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
214       pp_times fname false big_bang;
215       clean_exit baseuri false
216   | exn ->
217        if matita_debug then raise exn;
218        HLog.error (snd (MatitaExcPp.to_string exn));
219        pp_times fname false big_bang;
220        clean_exit baseuri false
221 ;;
222