]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/top.ml
23460410d36dcefc79074d339d6bfd14300c231c
[helm.git] / helm / software / lambda-delta / toplevel / top.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.              
9      \ /   This software is distributed as is, NO WARRANTY.              
10       V_______________________________________________________________ *)
11
12 module P    = Printf
13 module U    = NUri
14 module C    = Cps
15 module L    = Log
16 module T    = Time
17 module H    = Hierarchy
18 module O    = Output
19 module AP   = AutProcess
20 module AO   = AutOutput
21 module MA   = MetaAut
22 module MO   = MetaOutput
23 module MBrg = MetaBrg
24 module CL   = CommonLibrary
25 module BrgO = BrgOutput
26 module BrgR = BrgReduction
27 module BrgU = BrgUntrusted
28 module MBag = MetaBag
29 module BagO = BagOutput
30 module BagR = BagReduction
31 module BagU = BagUntrusted
32
33 type status = {
34    ast : AP.status;
35    mst : MA.status;
36    ac  : AO.counters;
37    mc  : MO.counters;
38    brgc: BrgO.counters;
39    bagc: BagO.counters
40 }
41
42 let initial_status cover = {
43    ac   = AO.initial_counters;
44    mc   = MO.initial_counters;
45    brgc = BrgO.initial_counters;
46    bagc = BagO.initial_counters;
47    mst  = MA.initial_status ~cover ();
48    ast  = AP.initial_status
49 }
50
51 let count count_fun c item =
52    if !L.level > 2 then count_fun C.start c item else c
53
54 let flush () = L.flush 0; L.flush_err ()
55
56 let bag_error s msg =
57    L.error BagO.specs (L.Warn s :: L.Loc :: msg); flush () 
58
59 let brg_error s msg =
60    L.error BrgO.specs (L.Warn s :: L.Loc :: msg); flush () 
61
62 let process_item f st =
63    let f ast = f {st with ast = ast} in
64    AP.process_item f st.ast
65
66 (* kernel related ***********************************************************)
67
68 type kernel = Brg | Bag
69
70 type kernel_item = BrgItem of Brg.item
71                  | BagItem of Bag.item
72
73 let kernel = ref Brg
74
75 let print_counters st = match !kernel with
76    | Brg -> BrgO.print_counters C.start st.brgc
77    | Bag -> BagO.print_counters C.start st.bagc
78
79 let kernel_of_meta f st item = match !kernel with
80    | Brg -> 
81       let f item = f st (BrgItem item) in
82       MBrg.brg_of_meta f item
83    | Bag -> 
84       let f item = f st (BagItem item) in
85       MBag.bag_of_meta f item  
86
87 let count_item st = function
88    | BrgItem item -> {st with brgc = count BrgO.count_item st.brgc item}
89    | BagItem item -> {st with bagc = count BagO.count_item st.bagc item}
90
91 let export_item base = function
92    | BrgItem item -> CL.export_item BrgO.export_obj base item
93    | BagItem _    -> ()
94
95 let type_check f st si g k =
96    let f _ = function
97       | None           -> f st None
98       | Some (i, u, _) -> f st (Some (i, u))
99    in
100    match k with
101       | BrgItem item -> BrgU.type_check f ~si g item
102       | BagItem item -> BagU.type_check f ~si g item
103
104 (****************************************************************************)
105
106 let main =
107 try 
108    let version_string = "Helena 0.8.0 M - August 2009" in
109    let stage = ref 3 in
110    let meta_file = ref None in
111    let progress = ref false in
112    let use_cover = ref true in
113    let si = ref false in
114    let library_dir = ref None in
115    let set_hierarchy s = 
116       let f = function
117          | Some g -> H.graph := g
118          | None   -> L.warn (P.sprintf "Unknown type hierarchy: %s" s)
119       in
120       H.graph_of_string f s
121    in
122    let set_kernel = function
123       | "brg" -> kernel := Brg
124       | "bag" -> kernel := Bag
125       | s     -> L.warn (P.sprintf "Unknown kernel version: %s" s)
126    in
127    let set_summary i = L.level := i in
128    let print_version () = L.warn (version_string ^ "\n"); exit 0 in
129    let set_stage i = stage := i in
130    let close = function
131       | None          -> ()
132       | Some (och, _) -> close_out och
133    in
134    let set_meta_file name =
135       close !meta_file;
136       let och = open_out name in
137       let frm = Format.formatter_of_out_channel och in
138       Format.pp_set_margin frm max_int;
139       meta_file := Some (och, frm)
140    in
141    let set_library_dir name =
142       library_dir := Some name
143    in
144    let read_file name =
145       if !L.level > 0 then T.gmtime version_string;      
146       if !L.level > 1 then
147          L.warn (P.sprintf "Processing file: %s" name);
148       if !L.level > 0 then T.utime_stamp "started";
149       let ich = open_in name in
150       let lexbuf = Lexing.from_channel ich in
151       let book = AutParser.book AutLexer.token lexbuf in
152       close_in ich;
153       if !L.level > 0 then T.utime_stamp "parsed";
154       let rec aux st = function
155          | []         -> st
156          | item :: tl ->
157 (* stage 3 *)
158             let f st = function
159                | None        -> st
160                | Some (i, u) -> 
161                   if !progress then 
162                      L.warn (P.sprintf "[%u] %s" i (U.string_of_uri u)); 
163                   st
164             in
165 (* stage 2 *)       
166             let f st item =
167                let st = count_item st item in
168                begin match !library_dir with
169                   | None      -> ()
170                   | Some base -> export_item base item 
171                end;
172                if !stage > 2 then type_check f st !si !H.graph item else st
173             in
174 (* stage 1 *)       
175             let f st mst item = 
176                let st = {st with
177                   mst = mst; mc = count MO.count_item st.mc item
178                } in
179                begin match !meta_file with
180                   | None          -> ()
181                   | Some (_, frm) -> MO.pp_item C.start frm item
182                end;
183                if !stage > 1 then kernel_of_meta f st item else st 
184             in  
185 (* stage 0 *)       
186             let st = {st with ac = count AO.count_item st.ac item} in
187             let f st item =
188                let st = 
189                   if !stage > 0 then MA.meta_of_aut (f st) st.mst item else st
190                in
191                aux st tl
192             in
193             process_item f st item
194       in
195       O.clear_reductions ();
196       let cover = 
197          if !use_cover then Filename.chop_extension (Filename.basename name)
198          else ""
199       in
200       let st = aux (initial_status cover) book in
201       if !L.level > 0 then T.utime_stamp "processed";
202       if !L.level > 2 then AO.print_counters C.start st.ac;
203       if !L.level > 2 then AO.print_process_counters C.start st.ast;
204       if !L.level > 2 && !stage > 0 then MO.print_counters C.start st.mc;
205       if !L.level > 2 && !stage > 1 then print_counters st;
206       if !L.level > 2 && !stage > 1 then O.print_reductions ()
207    in
208    let help = 
209       "Usage: helena [ -Vcipu | -Ss <number> | -m <file> | -hk <string> ] <file> ...\n\n" ^
210       "Summary levels: 0 just errors, 1 time stamps, 2 processed file names, \
211        3 data information, 4 typing information, 5 reduction information\n\n" ^
212        "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted\n"
213    in
214    let help_S = "<number>  set summary level" in
215    let help_V = " show version information" in   
216    let help_c = " disable initial segment of URI hierarchy" in
217    let help_h = "<string>  set type hierarchy" in
218    let help_i = " show local references by index" in
219    let help_k = "<string>  set kernel version" in
220    let help_m = "<file>  output intermediate representation" in
221    let help_p = " activate progress indicator" in
222    let help_u = " activate sort inclusion" in
223    let help_s = "<number>  Set translation stage" in
224    let help_x = "<directory>  export kernel objects (XML)" in
225    L.box 0; L.box_err ();
226    H.set_new_sorts ignore ["Set"; "Prop"];
227    Arg.parse [
228       ("-S", Arg.Int set_summary, help_S);
229       ("-V", Arg.Unit print_version, help_V);
230       ("-c", Arg.Clear use_cover, help_c);
231       ("-h", Arg.String set_hierarchy, help_h);
232       ("-i", Arg.Set O.indexes, help_i);
233       ("-k", Arg.String set_kernel, help_k);
234       ("-m", Arg.String set_meta_file, help_m); 
235       ("-p", Arg.Set progress, help_p);
236       ("-u", Arg.Set si, help_u);
237       ("-s", Arg.Int set_stage, help_s);
238       ("-x", Arg.String set_library_dir, help_x)
239    ] read_file help;
240    if !L.level > 0 then T.utime_stamp "at exit";
241    flush ()
242 with BagR.TypeError msg -> bag_error "Type Error" msg
243    | BrgR.TypeError msg -> brg_error "Type Error" msg