Standardize running code in a single function, include prelude.sh.
[goals.git] / src / main.ml
1 (* Goalfile parser
2  * Copyright (C) 2019 Richard W.M. Jones
3  * Copyright (C) 2019 Red Hat Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *)
19
20 open Printf
21
22 (* See comment in parser.mly. *)
23 let () =
24   Parser.lexer_read := Some Lexer.read;
25   Parser.eval_substitute := Some Eval.substitute
26
27 let main () =
28   (* Change directory (-C option). *)
29   Sys.chdir Cmdline.directory;
30
31   (* Parse the prelude. *)
32   let env =
33     if Cmdline.use_prelude then
34       Parse.parse_goalfile Ast.Env.empty Cmdline.prelude_gl_file
35     else
36       Ast.Env.empty in
37
38   (* Parse the input file. *)
39   let env = Parse.parse_goalfile env Cmdline.input_file in
40
41   (* Parse the command line assignments. *)
42   let env =
43     List.fold_left (
44       fun env (name, expr) ->
45         let expr = Parse.parse_expr "commandline" expr in
46         Ast.Env.add name expr env
47     ) env Cmdline.anon_vars in
48
49   (* Parse the target expressions. *)
50   let targets = List.map (Parse.parse_expr "commandline") Cmdline.targets in
51
52   (* If no target was set on the command line, use "all ()". *)
53   let targets =
54     if targets <> [] then targets
55     else [Ast.ECall (Ast.noloc, "all", [])] in
56
57   if Cmdline.debug_flag then
58     Ast.print_env stderr env;
59
60   (* Run the target expressions. *)
61   Run.run_targets env targets
62
63 let () =
64   try main ()
65   with
66     Failure msg | Sys_error msg ->
67       prerr_endline ("error: " ^ msg); exit 1