2020-04-16 18:47:35 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
2021-05-27 19:56:47 +03:00
|
|
|
and social benefits computation rules. Copyright (C) 2020 Inria,
|
|
|
|
contributors: Denis Merigoux <denis.merigoux@inria.fr>, Emile Rolley
|
|
|
|
<emile.rolley@tuta.io>
|
2020-03-08 03:52:31 +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
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
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-11-23 11:22:47 +03:00
|
|
|
module Cli = Utils.Cli
|
2022-05-18 16:10:59 +03:00
|
|
|
module File = Utils.File
|
2020-11-23 11:22:47 +03:00
|
|
|
module Errors = Utils.Errors
|
2020-12-26 19:37:41 +03:00
|
|
|
module Pos = Utils.Pos
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2021-05-26 22:18:18 +03:00
|
|
|
(** Associates a {!type: Cli.backend_lang} with its string represtation. *)
|
|
|
|
let languages = ["en", Cli.En; "fr", Cli.Fr; "pl", Cli.Pl]
|
2021-05-24 17:24:45 +03:00
|
|
|
|
2022-01-02 16:53:51 +03:00
|
|
|
(** Associates a file extension with its corresponding {!type: Cli.backend_lang}
|
2021-05-24 17:24:45 +03:00
|
|
|
string representation. *)
|
2021-05-26 18:44:57 +03:00
|
|
|
let extensions = [".catala_fr", "fr"; ".catala_en", "en"; ".catala_pl", "pl"]
|
2021-05-24 17:24:45 +03:00
|
|
|
|
2021-04-22 12:57:50 +03:00
|
|
|
(** Entry function for the executable. Returns a negative number in case of
|
2022-03-16 13:44:34 +03:00
|
|
|
error. Usage: [driver source_file options]*)
|
2022-03-04 21:25:06 +03:00
|
|
|
let driver source_file (options : Cli.options) : int =
|
2020-08-07 16:29:52 +03:00
|
|
|
try
|
2022-03-04 20:32:03 +03:00
|
|
|
List.iter
|
|
|
|
(fun d ->
|
|
|
|
match Sys.is_directory d with
|
|
|
|
| true -> Plugin.load_dir d
|
|
|
|
| false -> ()
|
|
|
|
| exception Sys_error _ -> ())
|
|
|
|
options.plugins_dirs;
|
2022-03-04 21:25:06 +03:00
|
|
|
Cli.set_option_globals options;
|
2020-08-07 16:29:52 +03:00
|
|
|
Cli.debug_print "Reading files...";
|
2021-05-24 14:10:53 +03:00
|
|
|
let filename = ref "" in
|
2022-03-04 21:25:06 +03:00
|
|
|
(match source_file with
|
|
|
|
| Pos.FileName f -> filename := f
|
|
|
|
| Contents c -> Cli.contents := c);
|
|
|
|
(match options.max_prec_digits with
|
|
|
|
| None -> ()
|
|
|
|
| Some i -> Cli.max_prec_digits := i);
|
2021-05-24 16:41:59 +03:00
|
|
|
let l =
|
2022-03-04 21:25:06 +03:00
|
|
|
match options.language with
|
2021-05-24 16:41:59 +03:00
|
|
|
| Some l -> l
|
2021-05-24 17:24:45 +03:00
|
|
|
| None -> (
|
2021-05-24 16:41:59 +03:00
|
|
|
(* Try to infer the language from the intput file extension. *)
|
2021-05-24 22:02:04 +03:00
|
|
|
let ext = Filename.extension !filename in
|
|
|
|
if ext = "" then
|
2021-05-24 14:10:53 +03:00
|
|
|
Errors.raise_error
|
2022-03-08 15:04:27 +03:00
|
|
|
"No file extension found for the file '%s'. (Try to add one or to \
|
|
|
|
specify the -l flag)"
|
|
|
|
!filename;
|
2021-05-24 17:24:45 +03:00
|
|
|
try List.assoc ext extensions with Not_found -> ext)
|
2021-05-24 16:41:59 +03:00
|
|
|
in
|
|
|
|
let language =
|
2021-05-24 17:24:45 +03:00
|
|
|
try List.assoc l languages
|
|
|
|
with Not_found ->
|
2022-03-08 15:04:27 +03:00
|
|
|
Errors.raise_error
|
|
|
|
"The selected language (%s) is not supported by Catala" l
|
2020-08-07 16:29:52 +03:00
|
|
|
in
|
2021-05-26 22:18:18 +03:00
|
|
|
Cli.locale_lang := language;
|
2022-03-04 21:25:06 +03:00
|
|
|
let backend = options.backend in
|
2020-08-07 16:29:52 +03:00
|
|
|
let backend =
|
2022-03-04 20:32:03 +03:00
|
|
|
match Cli.backend_option_of_string backend with
|
|
|
|
| #Cli.backend_option_builtin as backend -> backend
|
|
|
|
| `Plugin s -> (
|
|
|
|
try `Plugin (Plugin.find s)
|
|
|
|
with Not_found ->
|
|
|
|
Errors.raise_error
|
|
|
|
"The selected backend (%s) is not supported by Catala" backend)
|
2022-03-08 18:12:25 +03:00
|
|
|
in
|
2021-05-29 15:15:23 +03:00
|
|
|
let prgm =
|
|
|
|
Surface.Parser_driver.parse_top_level_file source_file language
|
2020-08-07 16:29:52 +03:00
|
|
|
in
|
2021-05-29 15:15:23 +03:00
|
|
|
let prgm = Surface.Fill_positions.fill_pos_with_legislative_info prgm in
|
2022-05-31 17:24:37 +03:00
|
|
|
let get_output ?ext () =
|
|
|
|
match options.output_file, ext with
|
|
|
|
| Some "-", _ | None, None -> None, fun f -> f stdout
|
|
|
|
| Some f, _ -> Some f, File.with_out_channel f
|
|
|
|
| None, Some ext ->
|
|
|
|
let src =
|
|
|
|
match source_file with FileName f -> f | Contents _ -> "a"
|
|
|
|
in
|
|
|
|
let f = Filename.remove_extension src ^ ext in
|
|
|
|
Some f, File.with_out_channel f
|
|
|
|
in
|
|
|
|
let get_output_format ?ext () =
|
|
|
|
let f, with_ = get_output ?ext () in
|
|
|
|
f, fun f -> with_ (fun oc -> File.with_formatter_of_out_channel oc f)
|
|
|
|
in
|
2022-03-04 20:32:03 +03:00
|
|
|
(match backend with
|
|
|
|
| `Makefile ->
|
2020-08-07 16:29:52 +03:00
|
|
|
let backend_extensions_list = [".tex"] in
|
2020-12-26 19:37:41 +03:00
|
|
|
let source_file =
|
|
|
|
match source_file with
|
|
|
|
| FileName f -> f
|
|
|
|
| Contents _ ->
|
|
|
|
Errors.raise_error
|
|
|
|
"The Makefile backend does not work if the input is not a file"
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2022-05-31 17:24:37 +03:00
|
|
|
let output_file, with_output = get_output ~ext:".d" () in
|
|
|
|
Cli.debug_print "Writing list of dependencies to %s..."
|
|
|
|
(Option.value ~default:"stdout" output_file);
|
|
|
|
with_output @@ fun oc ->
|
2020-08-07 16:29:52 +03:00
|
|
|
Printf.fprintf oc "%s:\\\n%s\n%s:"
|
|
|
|
(String.concat "\\\n"
|
2022-05-31 17:24:37 +03:00
|
|
|
(Option.value ~default:"stdout" output_file
|
2021-05-29 15:15:23 +03:00
|
|
|
:: List.map
|
|
|
|
(fun ext -> Filename.remove_extension source_file ^ ext)
|
|
|
|
backend_extensions_list))
|
|
|
|
(String.concat "\\\n" prgm.program_source_files)
|
2022-03-04 20:32:03 +03:00
|
|
|
(String.concat "\\\n" prgm.program_source_files)
|
|
|
|
| (`Latex | `Html) as backend ->
|
2022-03-08 15:04:27 +03:00
|
|
|
Cli.debug_print "Weaving literate program into %s"
|
2022-03-04 20:32:03 +03:00
|
|
|
(match backend with `Latex -> "LaTeX" | `Html -> "HTML");
|
2022-05-31 17:24:37 +03:00
|
|
|
let output_file, with_output =
|
|
|
|
get_output_format ()
|
|
|
|
~ext:(match backend with `Latex -> ".tex" | `Html -> ".html")
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2022-05-31 17:24:37 +03:00
|
|
|
with_output (fun fmt ->
|
2022-05-18 16:10:59 +03:00
|
|
|
let weave_output =
|
|
|
|
match backend with
|
2022-05-26 20:05:06 +03:00
|
|
|
| `Latex ->
|
|
|
|
Literate.Latex.ast_to_latex language
|
|
|
|
~print_only_law:options.print_only_law
|
|
|
|
| `Html ->
|
|
|
|
Literate.Html.ast_to_html language
|
|
|
|
~print_only_law:options.print_only_law
|
2022-05-18 16:10:59 +03:00
|
|
|
in
|
2022-05-31 17:24:37 +03:00
|
|
|
Cli.debug_print "Writing to %s"
|
|
|
|
(Option.value ~default:"stdout" output_file);
|
2022-05-18 16:10:59 +03:00
|
|
|
if options.wrap_weaved_output then
|
|
|
|
match backend with
|
2022-03-04 20:32:03 +03:00
|
|
|
| `Latex ->
|
2022-05-18 16:10:59 +03:00
|
|
|
Literate.Latex.wrap_latex prgm.Surface.Ast.program_source_files
|
|
|
|
language fmt (fun fmt -> weave_output fmt prgm)
|
2022-03-04 20:32:03 +03:00
|
|
|
| `Html ->
|
2022-05-18 16:10:59 +03:00
|
|
|
Literate.Html.wrap_html prgm.Surface.Ast.program_source_files
|
|
|
|
language fmt (fun fmt -> weave_output fmt prgm)
|
2022-03-04 20:32:03 +03:00
|
|
|
else weave_output fmt prgm)
|
|
|
|
| ( `Interpret | `Typecheck | `OCaml | `Python | `Scalc | `Lcalc | `Dcalc
|
|
|
|
| `Scopelang | `Proof | `Plugin _ ) as backend -> (
|
2020-11-26 15:38:42 +03:00
|
|
|
Cli.debug_print "Name resolution...";
|
2021-05-29 15:15:23 +03:00
|
|
|
let ctxt = Surface.Name_resolution.form_context prgm in
|
2020-08-06 16:44:51 +03:00
|
|
|
let scope_uid =
|
2022-03-04 21:25:06 +03:00
|
|
|
match options.ex_scope, backend with
|
2022-03-04 20:32:03 +03:00
|
|
|
| None, `Interpret ->
|
2022-01-10 19:57:11 +03:00
|
|
|
Errors.raise_error "No scope was provided for execution."
|
2021-04-13 16:22:25 +03:00
|
|
|
| None, _ ->
|
2022-05-04 18:40:55 +03:00
|
|
|
snd
|
2021-04-13 16:22:25 +03:00
|
|
|
(try Desugared.Ast.IdentMap.choose ctxt.scope_idmap
|
2022-03-08 15:04:27 +03:00
|
|
|
with Not_found ->
|
|
|
|
Errors.raise_error "There isn't any scope inside the program.")
|
2021-01-29 01:46:39 +03:00
|
|
|
| Some name, _ -> (
|
2020-11-23 14:20:38 +03:00
|
|
|
match Desugared.Ast.IdentMap.find_opt name ctxt.scope_idmap with
|
2022-03-08 15:04:27 +03:00
|
|
|
| None ->
|
|
|
|
Errors.raise_error "There is no scope \"%s\" inside the program."
|
2022-05-04 18:40:55 +03:00
|
|
|
name
|
2020-08-06 16:44:51 +03:00
|
|
|
| Some uid -> uid)
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2020-11-26 15:38:42 +03:00
|
|
|
Cli.debug_print "Desugaring...";
|
2021-05-29 15:15:23 +03:00
|
|
|
let prgm = Surface.Desugaring.desugar_program ctxt prgm in
|
2020-11-26 15:38:42 +03:00
|
|
|
Cli.debug_print "Collecting rules...";
|
2020-11-25 12:49:53 +03:00
|
|
|
let prgm = Desugared.Desugared_to_scope.translate_program prgm in
|
2021-01-28 02:28:28 +03:00
|
|
|
match backend with
|
2022-03-04 20:32:03 +03:00
|
|
|
| `Scopelang ->
|
2022-05-31 17:24:37 +03:00
|
|
|
let _output_file, with_output = get_output_format () in
|
|
|
|
with_output @@ fun fmt ->
|
2022-03-04 20:32:03 +03:00
|
|
|
if Option.is_some options.ex_scope then
|
|
|
|
Format.fprintf fmt "%a\n"
|
|
|
|
(Scopelang.Print.format_scope ~debug:options.debug)
|
|
|
|
( scope_uid,
|
|
|
|
Scopelang.Ast.ScopeMap.find scope_uid prgm.program_scopes )
|
|
|
|
else
|
|
|
|
Format.fprintf fmt "%a\n"
|
|
|
|
(Scopelang.Print.format_program ~debug:options.debug)
|
|
|
|
prgm
|
|
|
|
| ( `Interpret | `Typecheck | `OCaml | `Python | `Scalc | `Lcalc | `Dcalc
|
|
|
|
| `Proof | `Plugin _ ) as backend -> (
|
|
|
|
Cli.debug_print "Translating to default calculus...";
|
|
|
|
let prgm, type_ordering =
|
|
|
|
Scopelang.Scope_to_dcalc.translate_program prgm
|
2021-12-10 00:59:39 +03:00
|
|
|
in
|
2022-01-07 20:36:56 +03:00
|
|
|
let prgm =
|
2022-03-04 21:25:06 +03:00
|
|
|
if options.optimize then begin
|
2022-03-04 20:32:03 +03:00
|
|
|
Cli.debug_print "Optimizing default calculus...";
|
|
|
|
Dcalc.Optimizations.optimize_program prgm
|
2022-01-07 20:36:56 +03:00
|
|
|
end
|
|
|
|
else prgm
|
|
|
|
in
|
2022-03-04 20:32:03 +03:00
|
|
|
match backend with
|
|
|
|
| `Dcalc ->
|
2022-05-31 17:24:37 +03:00
|
|
|
let _output_file, with_output = get_output_format () in
|
|
|
|
with_output @@ fun fmt ->
|
2022-03-04 20:32:03 +03:00
|
|
|
if Option.is_some options.ex_scope then
|
|
|
|
Format.fprintf fmt "%a\n"
|
|
|
|
(Dcalc.Print.format_scope ~debug:options.debug prgm.decl_ctx)
|
|
|
|
( scope_uid,
|
|
|
|
Option.get
|
|
|
|
(Dcalc.Ast.fold_left_scope_defs ~init:None
|
|
|
|
~f:(fun acc scope_def _ ->
|
|
|
|
if
|
|
|
|
Dcalc.Ast.ScopeName.compare scope_def.scope_name
|
|
|
|
scope_uid
|
|
|
|
= 0
|
|
|
|
then Some scope_def.scope_body
|
|
|
|
else acc)
|
|
|
|
prgm.scopes) )
|
|
|
|
else
|
2022-05-31 19:38:14 +03:00
|
|
|
let prgrm_dcalc_expr =
|
2022-07-22 16:49:57 +03:00
|
|
|
Bindlib.unbox
|
|
|
|
(Dcalc.Ast.build_whole_program_expr ~box_expr:Dcalc.Ast.box_expr
|
|
|
|
~make_abs:Dcalc.Ast.make_abs
|
|
|
|
~make_let_in:Dcalc.Ast.make_let_in prgm scope_uid)
|
2022-05-31 19:38:14 +03:00
|
|
|
in
|
2022-03-04 20:32:03 +03:00
|
|
|
Format.fprintf fmt "%a\n"
|
|
|
|
(Dcalc.Print.format_expr prgm.decl_ctx)
|
|
|
|
prgrm_dcalc_expr
|
|
|
|
| ( `Interpret | `Typecheck | `OCaml | `Python | `Scalc | `Lcalc
|
|
|
|
| `Proof | `Plugin _ ) as backend -> (
|
|
|
|
Cli.debug_print "Typechecking...";
|
2022-05-31 19:38:14 +03:00
|
|
|
let prgm = Dcalc.Typing.infer_types_program prgm in
|
2022-03-04 20:32:03 +03:00
|
|
|
(* Cli.debug_print (Format.asprintf "Typechecking results :@\n%a"
|
|
|
|
(Dcalc.Print.format_typ prgm.decl_ctx) typ); *)
|
|
|
|
match backend with
|
|
|
|
| `Typecheck ->
|
|
|
|
(* That's it! *)
|
|
|
|
Cli.result_print "Typechecking successful!"
|
|
|
|
| `Proof ->
|
|
|
|
let vcs =
|
|
|
|
Verification.Conditions.generate_verification_conditions prgm
|
|
|
|
(match options.ex_scope with
|
|
|
|
| None -> None
|
|
|
|
| Some _ -> Some scope_uid)
|
|
|
|
in
|
|
|
|
|
|
|
|
Verification.Solver.solve_vc prgm.decl_ctx vcs
|
|
|
|
| `Interpret ->
|
|
|
|
Cli.debug_print "Starting interpretation...";
|
2022-05-31 19:38:14 +03:00
|
|
|
let prgrm_dcalc_expr =
|
2022-07-22 16:49:57 +03:00
|
|
|
Bindlib.unbox
|
|
|
|
(Dcalc.Ast.build_whole_program_expr ~box_expr:Dcalc.Ast.box_expr
|
|
|
|
~make_abs:Dcalc.Ast.make_abs
|
|
|
|
~make_let_in:Dcalc.Ast.make_let_in prgm scope_uid)
|
2022-05-31 19:38:14 +03:00
|
|
|
in
|
2022-03-04 20:32:03 +03:00
|
|
|
let results =
|
|
|
|
Dcalc.Interpreter.interpret_program prgm.decl_ctx prgrm_dcalc_expr
|
|
|
|
in
|
|
|
|
let out_regex = Re.Pcre.regexp "\\_out$" in
|
|
|
|
let results =
|
|
|
|
List.map
|
|
|
|
(fun ((v1, v1_pos), e1) ->
|
|
|
|
let v1 =
|
|
|
|
Re.Pcre.substitute ~rex:out_regex ~subst:(fun _ -> "") v1
|
|
|
|
in
|
|
|
|
(v1, v1_pos), e1)
|
|
|
|
results
|
|
|
|
in
|
|
|
|
let results =
|
|
|
|
List.sort
|
|
|
|
(fun ((v1, _), _) ((v2, _), _) -> String.compare v1 v2)
|
|
|
|
results
|
|
|
|
in
|
|
|
|
Cli.debug_print "End of interpretation";
|
|
|
|
Cli.result_print "Computation successful!%s"
|
|
|
|
(if List.length results > 0 then " Results:" else "");
|
|
|
|
List.iter
|
|
|
|
(fun ((var, _), result) ->
|
|
|
|
Cli.result_format "@[<hov 2>%s@ =@ %a@]" var
|
|
|
|
(Dcalc.Print.format_expr ~debug:options.debug prgm.decl_ctx)
|
|
|
|
result)
|
|
|
|
results
|
|
|
|
| (`OCaml | `Python | `Lcalc | `Scalc | `Plugin _) as backend -> (
|
|
|
|
Cli.debug_print "Compiling program into lambda calculus...";
|
|
|
|
let prgm =
|
|
|
|
if options.avoid_exceptions then
|
|
|
|
Lcalc.Compile_without_exceptions.translate_program prgm
|
|
|
|
else Lcalc.Compile_with_exceptions.translate_program prgm
|
|
|
|
in
|
|
|
|
let prgm =
|
|
|
|
if options.optimize then begin
|
|
|
|
Cli.debug_print "Optimizing lambda calculus...";
|
|
|
|
Lcalc.Optimizations.optimize_program prgm
|
|
|
|
end
|
2022-07-13 13:16:29 +03:00
|
|
|
else Lcalc.Ast.untype_program prgm
|
2022-03-04 20:32:03 +03:00
|
|
|
in
|
|
|
|
let prgm =
|
|
|
|
if options.closure_conversion then (
|
|
|
|
Cli.debug_print "Performing closure conversion...";
|
|
|
|
let prgm = Lcalc.Closure_conversion.closure_conversion prgm in
|
|
|
|
let prgm = Bindlib.unbox prgm in
|
|
|
|
prgm)
|
|
|
|
else prgm
|
|
|
|
in
|
|
|
|
match backend with
|
|
|
|
| `Lcalc ->
|
2022-05-31 17:24:37 +03:00
|
|
|
let _output_file, with_output = get_output_format () in
|
|
|
|
with_output @@ fun fmt ->
|
2022-05-18 16:10:59 +03:00
|
|
|
if Option.is_some options.ex_scope then
|
|
|
|
Format.fprintf fmt "%a\n"
|
|
|
|
(Lcalc.Print.format_scope ~debug:options.debug prgm.decl_ctx)
|
|
|
|
( scope_uid,
|
|
|
|
Option.get
|
|
|
|
(Dcalc.Ast.fold_left_scope_defs ~init:None
|
|
|
|
~f:(fun acc scope_def _ ->
|
|
|
|
if
|
|
|
|
Dcalc.Ast.ScopeName.compare scope_def.scope_name
|
|
|
|
scope_uid
|
|
|
|
= 0
|
|
|
|
then Some scope_def.scope_body
|
|
|
|
else acc)
|
|
|
|
prgm.scopes) )
|
|
|
|
else
|
2022-07-22 16:49:57 +03:00
|
|
|
let prgrm_lcalc_expr =
|
|
|
|
Bindlib.unbox
|
|
|
|
(Dcalc.Ast.build_whole_program_expr
|
|
|
|
~box_expr:Lcalc.Ast.box_expr ~make_abs:Lcalc.Ast.make_abs
|
|
|
|
~make_let_in:Lcalc.Ast.make_let_in prgm scope_uid)
|
|
|
|
in
|
|
|
|
Format.fprintf fmt "%a\n"
|
|
|
|
(Lcalc.Print.format_expr prgm.decl_ctx)
|
|
|
|
prgrm_lcalc_expr
|
2022-03-04 20:32:03 +03:00
|
|
|
| (`OCaml | `Python | `Scalc | `Plugin _) as backend -> (
|
|
|
|
match backend with
|
|
|
|
| `OCaml ->
|
2022-05-31 17:24:37 +03:00
|
|
|
let output_file, with_output =
|
|
|
|
get_output_format ~ext:".ml" ()
|
|
|
|
in
|
|
|
|
with_output @@ fun fmt ->
|
2022-03-04 20:32:03 +03:00
|
|
|
Cli.debug_print "Compiling program into OCaml...";
|
2022-05-31 17:24:37 +03:00
|
|
|
Cli.debug_print "Writing to %s..."
|
|
|
|
(Option.value ~default:"stdout" output_file);
|
2022-03-04 20:32:03 +03:00
|
|
|
Lcalc.To_ocaml.format_program fmt prgm type_ordering
|
|
|
|
| `Plugin (Plugin.Lcalc p) ->
|
2022-05-31 17:24:37 +03:00
|
|
|
let output_file, _ = get_output ~ext:p.Plugin.extension () in
|
2022-03-04 20:32:03 +03:00
|
|
|
Cli.debug_print "Compiling program through backend \"%s\"..."
|
|
|
|
p.Plugin.name;
|
2022-05-31 17:24:37 +03:00
|
|
|
Cli.debug_print "Writing to %s..."
|
|
|
|
(Option.value ~default:"stdout" output_file);
|
2022-07-26 18:27:42 +03:00
|
|
|
p.Plugin.apply ~output_file ~scope:options.ex_scope prgm
|
|
|
|
type_ordering
|
2022-03-04 20:32:03 +03:00
|
|
|
| (`Python | `Scalc | `Plugin (Plugin.Scalc _)) as backend -> (
|
|
|
|
let prgm = Scalc.Compile_from_lambda.translate_program prgm in
|
|
|
|
match backend with
|
|
|
|
| `Scalc ->
|
2022-05-31 17:24:37 +03:00
|
|
|
let _output_file, with_output = get_output_format () in
|
|
|
|
with_output @@ fun fmt ->
|
2022-03-04 20:32:03 +03:00
|
|
|
if Option.is_some options.ex_scope then
|
|
|
|
Format.fprintf fmt "%a\n"
|
|
|
|
(Scalc.Print.format_scope ~debug:options.debug
|
|
|
|
prgm.decl_ctx)
|
|
|
|
(List.find
|
2022-05-18 16:10:59 +03:00
|
|
|
(fun body ->
|
|
|
|
body.Scalc.Ast.scope_body_name = scope_uid)
|
2022-03-04 20:32:03 +03:00
|
|
|
prgm.scopes)
|
|
|
|
else
|
|
|
|
Format.fprintf fmt "%a\n"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "\n\n")
|
|
|
|
(fun fmt scope ->
|
|
|
|
(Scalc.Print.format_scope prgm.decl_ctx) fmt scope))
|
|
|
|
prgm.scopes
|
|
|
|
| `Python ->
|
2022-05-31 17:24:37 +03:00
|
|
|
let output_file, with_output =
|
|
|
|
get_output_format ~ext:".py" ()
|
|
|
|
in
|
2022-03-04 20:32:03 +03:00
|
|
|
Cli.debug_print "Compiling program into Python...";
|
2022-05-31 17:24:37 +03:00
|
|
|
Cli.debug_print "Writing to %s..."
|
|
|
|
(Option.value ~default:"stdout" output_file);
|
|
|
|
with_output @@ fun fmt ->
|
2022-03-04 20:32:03 +03:00
|
|
|
Scalc.To_python.format_program fmt prgm type_ordering
|
|
|
|
| `Plugin (Plugin.Lcalc _) -> assert false
|
|
|
|
| `Plugin (Plugin.Scalc p) ->
|
2022-05-31 17:24:37 +03:00
|
|
|
let output_file, _ = get_output ~ext:p.Plugin.extension () in
|
2022-03-04 20:32:03 +03:00
|
|
|
Cli.debug_print "Compiling program through backend \"%s\"..."
|
|
|
|
p.Plugin.name;
|
2022-05-31 17:24:37 +03:00
|
|
|
Cli.debug_print "Writing to %s..."
|
|
|
|
(Option.value ~default:"stdout" output_file);
|
2022-07-26 18:27:42 +03:00
|
|
|
p.Plugin.apply ~output_file ~scope:options.ex_scope prgm
|
|
|
|
type_ordering)))))));
|
2022-03-04 20:32:03 +03:00
|
|
|
0
|
2022-02-01 17:41:53 +03:00
|
|
|
with
|
|
|
|
| Errors.StructuredError (msg, pos) ->
|
2022-07-05 11:29:24 +03:00
|
|
|
let bt = Printexc.get_raw_backtrace () in
|
2022-03-08 15:04:27 +03:00
|
|
|
Cli.error_print "%s" (Errors.print_structured_error msg pos);
|
2022-07-05 11:29:24 +03:00
|
|
|
if Printexc.backtrace_status () then Printexc.print_raw_backtrace stderr bt;
|
2022-02-01 17:41:53 +03:00
|
|
|
-1
|
|
|
|
| Sys_error msg ->
|
2022-07-05 11:29:24 +03:00
|
|
|
let bt = Printexc.get_raw_backtrace () in
|
2022-03-08 15:04:27 +03:00
|
|
|
Cli.error_print "System error: %s" msg;
|
2022-07-05 11:29:24 +03:00
|
|
|
if Printexc.backtrace_status () then Printexc.print_raw_backtrace stderr bt;
|
2022-02-01 17:41:53 +03:00
|
|
|
-1
|
2020-03-08 03:52:31 +03:00
|
|
|
|
2020-12-26 19:37:41 +03:00
|
|
|
let main () =
|
2022-02-21 16:49:58 +03:00
|
|
|
let return_code =
|
2022-05-04 18:37:03 +03:00
|
|
|
Cmdliner.Cmd.eval'
|
|
|
|
(Cmdliner.Cmd.v Cli.info (Cli.catala_t (fun f -> driver (FileName f))))
|
2022-02-21 16:49:58 +03:00
|
|
|
in
|
2022-05-04 18:37:03 +03:00
|
|
|
exit return_code
|
2022-03-04 20:32:03 +03:00
|
|
|
|
|
|
|
(* Export module PluginAPI, hide parent module Plugin *)
|
|
|
|
module Plugin = Plugin.PluginAPI
|