]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/top.ml
- improved logging
[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 H    = Hierarchy
17 module AO   = AutOutput
18 module MA   = MetaAut
19 module MO   = MetaOutput
20 module MBag = MetaBag
21 module BagO = BagOutput
22 module BagU = BagUntrusted
23
24 type status = {
25    mst : MA.status;
26    ac  : AO.counters;
27    mc  : MO.counters;
28    bagc: BagO.counters
29 }
30
31 let initial_status = {
32    ac   = AO.initial_counters;
33    mc   = MO.initial_counters;
34    bagc = BagO.initial_counters;
35    mst  = MA.initial_status
36 }
37
38 let count count_fun c item =
39    if !L.level > 2 then count_fun C.start c item else c
40
41 let flush () = L.flush 0; L.flush_err ()
42
43 let bag_error s msg =
44    L.error BagO.specs (L.Warn s :: msg); flush () 
45
46 let main =
47 try 
48    let version_string = "Helena Checker 0.8.0 M - December 2008" in
49    let stage = ref 3 in
50    let meta_file = ref None in
51    let set_hierarchy s = 
52       let f = function
53          | Some g -> H.graph := g
54          | None   -> L.warn (P.sprintf "Unknown type hierarchy: %s" s)
55       in
56       H.graph_of_string f s
57    in
58    let set_summary i = L.level := i in
59    let print_version () = L.warn version_string; exit 0 in
60    let set_stage i = stage := i in
61    let close = function
62       | None          -> ()
63       | Some (och, _) -> close_out och
64    in
65    let set_meta_file name =
66       close !meta_file;
67       let och = open_out name in
68       let frm = Format.formatter_of_out_channel och in
69       Format.pp_set_margin frm max_int;
70       meta_file := Some (och, frm)
71    in
72    let read_file name =
73       if !L.level > 0 then Time.gmtime version_string;      
74       if !L.level > 1 then
75          L.warn (P.sprintf "Processing file: %s" name);
76       if !L.level > 0 then Time.utime_stamp "started";
77       let ich = open_in name in
78       let lexbuf = Lexing.from_channel ich in
79       let book = AutParser.book AutLexer.token lexbuf in
80       close_in ich;
81       if !L.level > 0 then Time.utime_stamp "parsed";
82       let rec aux st = function
83          | []         -> st
84          | item :: tl ->
85 (* stage 3 *)
86             let f st _ = function
87                | None           -> st
88                | Some (i, u, _) -> 
89                   Log.warn (P.sprintf "[%u] %s" i (U.string_of_uri u)); st
90             in
91 (* stage 2 *)       
92             let f st item =
93                let st = {st with bagc = count BagO.count_item st.bagc item} in
94                if !stage > 2 then BagU.type_check (f st) !H.graph item else st
95             in
96 (* stage 1 *)       
97             let f mst item = 
98                let st = {st with
99                   mst = mst; mc = count MO.count_item st.mc item
100                } in
101                begin match !meta_file with
102                   | None          -> ()
103                   | Some (_, frm) -> MO.pp_item C.start frm item
104                end;
105                if !stage > 1 then MBag.bag_of_meta (f st) item else st 
106             in  
107 (* stage 0 *)       
108             let st = {st with ac = count AO.count_item st.ac item} in
109             let st =
110                if !stage > 0 then MA.meta_of_aut f st.mst item else st
111             in
112             aux st tl
113       in 
114       let st = aux initial_status book in
115       if !L.level > 0 then Time.utime_stamp "processed";
116       if !L.level > 2 then AO.print_counters C.start st.ac;
117       if !L.level > 2 && !stage > 0 then MO.print_counters C.start st.mc;
118       if !L.level > 2 && !stage > 1 then BagO.print_counters C.start st.bagc;
119    in
120    let help = 
121       "Usage: helena [ -Vi | -Ss <number> | -m <file> | -h <string> ] <file> ...\n\n" ^
122       "Summary levels: 0 just errors, 1 time stamps, 2 processed file names, \
123        3 data information, 4 typing information, 5 reduction information\n\n" ^
124        "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted\n"
125    in
126    let help_S = "<number>  set summary level" in
127    let help_V = " show version information" in   
128    let help_h = "<string>  set type hierarchy" in
129    let help_i = " show local references by index" in
130    let help_m = "<file>  output intermediate representation" in
131    let help_s = "<number>  Set translation stage" in
132    L.box 0; L.box_err ();
133    H.set_new_sorts ignore ["Set"; "Prop"];
134    Arg.parse [
135       ("-S", Arg.Int set_summary, help_S);
136       ("-V", Arg.Unit print_version, help_V);
137       ("-h", Arg.String set_hierarchy, help_h);
138       ("-i", Arg.Set BagO.indexes, help_i);
139       ("-m", Arg.String set_meta_file, help_m); 
140       ("-s", Arg.Int set_stage, help_s);
141    ] read_file help;
142    if !L.level > 0 then Time.utime_stamp "at exit";
143    flush ()
144 with BagType.TypeError msg -> bag_error "Type Error" msg