]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/top.ml
6c6691354f65e1754330d9e46db95a88d8950ed5
[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 F    = Filename
13 module P    = Printf
14 module U    = NUri
15 module C    = Cps
16 module L    = Log
17 module T    = Time
18 module H    = Hierarchy
19 module O    = Output
20 module Y    = Entity
21 module X    = Library
22 module AL   = AutLexer
23 module AP   = AutProcess
24 module AO   = AutOutput
25 module DT   = CrgTxt
26 module DA   = CrgAut
27 module MA   = MetaAut
28 module MO   = MetaOutput
29 module ML   = MetaLibrary
30 module DO   = CrgOutput
31 module DBrg = CrgBrg
32 module MBrg = MetaBrg
33 module BrgO = BrgOutput
34 module BrgR = BrgReduction
35 module BrgU = BrgUntrusted
36 module MBag = MetaBag
37 module BagO = BagOutput
38 module BagT = BagType
39 module BagU = BagUntrusted
40
41 type status = {
42    ast : AP.status;
43    dst : DA.status;
44    mst : MA.status;
45    tst : DT.status;
46    ac  : AO.counters;
47    mc  : MO.counters;
48    brgc: BrgO.counters;
49    bagc: BagO.counters;
50    kst : Y.status
51 }
52
53 let flush_all () = L.flush 0; L.flush_err ()
54
55 let bag_error s msg =
56    L.error BagO.specs (L.Warn s :: L.Loc :: msg); flush_all () 
57
58 let brg_error s msg =
59    L.error BrgR.specs (L.Warn s :: L.Loc :: msg); flush_all () 
60
61 let initial_status mk_uri cover expand si = {
62    ac   = AO.initial_counters;
63    mc   = MO.initial_counters;
64    brgc = BrgO.initial_counters;
65    bagc = BagO.initial_counters;
66    mst  = MA.initial_status ~cover ();
67    dst  = DA.initial_status (mk_uri si cover);
68    tst  = DT.initial_status (mk_uri si cover);
69    ast  = AP.initial_status;
70    kst  = Y.initial_status expand si
71 }
72
73 (* kernel related ***********************************************************)
74
75 type kernel = Brg | Bag
76
77 type kernel_entity = BrgEntity  of Brg.entity
78                    | BagEntity  of Bag.entity
79                    | CrgEntity  of Crg.entity
80                    | MetaEntity of Meta.entity
81
82 let kernel = ref Brg
83
84 let print_counters st = match !kernel with
85    | Brg -> BrgO.print_counters C.start st.brgc
86    | Bag -> BagO.print_counters C.start st.bagc
87
88 let xlate_entity entity = match !kernel, entity with
89    | Brg, CrgEntity e  -> 
90       let f e = (BrgEntity e) in Y.xlate f DBrg.brg_of_crg e
91    | Brg, MetaEntity e -> 
92       let f e = (BrgEntity e) in Y.xlate f MBrg.brg_of_meta e
93    | Bag, MetaEntity e -> 
94       let f e = (BagEntity e) in Y.xlate f MBag.bag_of_meta e  
95    | _, entity         -> entity
96
97 let pp_progress e =
98    let f a u =
99       let s = U.string_of_uri u in
100       let err () = L.warn (P.sprintf "%s" s) in
101       let f i = L.warn (P.sprintf "[%u] %s" i s) in
102       Y.mark err f a
103    in
104    match e with
105       | CrgEntity e -> Y.common f e
106       | BrgEntity e -> Y.common f e
107       | BagEntity e -> Y.common f e      
108       | MetaEntity e -> Y.common f e
109
110 let count_entity st = function
111    | MetaEntity e -> {st with mc = MO.count_entity C.start st.mc e} 
112    | BrgEntity e  -> {st with brgc = BrgO.count_entity C.start st.brgc e}
113    | BagEntity e  -> {st with bagc = BagO.count_entity C.start st.bagc e}
114    | _            -> st
115
116 let export_entity si moch = function
117    | CrgEntity e  -> X.export_entity DO.export_term si e
118    | BrgEntity e  -> X.export_entity BrgO.export_term si e
119    | MetaEntity e ->
120       begin match moch with
121          | None     -> ()
122          | Some och -> ML.write_entity C.start och e
123       end
124    | BagEntity _  -> ()
125
126 let type_check st k =
127    let brg_err msg = brg_error "Type Error" msg; failwith "Interrupted" in
128    let ok _ _ = st in
129    match k with
130       | BrgEntity entity -> BrgU.type_check brg_err ok st.kst entity
131       | BagEntity entity -> BagU.type_check ok st.kst entity
132       | CrgEntity _
133       | MetaEntity _     -> st
134
135 (* extended lexer ***********************************************************)
136
137 type 'token lexer = {
138    parse : Lexing.lexbuf -> 'token;
139    mutable tokbuf: 'token option;
140    mutable unget : bool
141 }
142
143 let initial_lexer parse = {
144    parse = parse; tokbuf = None; unget = false
145 }
146
147 let token xl lexbuf = match xl.tokbuf with
148    | Some token when xl.unget ->   
149       xl.unget <- false; token
150    | _                        ->
151       let token = xl.parse lexbuf in
152       xl.tokbuf <- Some token; token
153
154 (* input related ************************************************************)
155
156 type input = Text | Automath
157
158 type input_entity = TxtEntity of Txt.command
159                   | AutEntity of Aut.command
160
161 let type_of_input name =
162    if F.check_suffix name ".hln" then Text 
163    else if F.check_suffix name ".aut" then 
164       let _ = H.set_sorts 0 ["Set"; "Prop"] in
165       assert (H.set_graph "Z2");
166       Automath
167    else begin
168       L.warn (P.sprintf "Unknown file type: %s" name); exit 2
169    end
170
171 let txt_xl = initial_lexer TxtLexer.token 
172
173 let aut_xl = initial_lexer AutLexer.token 
174
175 let entity_of_input lexbuf = function
176    | Text     -> 
177       begin match TxtParser.entry (token txt_xl) lexbuf with
178          | Some e, unget -> txt_xl.unget <- unget; Some (TxtEntity e)
179          | None, _       -> None
180       end
181    | Automath -> 
182       begin match AutParser.entry (token aut_xl) lexbuf with
183          | Some e, unget -> aut_xl.unget <- unget; Some (AutEntity e)
184          | None, _       -> None
185       end
186
187 let process_input f st = function 
188    | AutEntity e     ->
189       let f ast e = f {st with ast = ast} (AutEntity e) in
190       AP.process_command f st.ast e
191    | xe              -> f st xe
192
193 let count_input st = function
194    | AutEntity e -> {st with ac = AO.count_command C.start st.ac e}
195    | xe          -> st
196
197 (****************************************************************************)
198
199 let stage = ref 3
200 let moch = ref None
201 let meta = ref false
202 let progress = ref false
203 let preprocess = ref false
204 let root = ref ""
205 let si = ref false
206 let expand = ref false
207 let cc = ref false
208 let export = ref false
209 let old = ref false
210
211 let process_2 st entity =
212    let st = if !L.level > 2 then count_entity st entity else st in
213    if !export then export_entity !si !moch entity;
214    if !stage > 2 then type_check st entity else st
215             
216 let process_1 st entity = 
217    if !progress then pp_progress entity;
218    let st = if !L.level > 2 then count_entity st entity else st in
219    if !export && !stage = 1 then export_entity !si !moch entity;
220    if !stage > 1 then process_2 st (xlate_entity entity) else st 
221
222 let process_0 st entity = 
223    let f st entity =
224       if !stage = 0 then st else
225       let frr mst = {st with mst = mst} in
226       let h mst e = process_1 {st with mst = mst} (MetaEntity e) in
227       let err dst = {st with dst = dst} in
228       let g dst e = process_1 {st with dst = dst} (CrgEntity e) in
229       let crr tst = {st with tst = tst} in
230       let d tst e = process_1 {st with tst = tst} (CrgEntity e) in
231       match entity, !old with
232          | AutEntity e, true  -> MA.meta_of_aut frr h st.mst e 
233          | AutEntity e, false -> DA.crg_of_aut err g st.dst e
234          | TxtEntity e, false -> DT.crg_of_txt crr d st.tst e
235          | _                  -> assert false
236    in
237    let st = if !L.level > 2 then count_input st entity else st in 
238    if !preprocess then process_input f st entity else f st entity
239
240 let process st name =
241    let input = type_of_input name in
242    let ich = open_in name in
243    let lexbuf = Lexing.from_channel ich in 
244    let rec aux st = match entity_of_input lexbuf input with
245       | None   -> close_in ich; st, input
246       | Some e -> aux (process_0 st e)
247    in
248    aux st
249
250 (****************************************************************************)
251
252 let main =
253 try 
254    let version_string = "Helena 0.8.1 M - February 2010" in
255    let print_version () = L.warn (version_string ^ "\n"); exit 0 in
256    let set_hierarchy s = 
257       if H.set_graph s then () else 
258          L.warn (P.sprintf "Unknown type hierarchy: %s" s)
259    in
260    let set_kernel = function
261       | "brg" -> kernel := Brg
262       | "bag" -> kernel := Bag
263       | s     -> L.warn (P.sprintf "Unknown kernel version: %s" s)
264    in
265    let set_summary i = L.level := i in
266    let set_stage i = stage := i in
267    let set_meta_file name =
268       let f och = moch := Some och in
269       ML.open_out f name
270    in
271    let set_root s = root := s in
272    let close = function
273       | None     -> ()
274       | Some och -> ML.close_out C.start och
275    in
276    let process_file name =
277       if !L.level > 0 then T.gmtime version_string;      
278       if !L.level > 1 then
279          L.warn (P.sprintf "Processing file: %s" name);
280       if !L.level > 0 then T.utime_stamp "started";
281       let base_name = Filename.chop_extension (Filename.basename name) in
282       if !meta then set_meta_file base_name;
283       O.clear_reductions ();
284       let mk_uri =
285          if !stage < 2 then Crg.mk_uri else
286          match !kernel with
287             | Brg -> Brg.mk_uri
288             | Bag -> Bag.mk_uri
289       in
290       let cover = F.concat !root base_name in
291       let st = initial_status mk_uri cover !expand !si in
292       let st, input = process st name in
293       if !L.level > 0 then T.utime_stamp "processed";
294       if !L.level > 2 then begin
295          AO.print_counters C.start st.ac;
296          if !preprocess then AO.print_process_counters C.start st.ast;
297          if !stage > 0 then MO.print_counters C.start st.mc;
298          if !stage > 1 then print_counters st;
299          if !stage > 2 then O.print_reductions ()
300       end
301    in
302    let exit () =
303       close !moch;
304       if !L.level > 0 then T.utime_stamp "at exit";
305       flush_all ()
306    in
307    let help = 
308       "Usage: helena [ -Vcgijmopqux | -Ss <number> | -hkr <string> ] <file> ...\n\n" ^
309       "Summary levels: 0 just errors (default), 1 time stamps, 2 processed file names, \
310        3 data information, 4 typing information, 5 reduction information\n\n" ^
311        "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
312    in
313    let help_S = "<number>  set summary level (see above)" in
314    let help_V = " show version information" in   
315
316    let help_c = " output conversion constraints" in
317    let help_g = " always expand global definitions" in
318    let help_h = "<string>  set type hierarchy (default: Z1)" in
319    let help_i = " show local references by index" in
320    let help_j = " show URI of processed kernel objects" in
321    let help_k = "<string>  set kernel version (default: brg)" in
322    let help_m = " output intermediate representation (HAL)" in
323    let help_o = " use old abstract language instead of crg" in   
324    let help_p = " preprocess Automath source" in
325    let help_q = " disable quotation of identifiers" in
326    let help_r = "<string>  set initial segment of URI hierarchy" in
327    let help_s = "<number>  set translation stage (see above)" in
328    let help_u = " activate sort inclusion" in
329    let help_x = " export kernel entities (XML)" in
330    L.box 0; L.box_err ();
331    at_exit exit;
332    Arg.parse [
333       ("-S", Arg.Int set_summary, help_S);
334       ("-V", Arg.Unit print_version, help_V);
335       ("-c", Arg.Set cc, help_c);
336       ("-g", Arg.Set expand, help_g);
337       ("-h", Arg.String set_hierarchy, help_h);
338       ("-i", Arg.Set O.indexes, help_i);
339       ("-j", Arg.Set progress, help_j);      
340       ("-k", Arg.String set_kernel, help_k);
341       ("-m", Arg.Set meta, help_m); 
342       ("-o", Arg.Set old, help_o);      
343       ("-p", Arg.Set preprocess, help_p);      
344       ("-q", Arg.Set AL.unquote, help_q);      
345       ("-r", Arg.String set_root, help_r);
346       ("-s", Arg.Int set_stage, help_s);
347       ("-u", Arg.Set si, help_u);
348       ("-x", Arg.Set export, help_x)
349    ] process_file help;
350 with BagT.TypeError msg -> bag_error "Type Error" msg