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