catala/src/lawspec/driver.ml

59 lines
2.2 KiB
OCaml
Raw Normal View History

2020-03-09 14:01:56 +03:00
(* This file is part of the Lawspec compiler, a specification language for tax and social benefits
2020-04-15 16:33:21 +03:00
computation rules. Copyright (C) 2020 Inria, contributor: Denis Merigoux
2020-03-09 14:01:56 +03:00
<denis.merigoux@inria.fr>
2020-03-08 03:52:31 +03:00
2020-03-09 14:01:56 +03:00
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
2020-03-08 03:52:31 +03:00
2020-03-09 14:01:56 +03:00
http://www.apache.org/licenses/LICENSE-2.0
2020-03-08 03:52:31 +03:00
2020-03-09 14:01:56 +03:00
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License. *)
2020-03-08 03:52:31 +03:00
open Cli
2020-03-12 20:04:27 +03:00
module I = Ir
2020-03-08 03:52:31 +03:00
(** Entry function for the executable. Returns a negative number in case of error. *)
2020-03-09 14:01:56 +03:00
let driver (source_files : string list) (debug : bool) (backend : string) (output_file : string) :
int =
2020-03-08 05:20:04 +03:00
Cli.debug_flag := debug;
2020-03-08 03:52:31 +03:00
Cli.debug_print "Reading files...";
let program = ref [] in
2020-03-09 14:01:56 +03:00
List.iter
(fun source_file ->
2020-03-08 03:52:31 +03:00
let input = open_in source_file in
Cli.debug_print (Printf.sprintf "Parsing %s" source_file);
2020-03-09 14:01:56 +03:00
let lexbuf =
Sedlex_menhir.create_lexbuf ~file:(Filename.basename source_file)
(Sedlexing.Utf8.from_channel input)
in
2020-03-08 03:52:31 +03:00
try
Parse_utils.current_file := source_file;
let commands = Sedlex_menhir.sedlex_with_menhir Lexer.lexer Parser.source_file lexbuf in
2020-03-09 14:01:56 +03:00
program := commands :: !program;
2020-03-08 03:52:31 +03:00
close_in input
2020-04-15 16:24:30 +03:00
with Errors.ParsingError msg ->
error_print msg;
close_in input;
exit (-1))
2020-03-09 14:01:56 +03:00
source_files;
2020-04-14 11:25:32 +03:00
if backend = "LaTeX" then begin
Cli.debug_print (Printf.sprintf "Weaving literate program into LaTeX");
2020-03-08 05:20:04 +03:00
let weaved_output =
String.concat "\n\n" (List.map (fun file -> Weave.ast_to_latex file) !program)
in
Cli.debug_print (Printf.sprintf "Writing to %s" output_file);
let oc = open_out output_file in
Printf.fprintf oc "%s" weaved_output;
close_out oc;
2020-04-14 11:25:32 +03:00
0
end
2020-03-08 05:20:04 +03:00
else begin
Cli.error_print (Printf.sprintf "Unkown backend: %s" backend);
1
end
2020-03-08 03:52:31 +03:00
2020-03-09 14:01:56 +03:00
let main () = Cmdliner.Term.exit @@ Cmdliner.Term.eval (Cli.lawspec_t driver, Cli.info)