]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitac.ml
- added code for Coercion command, Print Env command.
[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   end
50
51 (** {2 Initialization} *)
52
53 let arg_spec = [
54 (*   "-opt", Arg...., "set bla bla bla"; *)
55 ]
56 let usage =
57   sprintf "MatitaC v%s\nUsage: matitac [option ...] file ...\nOptions:"
58     BuildTimeConf.version
59
60 let _ = Helm_registry.load_from "matita.conf.xml"
61
62 let scripts =
63   let acc = ref [] in
64   let add_script fname = acc := fname :: !acc in
65   Arg.parse arg_spec add_script usage;
66   List.rev !acc
67
68 let parserr = new MatitaDisambiguator.parserr ()
69 let dbd =
70   Mysql.quick_connect
71     ~host:(Helm_registry.get "db.host")
72     ~user:(Helm_registry.get "db.user")
73     ~database:(Helm_registry.get "db.database")
74     ()
75 let _ = MetadataDb.clean ~dbd ~owner:(Helm_registry.get "matita.owner")
76 let disambiguator =
77   new MatitaDisambiguator.disambiguator ~parserr ~dbd
78     ~chooseUris:mono_uris_callback ~chooseInterp:mono_interp_callback
79     ()
80 let console = new tty_console
81 let currentProof = (new MatitaProof.currentProof :> MatitaTypes.currentProof)
82 let interpreter =
83   new MatitaInterpreter.interpreter
84     ~disambiguator ~currentProof ~console ~dbd ()
85
86 let run_script fname =
87   message (sprintf "execution of %s started:" fname);
88   let script =
89     let ic = open_in fname in
90     let ast = 
91       try 
92         snd (CicTextualParser2.parse_script (Stream.of_channel ic)) 
93       with
94         exn -> 
95           error (explain exn); 
96           assert false (* should be something like (Unix.exit 1) *)
97     in
98     close_in ic;
99     ast
100   in
101   let rec aux = function
102     | [] -> ()  (* script is over *)
103     | DisambiguateTypes.Comment _ :: tl -> aux tl
104     | DisambiguateTypes.Command ast :: tl ->
105         let loc =
106           match ast with
107           | TacticAst.LocatedTactical (loc, _) -> loc
108           | _ -> assert false
109         in
110         let (success, _) = interpreter#evalAst ast in
111         if not success then
112           error (sprintf "%s: error at %s, aborting." fname
113             (CicAst.pp_location loc))
114         else
115           aux tl
116   in
117   aux script;
118   message (sprintf "execution of %s completed." fname)
119
120 let _ = List.iter run_script scripts
121