]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitac.ml
added choose_uri method to console, used by the interpreter to implement the
[helm.git] / helm / matita / matitac.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 open MatitaTypes
29
30 let message s = printf  "** MatitaC: %s\n" s; flush stdout
31 let warn s =    eprintf "** MatitaC WARNING: %s\n" s; flush stdout
32 let error s =   eprintf "** MatitaC ERROR: %s\n" s; flush stderr
33
34   (** console which prints on stdout/stderr *)
35 class tty_console =
36   object (self)
37     method clear () = ()
38     method echo_message s = message s
39     method echo_error s =  error s
40       (* TODO Zack: this method is similar to omonymous method in console,
41       * factorize it in a common super class *)
42     method wrap_exn: 'a. (unit -> 'a) -> 'a option =
43       fun f ->
44       try
45         Some (f ())
46       with exn ->
47         self#echo_error (explain exn);
48         None
49     method show ?(msg = "") () = assert false; ()
50     method choose_uri (uris: string list): string = assert false
51   end
52
53 (** {2 Initialization} *)
54
55 let arg_spec = [
56 (*   "-opt", Arg...., "set bla bla bla"; *)
57 ]
58 let usage =
59   sprintf "MatitaC v%s\nUsage: matitac [option ...] file ...\nOptions:"
60     BuildTimeConf.version
61
62 let _ =
63   Helm_registry.load_from "matita.conf.xml";
64   Http_getter.init ();
65   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
66   MatitaDb.clean_owner_environment ();
67   MatitaDb.create_owner_environment ()
68
69 let scripts =
70   let acc = ref [] in
71   let add_script fname = acc := fname :: !acc in
72   Arg.parse arg_spec add_script usage;
73   List.rev !acc
74
75 let console = new tty_console
76 let interpreter = MatitaInterpreter.interpreter ~console ()
77
78 let run_script fname =
79   message (sprintf "execution of %s started:" fname);
80   let script =
81     let ic = open_in fname in
82     let ast = 
83       try 
84         snd (CicTextualParser2.parse_script (Stream.of_channel ic)) 
85       with
86         exn -> 
87           error (explain exn); 
88           assert false (* should be something like (Unix.exit 1) *)
89     in
90     close_in ic;
91     ast
92   in
93   let rec aux = function
94     | [] -> ()  (* script is over *)
95     | DisambiguateTypes.Comment _ :: tl -> aux tl
96     | DisambiguateTypes.Command ast :: tl ->
97         let loc =
98           match ast with
99           | TacticAst.LocatedTactical (loc, _) -> loc
100           | _ -> assert false
101         in
102         let (success, _) = interpreter#evalAst ast in
103         if not success then
104           error (sprintf "%s: error at %s, aborting." fname
105             (CicAst.pp_location loc))
106         else
107           aux tl
108   in
109   aux script;
110   message (sprintf "execution of %s completed." fname)
111
112 let _ = List.iter run_script scripts
113