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