2022-03-08 17:03:14 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2020 Inria, contributor:
|
|
|
|
Denis Merigoux <denis.merigoux@inria.fr>
|
2021-01-28 02:28:28 +03:00
|
|
|
|
2022-03-08 17:03:14 +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
|
2021-01-28 02:28:28 +03:00
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
2022-03-08 17:03:14 +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
|
2021-01-28 02:28:28 +03:00
|
|
|
the License. *)
|
|
|
|
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2022-08-12 23:42:39 +03:00
|
|
|
open Shared_ast
|
2021-01-28 02:28:28 +03:00
|
|
|
open Ast
|
2021-02-12 19:20:14 +03:00
|
|
|
module D = Dcalc.Ast
|
2021-01-28 02:28:28 +03:00
|
|
|
|
2024-04-26 19:31:26 +03:00
|
|
|
let format_string_list (fmt : Format.formatter) (uids : string list) : unit =
|
|
|
|
let sanitize_quotes = Re.compile (Re.char '"') in
|
|
|
|
Format.fprintf fmt "@[<hov 2>[%a]@]"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ";@ ")
|
|
|
|
(fun fmt info ->
|
|
|
|
Format.fprintf fmt "\"%s\""
|
|
|
|
(Re.replace sanitize_quotes ~f:(fun _ -> "\\\"") info)))
|
|
|
|
uids
|
|
|
|
|
|
|
|
let format_pos ppf pos =
|
|
|
|
Format.fprintf ppf
|
|
|
|
"@[<hov 1>{filename=%S;@ start_line=%d; start_column=%d;@ end_line=%d; \
|
|
|
|
end_column=%d;@ law_headings=%a}@]"
|
|
|
|
(Pos.get_file pos) (Pos.get_start_line pos) (Pos.get_start_column pos)
|
|
|
|
(Pos.get_end_line pos) (Pos.get_end_column pos) format_string_list
|
|
|
|
(Pos.get_law_info pos)
|
|
|
|
|
2023-05-17 16:44:57 +03:00
|
|
|
let format_lit (fmt : Format.formatter) (l : lit Mark.pos) : unit =
|
|
|
|
match Mark.remove l with
|
2022-08-17 17:14:14 +03:00
|
|
|
| LBool b -> Print.lit fmt (LBool b)
|
2022-03-08 17:03:14 +03:00
|
|
|
| LInt i ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Format.fprintf fmt "integer_of_string@ \"%s\"" (Runtime.integer_to_string i)
|
2022-08-17 17:14:14 +03:00
|
|
|
| LUnit -> Print.lit fmt LUnit
|
|
|
|
| LRat i -> Format.fprintf fmt "decimal_of_string \"%a\"" Print.lit (LRat i)
|
2021-03-05 21:16:56 +03:00
|
|
|
| LMoney e ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Format.fprintf fmt "money_of_cents_string@ \"%s\""
|
|
|
|
(Runtime.integer_to_string (Runtime.money_to_cents e))
|
2021-01-29 01:46:39 +03:00
|
|
|
| LDate d ->
|
2022-07-22 20:02:09 +03:00
|
|
|
Format.fprintf fmt "date_of_numbers (%d) (%d) (%d)"
|
2022-05-04 18:40:55 +03:00
|
|
|
(Runtime.integer_to_int (Runtime.year_of_date d))
|
|
|
|
(Runtime.integer_to_int (Runtime.month_number_of_date d))
|
|
|
|
(Runtime.integer_to_int (Runtime.day_of_month_of_date d))
|
2021-01-29 01:46:39 +03:00
|
|
|
| LDuration d ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let years, months, days = Runtime.duration_to_years_months_days d in
|
2022-07-22 20:02:09 +03:00
|
|
|
Format.fprintf fmt "duration_of_numbers (%d) (%d) (%d)" years months days
|
2021-01-28 02:28:28 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let format_uid_list (fmt : Format.formatter) (uids : Uid.MarkedString.info list)
|
|
|
|
: unit =
|
2021-04-03 18:58:31 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>[%a]@]"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ";@ ")
|
2022-03-08 17:03:14 +03:00
|
|
|
(fun fmt info ->
|
2022-11-21 12:46:17 +03:00
|
|
|
Format.fprintf fmt "\"%a\"" Uid.MarkedString.format info))
|
2021-04-03 18:58:31 +03:00
|
|
|
uids
|
|
|
|
|
2023-07-11 18:10:00 +03:00
|
|
|
(* list taken from
|
|
|
|
http://caml.inria.fr/pub/docs/manual-ocaml/lex.html#sss:keywords *)
|
|
|
|
let ocaml_keywords =
|
|
|
|
[
|
|
|
|
"and";
|
|
|
|
"as";
|
|
|
|
"assert";
|
|
|
|
"asr";
|
|
|
|
"begin";
|
|
|
|
"class";
|
|
|
|
"constraint";
|
|
|
|
"do";
|
|
|
|
"done";
|
|
|
|
"downto";
|
|
|
|
"else";
|
|
|
|
"end";
|
|
|
|
"exception";
|
|
|
|
"external";
|
|
|
|
"false";
|
|
|
|
"for";
|
|
|
|
"fun";
|
|
|
|
"function";
|
|
|
|
"functor";
|
|
|
|
"if";
|
|
|
|
"in";
|
|
|
|
"include";
|
|
|
|
"inherit";
|
|
|
|
"initializer";
|
|
|
|
"land";
|
|
|
|
"lazy";
|
|
|
|
"let";
|
|
|
|
"lor";
|
|
|
|
"lsl";
|
|
|
|
"lsr";
|
|
|
|
"lxor";
|
|
|
|
"match";
|
|
|
|
"method";
|
|
|
|
"mod";
|
|
|
|
"module";
|
|
|
|
"mutable";
|
|
|
|
"new";
|
|
|
|
"nonrec";
|
|
|
|
"object";
|
|
|
|
"of";
|
|
|
|
"open";
|
|
|
|
"or";
|
|
|
|
"private";
|
|
|
|
"rec";
|
|
|
|
"sig";
|
|
|
|
"struct";
|
|
|
|
"then";
|
|
|
|
"to";
|
|
|
|
"true";
|
|
|
|
"try";
|
|
|
|
"type";
|
|
|
|
"val";
|
|
|
|
"virtual";
|
|
|
|
"when";
|
|
|
|
"while";
|
|
|
|
"with";
|
|
|
|
"Stdlib";
|
|
|
|
"Runtime";
|
|
|
|
"Oper";
|
|
|
|
]
|
|
|
|
|
|
|
|
let ocaml_keywords_set = String.Set.of_list ocaml_keywords
|
|
|
|
|
2021-03-10 18:50:54 +03:00
|
|
|
let avoid_keywords (s : string) : string =
|
2023-07-11 18:10:00 +03:00
|
|
|
if String.Set.mem s ocaml_keywords_set then s ^ "_user" else s
|
2023-04-19 19:26:50 +03:00
|
|
|
(* Fixme: this could cause clashes if the user program contains both e.g. [new]
|
|
|
|
and [new_user] *)
|
2021-03-10 18:50:54 +03:00
|
|
|
|
2024-03-05 19:54:53 +03:00
|
|
|
let ppclean fmt str =
|
|
|
|
str |> String.to_ascii |> avoid_keywords |> Format.pp_print_string fmt
|
|
|
|
|
|
|
|
let ppsnake fmt str =
|
|
|
|
str
|
2022-11-21 13:17:42 +03:00
|
|
|
|> String.to_ascii
|
|
|
|
|> String.to_snake_case
|
2022-08-03 18:07:35 +03:00
|
|
|
|> avoid_keywords
|
2024-03-05 19:54:53 +03:00
|
|
|
|> Format.pp_print_string fmt
|
|
|
|
|
|
|
|
let format_struct_name (fmt : Format.formatter) (v : StructName.t) : unit =
|
|
|
|
(match StructName.path v with
|
|
|
|
| [] -> ()
|
|
|
|
| path ->
|
|
|
|
ppclean fmt (Uid.Path.to_string path);
|
|
|
|
Format.pp_print_char fmt '.');
|
|
|
|
ppsnake fmt (Mark.remove (StructName.get_info v))
|
2022-07-19 18:59:45 +03:00
|
|
|
|
2022-07-20 19:24:54 +03:00
|
|
|
let format_to_module_name
|
|
|
|
(fmt : Format.formatter)
|
2022-08-12 23:42:39 +03:00
|
|
|
(name : [< `Ename of EnumName.t | `Sname of StructName.t ]) =
|
2024-03-05 19:54:53 +03:00
|
|
|
ppclean fmt
|
|
|
|
(match name with
|
|
|
|
| `Ename v -> EnumName.to_string v
|
|
|
|
| `Sname v -> StructName.to_string v)
|
2021-01-28 20:30:01 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let format_struct_field_name
|
2022-05-04 19:40:28 +03:00
|
|
|
(fmt : Format.formatter)
|
2022-11-21 12:12:45 +03:00
|
|
|
((sname_opt, v) : StructName.t option * StructField.t) : unit =
|
2024-03-05 19:54:53 +03:00
|
|
|
Option.iter
|
|
|
|
(fun sname ->
|
|
|
|
format_to_module_name fmt (`Sname sname);
|
|
|
|
Format.pp_print_char fmt '.')
|
|
|
|
sname_opt;
|
|
|
|
ppclean fmt (StructField.to_string v)
|
2021-01-28 02:28:28 +03:00
|
|
|
|
2022-08-16 11:04:01 +03:00
|
|
|
let format_enum_name (fmt : Format.formatter) (v : EnumName.t) : unit =
|
2024-03-05 19:54:53 +03:00
|
|
|
(match EnumName.path v with
|
|
|
|
| [] -> ()
|
|
|
|
| path ->
|
|
|
|
ppclean fmt (Uid.Path.to_string path);
|
|
|
|
Format.pp_print_char fmt '.');
|
|
|
|
ppsnake fmt (Mark.remove (EnumName.get_info v))
|
2021-01-28 20:30:01 +03:00
|
|
|
|
2022-08-16 11:04:01 +03:00
|
|
|
let format_enum_cons_name (fmt : Format.formatter) (v : EnumConstructor.t) :
|
|
|
|
unit =
|
2024-03-05 19:54:53 +03:00
|
|
|
ppclean fmt (EnumConstructor.to_string v)
|
2021-01-28 02:28:28 +03:00
|
|
|
|
2022-08-25 20:46:13 +03:00
|
|
|
let rec typ_embedding_name (fmt : Format.formatter) (ty : typ) : unit =
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove ty with
|
2024-03-05 19:54:53 +03:00
|
|
|
| TLit TUnit -> Format.pp_print_string fmt "embed_unit"
|
|
|
|
| TLit TBool -> Format.pp_print_string fmt "embed_bool"
|
|
|
|
| TLit TInt -> Format.pp_print_string fmt "embed_integer"
|
|
|
|
| TLit TRat -> Format.pp_print_string fmt "embed_decimal"
|
|
|
|
| TLit TMoney -> Format.pp_print_string fmt "embed_money"
|
|
|
|
| TLit TDate -> Format.pp_print_string fmt "embed_date"
|
|
|
|
| TLit TDuration -> Format.pp_print_string fmt "embed_duration"
|
|
|
|
| TStruct s_name ->
|
|
|
|
Format.fprintf fmt "%a%sembed_%a" ppclean
|
|
|
|
(Uid.Path.to_string (StructName.path s_name))
|
|
|
|
(if StructName.path s_name = [] then "" else ".")
|
|
|
|
ppsnake
|
|
|
|
(Uid.MarkedString.to_string (StructName.get_info s_name))
|
|
|
|
| TEnum e_name ->
|
|
|
|
Format.fprintf fmt "%a%sembed_%a" ppclean
|
|
|
|
(Uid.Path.to_string (EnumName.path e_name))
|
|
|
|
(if EnumName.path e_name = [] then "" else ".")
|
|
|
|
ppsnake
|
|
|
|
(Uid.MarkedString.to_string (EnumName.get_info e_name))
|
2022-08-12 23:42:39 +03:00
|
|
|
| TArray ty -> Format.fprintf fmt "embed_array (%a)" typ_embedding_name ty
|
2024-03-05 19:54:53 +03:00
|
|
|
| _ -> Format.pp_print_string fmt "unembeddable"
|
2021-04-09 00:23:10 +03:00
|
|
|
|
2022-08-25 18:29:00 +03:00
|
|
|
let typ_needs_parens (e : typ) : bool =
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e with TArrow _ | TArray _ -> true | _ -> false
|
2021-02-01 11:54:48 +03:00
|
|
|
|
2022-08-25 18:29:00 +03:00
|
|
|
let rec format_typ (fmt : Format.formatter) (typ : typ) : unit =
|
|
|
|
let format_typ_with_parens (fmt : Format.formatter) (t : typ) =
|
2021-02-01 11:54:48 +03:00
|
|
|
if typ_needs_parens t then Format.fprintf fmt "(%a)" format_typ t
|
|
|
|
else Format.fprintf fmt "%a" format_typ t
|
2021-01-28 02:28:28 +03:00
|
|
|
in
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove typ with
|
2022-08-17 17:14:14 +03:00
|
|
|
| TLit l -> Format.fprintf fmt "%a" Print.tlit l
|
2022-08-23 16:23:52 +03:00
|
|
|
| TTuple ts ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>(%a)@]"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@ *@ ")
|
|
|
|
format_typ_with_parens)
|
|
|
|
ts
|
2022-08-23 16:23:52 +03:00
|
|
|
| TStruct s -> Format.fprintf fmt "%a.t" format_to_module_name (`Sname s)
|
|
|
|
| TOption t ->
|
2023-11-16 17:45:06 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>(%a)@] %a.t" format_typ_with_parens t
|
2023-11-27 12:47:43 +03:00
|
|
|
format_to_module_name (`Ename Expr.option_enum)
|
2023-11-03 17:09:02 +03:00
|
|
|
| TDefault t -> format_typ fmt t
|
2022-08-23 16:23:52 +03:00
|
|
|
| TEnum e -> Format.fprintf fmt "%a.t" format_to_module_name (`Ename e)
|
2021-01-28 02:28:28 +03:00
|
|
|
| TArrow (t1, t2) ->
|
2023-02-20 19:58:29 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a@]"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt " ->@ ")
|
|
|
|
format_typ_with_parens)
|
|
|
|
(t1 @ [t2])
|
2021-02-01 11:54:48 +03:00
|
|
|
| TArray t1 -> Format.fprintf fmt "@[%a@ array@]" format_typ_with_parens t1
|
2021-01-28 02:28:28 +03:00
|
|
|
| TAny -> Format.fprintf fmt "_"
|
2023-06-15 17:33:14 +03:00
|
|
|
| TClosureEnv -> failwith "unimplemented!"
|
2021-01-28 02:28:28 +03:00
|
|
|
|
2023-08-10 17:52:39 +03:00
|
|
|
let format_var_str (fmt : Format.formatter) (v : string) : unit =
|
2023-08-16 01:04:45 +03:00
|
|
|
let lowercase_name = String.to_snake_case (String.to_ascii v) in
|
2021-01-28 15:58:59 +03:00
|
|
|
let lowercase_name =
|
2022-03-08 17:03:14 +03:00
|
|
|
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "\\.")
|
|
|
|
~subst:(fun _ -> "_dot_")
|
|
|
|
lowercase_name
|
2021-01-28 15:58:59 +03:00
|
|
|
in
|
2023-07-11 18:10:00 +03:00
|
|
|
let lowercase_name = String.to_ascii lowercase_name in
|
2022-02-04 16:30:42 +03:00
|
|
|
if
|
2022-05-04 16:48:03 +03:00
|
|
|
List.mem lowercase_name ["handle_default"; "handle_default_opt"]
|
2023-08-10 17:52:39 +03:00
|
|
|
(* O_O *)
|
|
|
|
|| String.begins_with_uppercase v
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
then Format.pp_print_string fmt lowercase_name
|
|
|
|
else if lowercase_name = "_" then Format.pp_print_string fmt lowercase_name
|
2023-04-14 11:50:28 +03:00
|
|
|
else Format.fprintf fmt "%s_" lowercase_name
|
2021-01-28 02:28:28 +03:00
|
|
|
|
2023-08-10 17:52:39 +03:00
|
|
|
let format_var (fmt : Format.formatter) (v : 'm Var.t) : unit =
|
|
|
|
format_var_str fmt (Bindlib.name_of v)
|
|
|
|
|
2022-08-25 17:35:08 +03:00
|
|
|
let needs_parens (e : 'm expr) : bool =
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e with
|
2023-12-18 12:25:00 +03:00
|
|
|
| EApp { f = EAbs _, _; _ } | ELit (LBool _ | LUnit) | EVar _ | ETuple _ ->
|
2022-05-04 18:40:55 +03:00
|
|
|
false
|
2021-02-01 11:54:48 +03:00
|
|
|
| _ -> true
|
2021-01-28 02:28:28 +03:00
|
|
|
|
2022-08-25 20:46:13 +03:00
|
|
|
let rec format_expr (ctx : decl_ctx) (fmt : Format.formatter) (e : 'm expr) :
|
|
|
|
unit =
|
2021-01-28 02:28:28 +03:00
|
|
|
let format_expr = format_expr ctx in
|
2022-08-25 17:35:08 +03:00
|
|
|
let format_with_parens (fmt : Format.formatter) (e : 'm expr) =
|
2021-01-28 02:28:28 +03:00
|
|
|
if needs_parens e then Format.fprintf fmt "(%a)" format_expr e
|
|
|
|
else Format.fprintf fmt "%a" format_expr e
|
|
|
|
in
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e with
|
2022-06-03 17:40:03 +03:00
|
|
|
| EVar v -> Format.fprintf fmt "%a" format_var v
|
2023-08-30 18:49:29 +03:00
|
|
|
| EExternal { name } -> (
|
2023-08-16 01:04:45 +03:00
|
|
|
(* FIXME: this is wrong in general !! We assume the idents exposed by the
|
|
|
|
module depend only on the original name, while they actually get through
|
|
|
|
Bindlib and may have been renamed. A correct implem could use the runtime
|
|
|
|
registration used by the interpreter, but that would be distasteful and
|
|
|
|
incur a penalty ; or we would need to reproduce the same structure as in
|
|
|
|
the original module to ensure that bindlib performs the exact same
|
|
|
|
renamings ; or finally we could normalise the names at generation time
|
|
|
|
(either at toplevel or in a dedicated submodule ?) *)
|
2023-08-31 11:07:57 +03:00
|
|
|
let path =
|
2023-08-30 19:33:24 +03:00
|
|
|
match Mark.remove name with
|
2023-08-31 11:07:57 +03:00
|
|
|
| External_value name -> TopdefName.path name
|
|
|
|
| External_scope name -> ScopeName.path name
|
|
|
|
in
|
|
|
|
Uid.Path.format fmt path;
|
|
|
|
match Mark.remove name with
|
|
|
|
| External_value name ->
|
|
|
|
format_var_str fmt (Mark.remove (TopdefName.get_info name))
|
|
|
|
| External_scope name ->
|
|
|
|
format_var_str fmt (Mark.remove (ScopeName.get_info name)))
|
2022-11-17 19:13:35 +03:00
|
|
|
| ETuple es ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>(%a)@]"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
|
|
|
|
(fun fmt e -> Format.fprintf fmt "%a" format_with_parens e))
|
|
|
|
es
|
2022-11-17 19:13:35 +03:00
|
|
|
| EStruct { name = s; fields = es } ->
|
2022-11-21 12:12:45 +03:00
|
|
|
if StructField.Map.is_empty es then Format.fprintf fmt "()"
|
2022-05-04 18:40:55 +03:00
|
|
|
else
|
|
|
|
Format.fprintf fmt "{@[<hov 2>%a@]}"
|
2021-01-28 02:28:28 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ";@ ")
|
2022-11-17 19:13:35 +03:00
|
|
|
(fun fmt (struct_field, e) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a =@ %a@]" format_struct_field_name
|
2022-07-12 15:10:53 +03:00
|
|
|
(Some s, struct_field) format_with_parens e))
|
2022-11-21 12:12:45 +03:00
|
|
|
(StructField.Map.bindings es)
|
2022-05-04 18:40:55 +03:00
|
|
|
| EArray es ->
|
|
|
|
Format.fprintf fmt "@[<hov 2>[|%a|]@]"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ";@ ")
|
|
|
|
(fun fmt e -> Format.fprintf fmt "%a" format_with_parens e))
|
|
|
|
es
|
2022-11-17 19:13:35 +03:00
|
|
|
| ETupleAccess { e; index; size } ->
|
|
|
|
Format.fprintf fmt "let@ %a@ = %a@ in@ x"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
(fun fmt i ->
|
|
|
|
Format.pp_print_string fmt (if i = index then "x" else "_")))
|
2022-11-17 19:13:35 +03:00
|
|
|
(List.init size Fun.id) format_with_parens e
|
|
|
|
| EStructAccess { e; field; name } ->
|
|
|
|
Format.fprintf fmt "%a.%a" format_with_parens e format_struct_field_name
|
|
|
|
(Some name, field)
|
|
|
|
| EInj { e; cons; name } ->
|
|
|
|
Format.fprintf fmt "@[<hov 2>%a.%a@ %a@]" format_to_module_name
|
|
|
|
(`Ename name) format_enum_cons_name cons format_with_parens e
|
|
|
|
| EMatch { e; cases; name } ->
|
2022-08-04 18:37:27 +03:00
|
|
|
Format.fprintf fmt "@[<hv>@[<hov 2>match@ %a@]@ with@\n| %a@]"
|
|
|
|
format_with_parens e
|
2022-05-04 18:40:55 +03:00
|
|
|
(Format.pp_print_list
|
2022-08-04 18:37:27 +03:00
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@ | ")
|
2022-11-17 19:13:35 +03:00
|
|
|
(fun fmt (c, e) ->
|
2022-08-04 18:37:27 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a.%a %a@]" format_to_module_name
|
2022-11-17 19:13:35 +03:00
|
|
|
(`Ename name) format_enum_cons_name c
|
2022-05-04 18:40:55 +03:00
|
|
|
(fun fmt e ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e with
|
2022-11-17 19:13:35 +03:00
|
|
|
| EAbs { binder; _ } ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let xs, body = Bindlib.unmbind binder in
|
2022-08-04 18:37:27 +03:00
|
|
|
Format.fprintf fmt "%a ->@ %a"
|
2022-05-04 18:40:55 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@,")
|
|
|
|
(fun fmt x -> Format.fprintf fmt "%a" format_var x))
|
|
|
|
(Array.to_list xs) format_with_parens body
|
|
|
|
| _ -> assert false
|
|
|
|
(* should not happen *))
|
|
|
|
e))
|
2022-11-21 12:12:45 +03:00
|
|
|
(EnumConstructor.Map.bindings cases)
|
2023-05-17 16:44:57 +03:00
|
|
|
| ELit l -> Format.fprintf fmt "%a" format_lit (Mark.add (Expr.pos e) l)
|
2023-12-18 12:25:00 +03:00
|
|
|
| EApp { f = EAbs { binder; tys }, _; args; _ } ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let xs, body = Bindlib.unmbind binder in
|
2022-11-17 19:13:35 +03:00
|
|
|
let xs_tau = List.map2 (fun x tau -> x, tau) (Array.to_list xs) tys in
|
2022-05-04 16:48:03 +03:00
|
|
|
let xs_tau_arg = List.map2 (fun (x, tau) arg -> x, tau, arg) xs_tau args in
|
2022-05-04 18:40:55 +03:00
|
|
|
Format.fprintf fmt "(%a%a)"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "")
|
|
|
|
(fun fmt (x, tau, arg) ->
|
|
|
|
Format.fprintf fmt "@[<hov 2>let@ %a@ :@ %a@ =@ %a@]@ in@\n"
|
|
|
|
format_var x format_typ tau format_with_parens arg))
|
|
|
|
xs_tau_arg format_with_parens body
|
2022-11-17 19:13:35 +03:00
|
|
|
| EAbs { binder; tys } ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let xs, body = Bindlib.unmbind binder in
|
2022-11-17 19:13:35 +03:00
|
|
|
let xs_tau = List.map2 (fun x tau -> x, tau) (Array.to_list xs) tys in
|
2022-05-04 18:40:55 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>fun@ %a ->@ %a@]"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@ ")
|
|
|
|
(fun fmt (x, tau) ->
|
|
|
|
Format.fprintf fmt "@[<hov 2>(%a:@ %a)@]" format_var x format_typ tau))
|
|
|
|
xs_tau format_expr body
|
2022-11-17 19:13:35 +03:00
|
|
|
| EApp
|
|
|
|
{
|
2023-12-18 12:25:00 +03:00
|
|
|
f = EAppOp { op = Log (BeginCall, info); args = [f]; _ }, _;
|
2022-11-17 19:13:35 +03:00
|
|
|
args = [arg];
|
2023-12-18 12:25:00 +03:00
|
|
|
_;
|
2022-11-17 19:13:35 +03:00
|
|
|
}
|
2024-03-15 16:23:30 +03:00
|
|
|
when Global.options.trace ->
|
2022-06-15 15:34:15 +03:00
|
|
|
Format.fprintf fmt "(log_begin_call@ %a@ %a)@ %a" format_uid_list info
|
2022-05-04 18:40:55 +03:00
|
|
|
format_with_parens f format_with_parens arg
|
2023-12-18 12:25:00 +03:00
|
|
|
| EAppOp { op = Log (VarDef var_def_info, info); args = [arg1]; _ }
|
2024-03-15 16:23:30 +03:00
|
|
|
when Global.options.trace ->
|
2023-05-26 17:54:52 +03:00
|
|
|
Format.fprintf fmt
|
|
|
|
"(log_variable_definition@ %a@ {io_input=%s;@ io_output=%b}@ (%a)@ %a)"
|
|
|
|
format_uid_list info
|
|
|
|
(match var_def_info.log_io_input with
|
|
|
|
| NoInput -> "NoInput"
|
|
|
|
| OnlyInput -> "OnlyInput"
|
|
|
|
| Reentrant -> "Reentrant")
|
|
|
|
var_def_info.log_io_output typ_embedding_name
|
|
|
|
(var_def_info.log_typ, Pos.no_pos)
|
|
|
|
format_with_parens arg1
|
2023-12-18 12:25:00 +03:00
|
|
|
| EAppOp { op = Log (PosRecordIfTrueBool, _); args = [arg1]; _ }
|
2024-03-15 16:23:30 +03:00
|
|
|
when Global.options.trace ->
|
2023-12-18 12:25:00 +03:00
|
|
|
let pos = Expr.pos e in
|
2022-05-04 18:40:55 +03:00
|
|
|
Format.fprintf fmt
|
|
|
|
"(log_decision_taken@ @[<hov 2>{filename = \"%s\";@ start_line=%d;@ \
|
|
|
|
start_column=%d;@ end_line=%d; end_column=%d;@ law_headings=%a}@]@ %a)"
|
|
|
|
(Pos.get_file pos) (Pos.get_start_line pos) (Pos.get_start_column pos)
|
|
|
|
(Pos.get_end_line pos) (Pos.get_end_column pos) format_string_list
|
|
|
|
(Pos.get_law_info pos) format_with_parens arg1
|
2024-03-19 17:23:06 +03:00
|
|
|
| EAppOp { op = Log (EndCall, info); args = [arg1]; _ }
|
|
|
|
when Global.options.trace ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Format.fprintf fmt "(log_end_call@ %a@ %a)" format_uid_list info
|
|
|
|
format_with_parens arg1
|
2023-12-18 12:25:00 +03:00
|
|
|
| EAppOp { op = Log _; args = [arg1]; _ } ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Format.fprintf fmt "%a" format_with_parens arg1
|
2024-04-29 17:09:38 +03:00
|
|
|
| EAppOp
|
|
|
|
{
|
|
|
|
op = (HandleDefault | HandleDefaultOpt) as op;
|
|
|
|
args = (EArray excs, _) :: _ as args;
|
|
|
|
_;
|
|
|
|
} ->
|
|
|
|
let pos = List.map Expr.pos excs in
|
|
|
|
Format.fprintf fmt "@[<hov 2>%s@ [|%a|]@ %a@]"
|
2023-05-02 17:46:36 +03:00
|
|
|
(Print.operator_to_string op)
|
2022-07-29 18:04:34 +03:00
|
|
|
(Format.pp_print_list
|
2024-04-29 17:09:38 +03:00
|
|
|
~pp_sep:(fun ppf () -> Format.fprintf ppf ";@ ")
|
|
|
|
format_pos)
|
|
|
|
pos
|
|
|
|
(Format.pp_print_list ~pp_sep:Format.pp_print_space format_with_parens)
|
2022-07-29 18:04:34 +03:00
|
|
|
args
|
2023-12-18 12:25:00 +03:00
|
|
|
| EApp { f; args; _ } ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ %a@]" format_with_parens f
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@ ")
|
|
|
|
format_with_parens)
|
|
|
|
args
|
2022-11-17 19:13:35 +03:00
|
|
|
| EIfThenElse { cond; etrue; efalse } ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Format.fprintf fmt
|
|
|
|
"@[<hov 2> if@ @[<hov 2>%a@]@ then@ @[<hov 2>%a@]@ else@ @[<hov 2>%a@]@]"
|
2022-11-17 19:13:35 +03:00
|
|
|
format_with_parens cond format_with_parens etrue format_with_parens efalse
|
2023-12-18 12:25:00 +03:00
|
|
|
| EAppOp { op; args; _ } ->
|
2024-04-26 19:31:26 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%s@ %t%a@]" (Operator.name op)
|
|
|
|
(fun ppf ->
|
|
|
|
match op with
|
|
|
|
| Map2 | Div_int_int | Div_rat_rat | Div_mon_mon | Div_mon_rat
|
|
|
|
| Div_dur_dur | Lt_dur_dur | Lte_dur_dur | Gt_dur_dur | Gte_dur_dur
|
|
|
|
| Eq_dur_dur ->
|
|
|
|
Format.fprintf ppf "%a@ " format_pos (Expr.pos e)
|
|
|
|
| _ -> ())
|
2023-12-18 12:25:00 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@ ")
|
|
|
|
format_with_parens)
|
|
|
|
args
|
2021-01-30 19:54:05 +03:00
|
|
|
| EAssert e' ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Format.fprintf fmt
|
2024-04-29 17:09:38 +03:00
|
|
|
"@[<hov 2>if@ %a@ then@ ()@ else@ raise (Error (%s, [%a]))@]"
|
2022-05-04 18:40:55 +03:00
|
|
|
format_with_parens e'
|
2024-04-26 19:31:26 +03:00
|
|
|
Runtime.(error_to_string AssertionFailed)
|
|
|
|
format_pos (Expr.pos e')
|
|
|
|
| EFatalError er ->
|
2024-04-29 17:09:38 +03:00
|
|
|
Format.fprintf fmt "raise@ (Runtime_ocaml.Runtime.Error (%a, [%a]))"
|
2024-04-26 19:31:26 +03:00
|
|
|
Print.runtime_error er format_pos (Expr.pos e)
|
2024-04-29 14:42:40 +03:00
|
|
|
| ERaiseEmpty -> Format.fprintf fmt "raise Empty"
|
2024-04-26 19:31:26 +03:00
|
|
|
| ECatchEmpty { body; handler } ->
|
2024-04-29 14:42:40 +03:00
|
|
|
Format.fprintf fmt "@[<hv>@[<hov 2>try@ %a@]@ with Empty ->@]@ @[%a@]"
|
|
|
|
format_with_parens body format_with_parens handler
|
2023-04-05 11:23:21 +03:00
|
|
|
| _ -> .
|
2021-01-28 02:28:28 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let format_struct_embedding
|
|
|
|
(fmt : Format.formatter)
|
2022-11-21 12:12:45 +03:00
|
|
|
((struct_name, struct_fields) : StructName.t * typ StructField.Map.t) =
|
2024-03-05 19:54:53 +03:00
|
|
|
if StructName.path struct_name = [] then
|
|
|
|
if StructField.Map.is_empty struct_fields then
|
|
|
|
Format.fprintf fmt "let embed_%a (_: %a.t) : runtime_value = Unit@\n@\n"
|
|
|
|
format_struct_name struct_name format_to_module_name
|
|
|
|
(`Sname struct_name)
|
|
|
|
else
|
|
|
|
Format.fprintf fmt
|
|
|
|
"@[<hov 2>let embed_%a (x: %a.t) : runtime_value =@ Struct(\"%a\",@ \
|
|
|
|
@[<hov 2>[%a]@])@]@\n\
|
|
|
|
@\n"
|
|
|
|
format_struct_name struct_name format_to_module_name
|
|
|
|
(`Sname struct_name) StructName.format struct_name
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ";@\n")
|
|
|
|
(fun fmt (struct_field, struct_field_type) ->
|
|
|
|
Format.fprintf fmt "(\"%a\",@ %a@ x.%a)" StructField.format
|
|
|
|
struct_field typ_embedding_name struct_field_type
|
|
|
|
format_struct_field_name
|
|
|
|
(Some struct_name, struct_field)))
|
|
|
|
(StructField.Map.bindings struct_fields)
|
2021-04-09 00:23:10 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let format_enum_embedding
|
|
|
|
(fmt : Format.formatter)
|
2022-11-21 12:12:45 +03:00
|
|
|
((enum_name, enum_cases) : EnumName.t * typ EnumConstructor.Map.t) =
|
2024-03-05 19:54:53 +03:00
|
|
|
if EnumName.path enum_name = [] then
|
|
|
|
if EnumConstructor.Map.is_empty enum_cases then
|
|
|
|
Format.fprintf fmt "let embed_%a (_: %a.t) : runtime_value = Unit@\n@\n"
|
|
|
|
format_enum_name enum_name format_to_module_name (`Ename enum_name)
|
|
|
|
else
|
|
|
|
Format.fprintf fmt
|
|
|
|
"@[<hv 2>@[<hov 2>let embed_%a@ @[<hov 2>(x:@ %a.t)@]@ : runtime_value \
|
|
|
|
=@]@ Enum(\"%a\",@ @[<hov 2>match x with@ %a@])@]@\n\
|
|
|
|
@\n"
|
|
|
|
format_enum_name enum_name format_to_module_name (`Ename enum_name)
|
|
|
|
EnumName.format enum_name
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@\n")
|
|
|
|
(fun fmt (enum_cons, enum_cons_type) ->
|
|
|
|
Format.fprintf fmt "@[<hov 2>| %a x ->@ (\"%a\", %a x)@]"
|
|
|
|
format_enum_cons_name enum_cons EnumConstructor.format enum_cons
|
|
|
|
typ_embedding_name enum_cons_type))
|
|
|
|
(EnumConstructor.Map.bindings enum_cases)
|
2021-04-09 00:23:10 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let format_ctx
|
|
|
|
(type_ordering : Scopelang.Dependency.TVertex.t list)
|
|
|
|
(fmt : Format.formatter)
|
2022-08-12 23:42:39 +03:00
|
|
|
(ctx : decl_ctx) : unit =
|
2021-01-29 01:46:39 +03:00
|
|
|
let format_struct_decl fmt (struct_name, struct_fields) =
|
2022-11-21 12:12:45 +03:00
|
|
|
if StructField.Map.is_empty struct_fields then
|
2022-07-12 15:10:53 +03:00
|
|
|
Format.fprintf fmt
|
2022-08-04 18:37:27 +03:00
|
|
|
"@[<v 2>module %a = struct@\n@[<hov 2>type t = unit@]@]@\nend@\n"
|
2022-07-20 19:24:54 +03:00
|
|
|
format_to_module_name (`Sname struct_name)
|
2021-02-03 12:53:21 +03:00
|
|
|
else
|
2022-07-12 15:10:53 +03:00
|
|
|
Format.fprintf fmt
|
2022-08-04 18:37:27 +03:00
|
|
|
"@[<v>@[<v 2>module %a = struct@ @[<hv 2>type t = {@,\
|
|
|
|
%a@;\
|
|
|
|
<0-2>}@]@]@ end@]@\n"
|
2022-07-20 19:24:54 +03:00
|
|
|
format_to_module_name (`Sname struct_name)
|
2021-02-03 12:53:21 +03:00
|
|
|
(Format.pp_print_list
|
2022-08-04 18:37:27 +03:00
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ";@ ")
|
2023-08-30 19:33:24 +03:00
|
|
|
(fun fmt (struct_field, struct_field_type) ->
|
2022-08-04 18:37:27 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a:@ %a@]" format_struct_field_name
|
2022-07-12 15:10:53 +03:00
|
|
|
(None, struct_field) format_typ struct_field_type))
|
2022-11-21 12:12:45 +03:00
|
|
|
(StructField.Map.bindings struct_fields);
|
2024-03-15 16:23:30 +03:00
|
|
|
if Global.options.trace then
|
2022-03-08 17:03:14 +03:00
|
|
|
format_struct_embedding fmt (struct_name, struct_fields)
|
2021-01-29 01:46:39 +03:00
|
|
|
in
|
|
|
|
let format_enum_decl fmt (enum_name, enum_cons) =
|
2022-07-21 16:57:02 +03:00
|
|
|
Format.fprintf fmt
|
|
|
|
"module %a = struct@\n@[<hov 2>@ type t =@\n@[<hov 2> %a@]@\nend@]@\n"
|
|
|
|
format_to_module_name (`Ename enum_name)
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@\n")
|
2023-08-30 19:33:24 +03:00
|
|
|
(fun fmt (enum_cons, enum_cons_type) ->
|
2022-08-04 18:37:27 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>| %a@ of@ %a@]" format_enum_cons_name
|
|
|
|
enum_cons format_typ enum_cons_type))
|
2022-11-21 12:12:45 +03:00
|
|
|
(EnumConstructor.Map.bindings enum_cons);
|
2024-03-15 16:23:30 +03:00
|
|
|
if Global.options.trace then format_enum_embedding fmt (enum_name, enum_cons)
|
2021-01-29 01:46:39 +03:00
|
|
|
in
|
2021-01-29 18:24:20 +03:00
|
|
|
let is_in_type_ordering s =
|
|
|
|
List.exists
|
|
|
|
(fun struct_or_enum ->
|
|
|
|
match struct_or_enum with
|
|
|
|
| Scopelang.Dependency.TVertex.Enum _ -> false
|
|
|
|
| Scopelang.Dependency.TVertex.Struct s' -> s = s')
|
|
|
|
type_ordering
|
|
|
|
in
|
|
|
|
let scope_structs =
|
|
|
|
List.map
|
|
|
|
(fun (s, _) -> Scopelang.Dependency.TVertex.Struct s)
|
2022-11-21 12:12:45 +03:00
|
|
|
(StructName.Map.bindings
|
|
|
|
(StructName.Map.filter
|
2022-03-08 17:03:14 +03:00
|
|
|
(fun s _ -> not (is_in_type_ordering s))
|
|
|
|
ctx.ctx_structs))
|
2021-01-29 18:24:20 +03:00
|
|
|
in
|
2021-01-29 01:46:39 +03:00
|
|
|
List.iter
|
|
|
|
(fun struct_or_enum ->
|
|
|
|
match struct_or_enum with
|
|
|
|
| Scopelang.Dependency.TVertex.Struct s ->
|
2023-08-30 18:49:29 +03:00
|
|
|
let def = StructName.Map.find s ctx.ctx_structs in
|
|
|
|
if StructName.path s = [] then
|
|
|
|
Format.fprintf fmt "%a@\n" format_struct_decl (s, def)
|
2021-01-29 01:46:39 +03:00
|
|
|
| Scopelang.Dependency.TVertex.Enum e ->
|
2023-08-30 18:49:29 +03:00
|
|
|
let def = EnumName.Map.find e ctx.ctx_enums in
|
|
|
|
if EnumName.path e = [] then
|
|
|
|
Format.fprintf fmt "%a@\n" format_enum_decl (e, def))
|
2021-01-29 18:24:20 +03:00
|
|
|
(type_ordering @ scope_structs)
|
2021-01-28 02:28:28 +03:00
|
|
|
|
2023-07-11 18:10:00 +03:00
|
|
|
let rename_vars e =
|
|
|
|
Expr.(
|
|
|
|
unbox
|
|
|
|
(rename_vars ~exclude:ocaml_keywords ~reset_context_for_closed_terms:true
|
|
|
|
~skip_constant_binders:true ~constant_binder_name:(Some "_") e))
|
|
|
|
|
2024-02-09 18:48:02 +03:00
|
|
|
let format_expr ctx fmt e =
|
|
|
|
Format.pp_open_vbox fmt 0;
|
|
|
|
format_expr ctx fmt (rename_vars e);
|
|
|
|
Format.pp_close_box fmt ()
|
2023-07-11 18:10:00 +03:00
|
|
|
|
2024-02-09 18:48:02 +03:00
|
|
|
let format_scope_body_expr
|
2022-08-12 23:42:39 +03:00
|
|
|
(ctx : decl_ctx)
|
2022-04-26 17:06:36 +03:00
|
|
|
(fmt : Format.formatter)
|
2022-08-25 20:46:13 +03:00
|
|
|
(scope_lets : 'm Ast.expr scope_body_expr) : unit =
|
2024-02-09 18:48:02 +03:00
|
|
|
Format.pp_open_vbox fmt 0;
|
|
|
|
let last_e =
|
|
|
|
BoundList.iter
|
|
|
|
~f:(fun scope_let_var scope_let ->
|
|
|
|
Format.fprintf fmt "@[<hv>@[<hov 2>let %a: %a =@ %a@ @]in@]@,"
|
|
|
|
format_var scope_let_var format_typ scope_let.scope_let_typ
|
|
|
|
(format_expr ctx) scope_let.scope_let_expr)
|
|
|
|
scope_lets
|
|
|
|
in
|
|
|
|
format_expr ctx fmt last_e;
|
|
|
|
Format.pp_close_box fmt ()
|
2022-04-26 17:06:36 +03:00
|
|
|
|
2023-02-13 17:00:23 +03:00
|
|
|
let format_code_items
|
2022-08-12 23:42:39 +03:00
|
|
|
(ctx : decl_ctx)
|
2022-04-26 17:06:36 +03:00
|
|
|
(fmt : Format.formatter)
|
2023-12-04 18:41:03 +03:00
|
|
|
(code_items : 'm Ast.expr code_item_list) :
|
|
|
|
('m Ast.expr Var.t * 'm Ast.expr code_item) String.Map.t =
|
2024-02-09 18:48:02 +03:00
|
|
|
Format.pp_open_vbox fmt 0;
|
|
|
|
let var_bindings, () =
|
|
|
|
BoundList.fold_left
|
|
|
|
~f:(fun bnd item var ->
|
|
|
|
match item with
|
|
|
|
| Topdef (name, typ, e) ->
|
|
|
|
Format.fprintf fmt "@,@[<v 2>@[<hov 2>let %a : %a =@]@ %a@]@,"
|
|
|
|
format_var var format_typ typ (format_expr ctx) e;
|
|
|
|
String.Map.add (TopdefName.to_string name) (var, item) bnd
|
|
|
|
| ScopeDef (name, body) ->
|
|
|
|
let scope_input_var, scope_body_expr =
|
|
|
|
Bindlib.unbind body.scope_body_expr
|
|
|
|
in
|
|
|
|
Format.fprintf fmt
|
|
|
|
"@,@[<hv 2>@[<hov 2>let %a (%a: %a.t) : %a.t =@]@ %a@]@," format_var
|
|
|
|
var format_var scope_input_var format_to_module_name
|
|
|
|
(`Sname body.scope_body_input_struct) format_to_module_name
|
|
|
|
(`Sname body.scope_body_output_struct)
|
|
|
|
(format_scope_body_expr ctx)
|
|
|
|
scope_body_expr;
|
|
|
|
String.Map.add (ScopeName.to_string name) (var, item) bnd)
|
|
|
|
~init:String.Map.empty code_items
|
|
|
|
in
|
|
|
|
Format.pp_close_box fmt ();
|
|
|
|
var_bindings
|
2023-04-19 19:26:50 +03:00
|
|
|
|
2023-06-28 17:25:02 +03:00
|
|
|
let format_scope_exec
|
|
|
|
(ctx : decl_ctx)
|
|
|
|
(fmt : Format.formatter)
|
2023-12-04 18:41:03 +03:00
|
|
|
(bnd : ('m Ast.expr Var.t * _) String.Map.t)
|
2023-06-28 17:25:02 +03:00
|
|
|
scope_name
|
|
|
|
scope_body =
|
2023-08-30 19:33:24 +03:00
|
|
|
let scope_name_str = Format.asprintf "%a" ScopeName.format scope_name in
|
2023-12-04 18:41:03 +03:00
|
|
|
let scope_var, _ = String.Map.find scope_name_str bnd in
|
2023-08-30 18:49:29 +03:00
|
|
|
let scope_input =
|
2023-06-28 17:25:02 +03:00
|
|
|
StructName.Map.find scope_body.scope_body_input_struct ctx.ctx_structs
|
|
|
|
in
|
|
|
|
if not (StructField.Map.is_empty scope_input) then
|
2024-04-10 19:39:30 +03:00
|
|
|
Message.error
|
2023-06-28 17:25:02 +03:00
|
|
|
"The scope @{<bold>%s@} defines input variables.@ This is not supported \
|
|
|
|
for a main scope at the moment."
|
|
|
|
scope_name_str;
|
|
|
|
Format.pp_open_vbox fmt 2;
|
2023-09-09 23:02:39 +03:00
|
|
|
Format.pp_print_string fmt "let () =";
|
2023-06-28 17:25:02 +03:00
|
|
|
(* TODO: dump the output using yojson that should be already available from
|
|
|
|
the runtime *)
|
|
|
|
Format.pp_print_space fmt ();
|
|
|
|
format_var fmt scope_var;
|
|
|
|
Format.pp_print_space fmt ();
|
|
|
|
Format.pp_print_string fmt "()";
|
|
|
|
Format.pp_close_box fmt ()
|
|
|
|
|
2023-12-04 18:41:03 +03:00
|
|
|
let format_scope_exec_args
|
|
|
|
(ctx : decl_ctx)
|
|
|
|
(fmt : Format.formatter)
|
|
|
|
(bnd : ('m Ast.expr Var.t * 'm Ast.expr code_item) String.Map.t) =
|
|
|
|
let scopes_with_no_input =
|
|
|
|
String.Map.fold
|
|
|
|
(fun strname (var, item) acc ->
|
|
|
|
match item with
|
|
|
|
| Topdef _ -> acc
|
|
|
|
| ScopeDef (name, body) ->
|
|
|
|
let input_struct =
|
|
|
|
StructName.Map.find body.scope_body_input_struct ctx.ctx_structs
|
|
|
|
in
|
|
|
|
if StructField.Map.is_empty input_struct then
|
|
|
|
(var, name, strname) :: acc
|
|
|
|
else acc)
|
|
|
|
bnd []
|
|
|
|
|> List.rev
|
|
|
|
in
|
|
|
|
if scopes_with_no_input = [] then
|
2024-04-10 19:39:30 +03:00
|
|
|
Message.error
|
2023-12-04 18:41:03 +03:00
|
|
|
"No scopes that don't require input were found, executable can't be \
|
|
|
|
generated";
|
2024-04-10 19:39:30 +03:00
|
|
|
Message.debug "@[<hov 2>Generating entry points for scopes:@ %a@]@."
|
2023-12-04 18:41:03 +03:00
|
|
|
(Format.pp_print_list ~pp_sep:Format.pp_print_space (fun ppf (_, s, _) ->
|
|
|
|
ScopeName.format ppf s))
|
|
|
|
scopes_with_no_input;
|
|
|
|
Format.pp_print_string fmt "\nlet entry_scopes = [\n";
|
|
|
|
List.iter
|
|
|
|
(fun (_, _, name) -> Format.fprintf fmt " %S;\n" name)
|
|
|
|
scopes_with_no_input;
|
|
|
|
Format.pp_print_string fmt "]\n";
|
|
|
|
|
|
|
|
Format.pp_print_string fmt
|
|
|
|
{|
|
|
|
|
let commands =
|
|
|
|
List.map (fun c ->
|
|
|
|
if List.mem c entry_scopes then c else (
|
|
|
|
print_endline "Specify scopes from the following list (or no argument for running them all):";
|
|
|
|
List.iter (fun n -> print_endline (" - " ^ n)) entry_scopes;
|
|
|
|
exit 1
|
|
|
|
))
|
|
|
|
(List.tl (Array.to_list Sys.argv))
|
|
|
|
|
|
|
|
let commands = if commands = [] then entry_scopes else commands
|
|
|
|
|
|
|
|
|};
|
|
|
|
List.iter
|
|
|
|
(fun (var, _, name) ->
|
|
|
|
(* Note: this only checks that execution doesn't raise errors or assert
|
|
|
|
failures. Adding a printer for the results could be an idea... *)
|
|
|
|
Format.fprintf fmt
|
|
|
|
"let () = if List.mem %S commands then (\n\
|
|
|
|
\ ignore (%a ());\n\
|
|
|
|
\ print_endline \"Scope %s executed successfully.\"\n\
|
|
|
|
)@\n"
|
|
|
|
name format_var var name)
|
|
|
|
scopes_with_no_input
|
|
|
|
|
2024-03-08 19:32:47 +03:00
|
|
|
let reexport_used_modules fmt modules =
|
|
|
|
List.iter
|
|
|
|
(fun m ->
|
|
|
|
Format.fprintf fmt "@[<hv 2>module %a@ = %a@]@," ModuleName.format m
|
|
|
|
ModuleName.format m)
|
|
|
|
modules
|
|
|
|
|
2023-04-19 19:26:50 +03:00
|
|
|
let format_module_registration
|
|
|
|
fmt
|
2023-12-04 18:41:03 +03:00
|
|
|
(bnd : ('m Ast.expr Var.t * _) String.Map.t)
|
2023-04-19 19:26:50 +03:00
|
|
|
modname =
|
|
|
|
Format.pp_open_vbox fmt 2;
|
|
|
|
Format.pp_print_string fmt "let () =";
|
|
|
|
Format.pp_print_space fmt ();
|
|
|
|
Format.pp_open_hvbox fmt 2;
|
2023-09-19 19:21:14 +03:00
|
|
|
Format.fprintf fmt "Runtime_ocaml.Runtime.register_module \"%a\""
|
|
|
|
ModuleName.format modname;
|
2023-04-19 19:26:50 +03:00
|
|
|
Format.pp_print_space fmt ();
|
|
|
|
Format.pp_open_vbox fmt 2;
|
|
|
|
Format.pp_print_string fmt "[ ";
|
|
|
|
Format.pp_print_seq
|
2023-06-13 13:24:21 +03:00
|
|
|
~pp_sep:(fun fmt () ->
|
|
|
|
Format.pp_print_char fmt ';';
|
|
|
|
Format.pp_print_cut fmt ())
|
2023-12-04 18:41:03 +03:00
|
|
|
(fun fmt (id, (var, _)) ->
|
2023-06-13 13:24:21 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%S,@ Obj.repr %a@]" id format_var var)
|
2023-04-19 19:26:50 +03:00
|
|
|
fmt (String.Map.to_seq bnd);
|
|
|
|
Format.pp_close_box fmt ();
|
|
|
|
Format.pp_print_char fmt ' ';
|
|
|
|
Format.pp_print_string fmt "]";
|
|
|
|
Format.pp_print_space fmt ();
|
|
|
|
Format.pp_print_string fmt "\"todo-module-hash\"";
|
|
|
|
Format.pp_close_box fmt ();
|
2023-09-19 12:44:18 +03:00
|
|
|
Format.pp_close_box fmt ();
|
|
|
|
Format.pp_print_newline fmt ()
|
2023-04-19 19:26:50 +03:00
|
|
|
|
|
|
|
let header =
|
|
|
|
{ocaml|
|
|
|
|
(** This file has been generated by the Catala compiler, do not edit! *)
|
|
|
|
|
|
|
|
open Runtime_ocaml.Runtime
|
|
|
|
|
|
|
|
[@@@ocaml.warning "-4-26-27-32-41-42"]
|
|
|
|
|
|
|
|
|ocaml}
|
2022-04-26 17:06:36 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let format_program
|
|
|
|
(fmt : Format.formatter)
|
2023-06-28 17:25:02 +03:00
|
|
|
?exec_scope
|
2023-12-04 18:41:03 +03:00
|
|
|
?(exec_args = true)
|
2022-06-23 15:06:11 +03:00
|
|
|
(p : 'm Ast.program)
|
2021-01-29 01:46:39 +03:00
|
|
|
(type_ordering : Scopelang.Dependency.TVertex.t list) : unit =
|
2024-03-08 19:32:47 +03:00
|
|
|
Format.pp_open_vbox fmt 0;
|
2023-04-19 19:26:50 +03:00
|
|
|
Format.pp_print_string fmt header;
|
2024-03-08 19:32:47 +03:00
|
|
|
reexport_used_modules fmt (Program.modules_to_list p.decl_ctx.ctx_modules);
|
2023-04-19 19:26:50 +03:00
|
|
|
format_ctx type_ordering fmt p.decl_ctx;
|
|
|
|
let bnd = format_code_items p.decl_ctx fmt p.code_items in
|
2024-03-08 19:32:47 +03:00
|
|
|
Format.pp_print_cut fmt ();
|
|
|
|
let () =
|
|
|
|
match p.module_name, exec_scope with
|
|
|
|
| Some modname, None -> format_module_registration fmt bnd modname
|
|
|
|
| None, Some scope_name ->
|
|
|
|
let scope_body = Program.get_scope_body p scope_name in
|
|
|
|
format_scope_exec p.decl_ctx fmt bnd scope_name scope_body
|
|
|
|
| None, None -> if exec_args then format_scope_exec_args p.decl_ctx fmt bnd
|
|
|
|
| Some _, Some _ ->
|
2024-04-10 19:39:30 +03:00
|
|
|
Message.error
|
2024-03-08 19:32:47 +03:00
|
|
|
"OCaml generation: both module registration and top-level scope \
|
|
|
|
execution where required at the same time."
|
|
|
|
in
|
|
|
|
Format.pp_close_box fmt ()
|