2021-06-21 19:00:06 +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>
|
|
|
|
|
|
|
|
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. *)
|
2021-06-23 18:47:34 +03:00
|
|
|
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2022-08-12 23:42:39 +03:00
|
|
|
open Shared_ast
|
2021-06-21 19:00:06 +03:00
|
|
|
open Ast
|
2022-07-19 20:17:02 +03:00
|
|
|
module Runtime = Runtime_ocaml.Runtime
|
2021-06-21 19:00:06 +03:00
|
|
|
module D = Dcalc.Ast
|
2021-06-23 18:47:34 +03:00
|
|
|
module L = Lcalc.Ast
|
2021-06-21 19:00:06 +03:00
|
|
|
|
2023-05-17 16:44:57 +03:00
|
|
|
let format_lit (fmt : Format.formatter) (l : lit Mark.pos) : unit =
|
|
|
|
match Mark.remove l with
|
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
|
|
|
| LBool true -> Format.pp_print_string fmt "True"
|
|
|
|
| LBool false -> Format.pp_print_string fmt "False"
|
2021-06-22 15:00:42 +03:00
|
|
|
| LInt i ->
|
|
|
|
Format.fprintf fmt "integer_of_string(\"%s\")" (Runtime.integer_to_string i)
|
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
|
|
|
| LUnit -> Format.pp_print_string fmt "Unit()"
|
2022-08-17 17:14:14 +03:00
|
|
|
| LRat i -> Format.fprintf fmt "decimal_of_string(\"%a\")" Print.lit (LRat i)
|
2021-06-21 19:00:06 +03:00
|
|
|
| LMoney e ->
|
2021-06-22 15:00:42 +03:00
|
|
|
Format.fprintf fmt "money_of_cents_string(\"%s\")"
|
2021-06-21 19:00:06 +03:00
|
|
|
(Runtime.integer_to_string (Runtime.money_to_cents e))
|
|
|
|
| LDate d ->
|
2021-06-22 15:00:42 +03:00
|
|
|
Format.fprintf fmt "date_of_numbers(%d,%d,%d)"
|
2021-06-21 19:00:06 +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))
|
|
|
|
| LDuration d ->
|
|
|
|
let years, months, days = Runtime.duration_to_years_months_days d in
|
2021-06-22 15:00:42 +03:00
|
|
|
Format.fprintf fmt "duration_of_numbers(%d,%d,%d)" years months days
|
2021-06-21 19:00:06 +03:00
|
|
|
|
2023-05-17 16:44:57 +03:00
|
|
|
let format_op (fmt : Format.formatter) (op : operator Mark.pos) : unit =
|
|
|
|
match Mark.remove op with
|
2023-05-26 18:08:26 +03:00
|
|
|
| Log (_entry, _infos) -> assert false
|
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
|
|
|
| Minus_int | Minus_rat | Minus_mon | Minus_dur ->
|
|
|
|
Format.pp_print_string fmt "-"
|
|
|
|
(* Todo: use the names from [Operator.name] *)
|
|
|
|
| Not -> Format.pp_print_string fmt "not"
|
|
|
|
| Length -> Format.pp_print_string fmt "list_length"
|
2022-12-13 15:28:01 +03:00
|
|
|
| ToRat_int -> Format.pp_print_string fmt "decimal_of_integer"
|
|
|
|
| ToRat_mon -> Format.pp_print_string fmt "decimal_of_money"
|
|
|
|
| ToMoney_rat -> Format.pp_print_string fmt "money_of_decimal"
|
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
|
|
|
| GetDay -> Format.pp_print_string fmt "day_of_month_of_date"
|
|
|
|
| GetMonth -> Format.pp_print_string fmt "month_number_of_date"
|
|
|
|
| GetYear -> Format.pp_print_string fmt "year_of_date"
|
|
|
|
| FirstDayOfMonth -> Format.pp_print_string fmt "first_day_of_month"
|
|
|
|
| LastDayOfMonth -> Format.pp_print_string fmt "last_day_of_month"
|
2022-12-13 15:28:01 +03:00
|
|
|
| Round_mon -> Format.pp_print_string fmt "money_round"
|
|
|
|
| Round_rat -> Format.pp_print_string fmt "decimal_round"
|
2024-08-26 16:44:00 +03:00
|
|
|
| Add_int_int | Add_rat_rat | Add_mon_mon | Add_dur_dur | Concat ->
|
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
|
|
|
Format.pp_print_string fmt "+"
|
2024-08-26 16:44:00 +03:00
|
|
|
| Add_dat_dur rounding ->
|
|
|
|
Format.fprintf fmt "add_date_duration(%s)"
|
|
|
|
(match rounding with
|
|
|
|
| RoundUp -> "DateRounding.RoundUp"
|
|
|
|
| RoundDown -> "DateRounding.RoundDown"
|
|
|
|
| AbortOnRound -> "DateRounding.AbortOnRound")
|
2024-09-10 19:26:34 +03:00
|
|
|
| Sub_int_int | Sub_rat_rat | Sub_mon_mon | Sub_dat_dat | Sub_dur_dur ->
|
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
|
|
|
Format.pp_print_string fmt "-"
|
2024-09-10 19:26:34 +03:00
|
|
|
| Sub_dat_dur rounding ->
|
|
|
|
Format.fprintf fmt "sub_date_duration(%s)"
|
|
|
|
(match rounding with
|
|
|
|
| RoundUp -> "DateRounding.RoundUp"
|
|
|
|
| RoundDown -> "DateRounding.RoundDown"
|
|
|
|
| AbortOnRound -> "DateRounding.AbortOnRound")
|
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
|
|
|
| Mult_int_int | Mult_rat_rat | Mult_mon_rat | Mult_dur_int ->
|
|
|
|
Format.pp_print_string fmt "*"
|
2023-03-06 16:42:24 +03:00
|
|
|
| Div_int_int | Div_rat_rat | Div_mon_mon | Div_mon_rat | Div_dur_dur ->
|
2024-08-26 16:44:00 +03:00
|
|
|
Format.pp_print_string fmt "div"
|
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
|
|
|
| And -> Format.pp_print_string fmt "and"
|
|
|
|
| Or -> Format.pp_print_string fmt "or"
|
2024-07-31 23:34:50 +03:00
|
|
|
| Eq -> Format.pp_print_string 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
|
|
|
| Xor -> Format.pp_print_string fmt "!="
|
2024-08-26 16:44:00 +03:00
|
|
|
| Lt_int_int | Lt_rat_rat | Lt_mon_mon | Lt_dat_dat ->
|
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
|
|
|
Format.pp_print_string fmt "<"
|
2024-08-26 16:44:00 +03:00
|
|
|
| Lte_int_int | Lte_rat_rat | Lte_mon_mon | Lte_dat_dat ->
|
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
|
|
|
Format.pp_print_string fmt "<="
|
2024-08-26 16:44:00 +03:00
|
|
|
| Gt_int_int | Gt_rat_rat | Gt_mon_mon | Gt_dat_dat ->
|
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
|
|
|
Format.pp_print_string fmt ">"
|
2024-08-26 16:44:00 +03:00
|
|
|
| Gte_int_int | Gte_rat_rat | Gte_mon_mon | Gte_dat_dat ->
|
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
|
|
|
Format.pp_print_string fmt ">="
|
2024-08-26 16:44:00 +03:00
|
|
|
| Lt_dur_dur -> Format.pp_print_string fmt "lt_duration"
|
|
|
|
| Lte_dur_dur -> Format.pp_print_string fmt "leq_duration"
|
|
|
|
| Gt_dur_dur -> Format.pp_print_string fmt "gt_duration"
|
|
|
|
| Gte_dur_dur -> Format.pp_print_string fmt "geq_duration"
|
2024-07-15 18:31:05 +03:00
|
|
|
| Eq_boo_boo | Eq_int_int | Eq_rat_rat | Eq_mon_mon | Eq_dat_dat | Eq_dur_dur
|
|
|
|
->
|
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
|
|
|
Format.pp_print_string fmt "=="
|
|
|
|
| Map -> Format.pp_print_string fmt "list_map"
|
2024-01-24 17:36:51 +03:00
|
|
|
| Map2 -> Format.pp_print_string fmt "list_map2"
|
2022-12-12 18:02:07 +03:00
|
|
|
| Reduce -> Format.pp_print_string fmt "list_reduce"
|
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
|
|
|
| Filter -> Format.pp_print_string fmt "list_filter"
|
|
|
|
| Fold -> Format.pp_print_string fmt "list_fold_left"
|
2024-06-24 17:48:56 +03:00
|
|
|
| HandleExceptions -> Format.pp_print_string fmt "handle_exceptions"
|
2023-06-15 17:52:36 +03:00
|
|
|
| FromClosureEnv | ToClosureEnv -> failwith "unimplemented"
|
2021-06-21 19:00:06 +03:00
|
|
|
|
|
|
|
let format_uid_list (fmt : Format.formatter) (uids : Uid.MarkedString.info list)
|
|
|
|
: unit =
|
2021-06-24 19:17:54 +03:00
|
|
|
Format.fprintf fmt "[%a]"
|
2021-06-21 19:00:06 +03:00
|
|
|
(Format.pp_print_list
|
2021-06-24 18:50:08 +03:00
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
|
2021-06-21 19:00:06 +03:00
|
|
|
(fun fmt info ->
|
2022-11-21 12:46:17 +03:00
|
|
|
Format.fprintf fmt "\"%a\"" Uid.MarkedString.format info))
|
2021-06-21 19:00:06 +03:00
|
|
|
uids
|
|
|
|
|
|
|
|
let format_string_list (fmt : Format.formatter) (uids : string list) : unit =
|
2022-07-22 20:02:09 +03:00
|
|
|
let sanitize_quotes = Re.compile (Re.char '"') in
|
2021-06-24 19:17:54 +03:00
|
|
|
Format.fprintf fmt "[%a]"
|
2021-06-21 19:00:06 +03:00
|
|
|
(Format.pp_print_list
|
2021-06-24 18:50:08 +03:00
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
|
2022-07-22 20:02:09 +03:00
|
|
|
(fun fmt info ->
|
|
|
|
Format.fprintf fmt "\"%s\""
|
|
|
|
(Re.replace sanitize_quotes ~f:(fun _ -> "\\\"") info)))
|
2021-06-21 19:00:06 +03:00
|
|
|
uids
|
|
|
|
|
2024-08-07 18:43:14 +03:00
|
|
|
let python_keywords =
|
|
|
|
(* list taken from
|
|
|
|
https://www.programiz.com/python-programming/keyword-list *)
|
|
|
|
[
|
|
|
|
"False";
|
|
|
|
"None";
|
|
|
|
"True";
|
|
|
|
"and";
|
|
|
|
"as";
|
|
|
|
"assert";
|
|
|
|
"async";
|
|
|
|
"await";
|
|
|
|
"break";
|
|
|
|
"class";
|
|
|
|
"continue";
|
|
|
|
"def";
|
|
|
|
"del";
|
|
|
|
"elif";
|
|
|
|
"else";
|
|
|
|
"except";
|
|
|
|
"finally";
|
|
|
|
"for";
|
|
|
|
"from";
|
|
|
|
"global";
|
|
|
|
"if";
|
|
|
|
"import";
|
|
|
|
"in";
|
|
|
|
"is";
|
|
|
|
"lambda";
|
|
|
|
"nonlocal";
|
|
|
|
"not";
|
|
|
|
"or";
|
|
|
|
"pass";
|
|
|
|
"raise";
|
|
|
|
"return";
|
|
|
|
"try";
|
|
|
|
"while";
|
|
|
|
"with";
|
|
|
|
"yield";
|
|
|
|
]
|
|
|
|
(* todo: reserved names should also include built-in types and everything
|
|
|
|
exposed by the runtime. *)
|
|
|
|
|
|
|
|
let renaming =
|
2024-08-07 19:03:10 +03:00
|
|
|
Renaming.program ()
|
2024-08-07 18:43:14 +03:00
|
|
|
~reserved:python_keywords
|
|
|
|
(* TODO: add catala runtime built-ins as reserved as well ? *)
|
2024-09-02 13:15:14 +03:00
|
|
|
~skip_constant_binders:false ~constant_binder_name:None
|
2024-09-24 16:29:29 +03:00
|
|
|
~namespaced_fields_constrs:true ~prefix_module:false
|
|
|
|
~f_var:String.to_snake_case ~f_struct:String.to_camel_case
|
|
|
|
~f_enum:String.to_camel_case
|
|
|
|
|
|
|
|
let format_qualified
|
|
|
|
(type id)
|
|
|
|
(module Id : Uid.Qualified with type t = id)
|
|
|
|
ctx
|
|
|
|
ppf
|
|
|
|
(s : id) =
|
|
|
|
match List.rev (Id.path s) with
|
|
|
|
| [] -> Format.pp_print_string ppf (Id.base s)
|
|
|
|
| m :: _ ->
|
|
|
|
Format.fprintf ppf "%a.%s" VarName.format
|
|
|
|
(ModuleName.Map.find m ctx.modules)
|
|
|
|
(Id.base s)
|
|
|
|
|
|
|
|
let format_struct = format_qualified (module StructName)
|
|
|
|
let format_enum = format_qualified (module EnumName)
|
2024-02-22 14:14:25 +03:00
|
|
|
|
|
|
|
let typ_needs_parens (e : typ) : bool =
|
|
|
|
match Mark.remove e with TArrow _ | TArray _ -> true | _ -> false
|
|
|
|
|
|
|
|
let rec format_typ ctx (fmt : Format.formatter) (typ : typ) : unit =
|
|
|
|
let format_typ = format_typ ctx in
|
|
|
|
let format_typ_with_parens (fmt : Format.formatter) (t : typ) =
|
|
|
|
if typ_needs_parens t then Format.fprintf fmt "(%a)" format_typ t
|
|
|
|
else Format.fprintf fmt "%a" format_typ t
|
|
|
|
in
|
|
|
|
match Mark.remove typ with
|
|
|
|
| TLit TUnit -> Format.fprintf fmt "Unit"
|
|
|
|
| TLit TMoney -> Format.fprintf fmt "Money"
|
|
|
|
| TLit TInt -> Format.fprintf fmt "Integer"
|
|
|
|
| TLit TRat -> Format.fprintf fmt "Decimal"
|
|
|
|
| TLit TDate -> Format.fprintf fmt "Date"
|
|
|
|
| TLit TDuration -> Format.fprintf fmt "Duration"
|
|
|
|
| TLit TBool -> Format.fprintf fmt "bool"
|
|
|
|
| TTuple ts ->
|
|
|
|
Format.fprintf fmt "Tuple[%a]"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ", ")
|
|
|
|
(fun fmt t -> Format.fprintf fmt "%a" format_typ_with_parens t))
|
|
|
|
ts
|
2024-09-24 16:29:29 +03:00
|
|
|
| TStruct s -> format_struct ctx fmt s
|
2024-02-22 14:14:25 +03:00
|
|
|
| TOption some_typ ->
|
|
|
|
(* We translate the option type with an overloading by Python's [None] *)
|
|
|
|
Format.fprintf fmt "Optional[%a]" format_typ some_typ
|
|
|
|
| TDefault t -> format_typ fmt t
|
2024-09-24 16:29:29 +03:00
|
|
|
| TEnum e -> format_enum ctx fmt e
|
2024-02-22 14:14:25 +03:00
|
|
|
| TArrow (t1, t2) ->
|
|
|
|
Format.fprintf fmt "Callable[[%a], %a]"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
|
|
|
|
format_typ_with_parens)
|
|
|
|
t1 format_typ_with_parens t2
|
|
|
|
| TArray t1 -> Format.fprintf fmt "List[%a]" format_typ_with_parens t1
|
|
|
|
| TAny -> Format.fprintf fmt "Any"
|
|
|
|
| TClosureEnv -> failwith "unimplemented!"
|
|
|
|
|
2023-02-10 20:56:40 +03:00
|
|
|
let format_func_name (fmt : Format.formatter) (v : FuncName.t) : unit =
|
2024-08-07 18:43:14 +03:00
|
|
|
FuncName.format fmt v
|
2021-06-21 19:00:06 +03:00
|
|
|
|
2024-02-22 14:14:25 +03:00
|
|
|
let rec format_expression ctx (fmt : Format.formatter) (e : expr) : unit =
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e with
|
2024-08-07 18:43:14 +03:00
|
|
|
| EVar v -> VarName.format fmt v
|
|
|
|
| EFunc f -> FuncName.format fmt f
|
2023-12-11 17:59:47 +03:00
|
|
|
| EStruct { fields = es; name = s } ->
|
2024-09-24 16:29:29 +03:00
|
|
|
Format.fprintf fmt "%a(%a)" (format_struct ctx) s
|
2022-02-01 17:41:53 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
|
2024-01-26 22:15:32 +03:00
|
|
|
(fun fmt (struct_field, e) ->
|
2024-08-07 18:43:14 +03:00
|
|
|
Format.fprintf fmt "%a = %a" StructField.format struct_field
|
2022-02-01 17:41:53 +03:00
|
|
|
(format_expression ctx) e))
|
2024-01-26 22:15:32 +03:00
|
|
|
(StructField.Map.bindings es)
|
2023-12-11 17:59:47 +03:00
|
|
|
| EStructFieldAccess { e1; field; _ } ->
|
2021-06-24 18:50:08 +03:00
|
|
|
Format.fprintf fmt "%a.%a" (format_expression ctx) e1 StructField.format
|
2024-08-07 18:43:14 +03:00
|
|
|
field
|
2023-12-11 17:59:47 +03:00
|
|
|
| EInj { cons; name = e_name; _ }
|
2023-05-17 14:26:47 +03:00
|
|
|
when EnumName.equal e_name Expr.option_enum
|
|
|
|
&& EnumConstructor.equal cons Expr.none_constr ->
|
2022-02-25 14:25:42 +03:00
|
|
|
(* We translate the option type with an overloading by Python's [None] *)
|
|
|
|
Format.fprintf fmt "None"
|
2023-12-12 20:21:20 +03:00
|
|
|
| EInj { e1 = e; cons; name = e_name; _ }
|
2023-05-17 14:26:47 +03:00
|
|
|
when EnumName.equal e_name Expr.option_enum
|
|
|
|
&& EnumConstructor.equal cons Expr.some_constr ->
|
2022-02-25 14:25:42 +03:00
|
|
|
(* We translate the option type with an overloading by Python's [None] *)
|
|
|
|
format_expression ctx fmt e
|
2023-12-12 20:21:20 +03:00
|
|
|
| EInj { e1 = e; cons; name = enum_name; _ } ->
|
2024-09-24 16:29:29 +03:00
|
|
|
Format.fprintf fmt "%a(%a_Code.%a,@ %a)" (format_enum ctx) enum_name
|
|
|
|
(format_enum ctx) enum_name EnumConstructor.format cons
|
2021-06-24 22:55:20 +03:00
|
|
|
(format_expression ctx) e
|
2021-06-21 19:00:06 +03:00
|
|
|
| EArray es ->
|
2021-06-24 19:17:54 +03:00
|
|
|
Format.fprintf fmt "[%a]"
|
2021-06-21 19:00:06 +03:00
|
|
|
(Format.pp_print_list
|
2021-06-23 18:47:34 +03:00
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
|
2021-06-24 18:50:08 +03:00
|
|
|
(fun fmt e -> Format.fprintf fmt "%a" (format_expression ctx) e))
|
2021-06-21 19:00:06 +03:00
|
|
|
es
|
2023-05-17 16:44:57 +03:00
|
|
|
| ELit l -> Format.fprintf fmt "%a" format_lit (Mark.copy e l)
|
2024-08-20 16:39:11 +03:00
|
|
|
| EPosLit ->
|
|
|
|
let pos = Mark.get e in
|
|
|
|
Format.fprintf fmt
|
|
|
|
"@[<hov 4>SourcePosition(@,\
|
|
|
|
filename=\"%s\",@ start_line=%d, start_column=%d,@ end_line=%d, \
|
|
|
|
end_column=%d,@ law_headings=%a@;\
|
|
|
|
<0 -4>)@]" (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)
|
2024-08-26 16:44:00 +03:00
|
|
|
| EAppOp
|
|
|
|
{
|
|
|
|
op = ((HandleExceptions | Map | Filter), _) as op;
|
|
|
|
args = [arg1; arg2];
|
|
|
|
_;
|
|
|
|
} ->
|
2024-04-30 17:35:08 +03:00
|
|
|
Format.fprintf fmt "%a(%a,@ %a)" format_op op (format_expression ctx) arg1
|
|
|
|
(format_expression ctx) arg2
|
2024-07-12 12:14:58 +03:00
|
|
|
| EAppOp { op; args = [arg1; arg2]; _ } ->
|
2024-04-30 17:35:08 +03:00
|
|
|
Format.fprintf fmt "(%a %a@ %a)" (format_expression ctx) arg1 format_op op
|
|
|
|
(format_expression ctx) arg2
|
2023-12-11 17:59:47 +03:00
|
|
|
| EApp
|
2024-04-30 17:35:08 +03:00
|
|
|
{
|
2024-07-12 12:14:58 +03:00
|
|
|
f = EAppOp { op = Log (BeginCall, info), _; args = [f]; _ }, _;
|
2024-04-30 17:35:08 +03:00
|
|
|
args = [arg];
|
|
|
|
}
|
2024-03-15 16:23:30 +03:00
|
|
|
when Global.options.trace ->
|
2021-06-24 19:17:54 +03:00
|
|
|
Format.fprintf fmt "log_begin_call(%a,@ %a,@ %a)" format_uid_list info
|
2022-02-25 14:25:42 +03:00
|
|
|
(format_expression ctx) f (format_expression ctx) arg
|
2024-07-12 12:14:58 +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 18:08:26 +03:00
|
|
|
Format.fprintf fmt
|
2023-11-22 20:08:44 +03:00
|
|
|
"log_variable_definition(%a,@ LogIO(input_io=InputIO.%s,@ \
|
|
|
|
output_io=%s),@ %a)"
|
2023-05-26 18:08:26 +03:00
|
|
|
format_uid_list info
|
|
|
|
(match var_def_info.log_io_input with
|
|
|
|
| Runtime.NoInput -> "NoInput"
|
|
|
|
| Runtime.OnlyInput -> "OnlyInput"
|
|
|
|
| Runtime.Reentrant -> "Reentrant")
|
2023-11-22 20:08:44 +03:00
|
|
|
(if var_def_info.log_io_output then "True" else "False")
|
|
|
|
(format_expression ctx) arg1
|
2024-07-12 12:14:58 +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 = Mark.get e in
|
2021-06-21 19:00:06 +03:00
|
|
|
Format.fprintf fmt
|
2021-06-24 19:17:54 +03:00
|
|
|
"log_decision_taken(SourcePosition(filename=\"%s\",@ start_line=%d,@ \
|
|
|
|
start_column=%d,@ end_line=%d, end_column=%d,@ law_headings=%a), %a)"
|
2021-06-21 19:00:06 +03:00
|
|
|
(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_expression ctx) arg1
|
2024-07-12 12:14:58 +03:00
|
|
|
| EAppOp { op = Log (EndCall, info), _; args = [arg1]; _ }
|
2024-04-30 17:35:08 +03:00
|
|
|
when Global.options.trace ->
|
2021-06-24 19:17:54 +03:00
|
|
|
Format.fprintf fmt "log_end_call(%a,@ %a)" format_uid_list info
|
2021-06-24 18:50:08 +03:00
|
|
|
(format_expression ctx) arg1
|
2024-07-12 12:14:58 +03:00
|
|
|
| EAppOp { op = Log _, _; args = [arg1]; _ } ->
|
2021-06-24 18:50:08 +03:00
|
|
|
Format.fprintf fmt "%a" (format_expression ctx) arg1
|
2024-07-12 12:14:58 +03:00
|
|
|
| EAppOp { op = (Not, _) as op; args = [arg1]; _ } ->
|
2024-04-30 17:35:08 +03:00
|
|
|
Format.fprintf fmt "%a %a" format_op op (format_expression ctx) arg1
|
2024-01-15 19:19:17 +03:00
|
|
|
| EAppOp
|
2023-12-11 17:59:47 +03:00
|
|
|
{
|
2024-04-30 17:35:08 +03:00
|
|
|
op = ((Minus_int | Minus_rat | Minus_mon | Minus_dur), _) as op;
|
2023-12-11 17:59:47 +03:00
|
|
|
args = [arg1];
|
2024-07-12 12:14:58 +03:00
|
|
|
_;
|
2023-12-11 17:59:47 +03:00
|
|
|
} ->
|
2024-04-30 17:35:08 +03:00
|
|
|
Format.fprintf fmt "%a %a" format_op op (format_expression ctx) arg1
|
2024-07-12 12:14:58 +03:00
|
|
|
| EAppOp { op; args = [arg1]; _ } ->
|
2024-07-08 15:44:55 +03:00
|
|
|
Format.fprintf fmt "%a(%a)" format_op op (format_expression ctx) arg1
|
2023-12-11 17:59:47 +03:00
|
|
|
| EApp { f; args } ->
|
2024-07-08 15:44:55 +03:00
|
|
|
Format.fprintf fmt "%a(@[<hv 0>%a)@]" (format_expression ctx) f
|
2021-06-24 18:50:08 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
|
|
|
|
(format_expression ctx))
|
2021-06-21 19:00:06 +03:00
|
|
|
args
|
2024-07-12 12:14:58 +03:00
|
|
|
| EAppOp { op; args; _ } ->
|
2024-07-08 15:44:55 +03:00
|
|
|
Format.fprintf fmt "%a(@[<hv 0>%a)@]" format_op op
|
2023-12-18 12:25:00 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
|
|
|
|
(format_expression ctx))
|
|
|
|
args
|
2024-01-30 19:09:09 +03:00
|
|
|
| ETuple es ->
|
|
|
|
Format.fprintf fmt "(%a)"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
|
|
|
|
(fun fmt e -> Format.fprintf fmt "%a" (format_expression ctx) e))
|
|
|
|
es
|
2024-07-15 18:31:05 +03:00
|
|
|
| ETupleAccess { e1; index; _ } ->
|
2024-01-30 19:09:09 +03:00
|
|
|
Format.fprintf fmt "%a[%d]" (format_expression ctx) e1 index
|
2024-02-22 14:14:25 +03:00
|
|
|
| EExternal { modname; name } ->
|
2024-08-07 18:43:14 +03:00
|
|
|
Format.fprintf fmt "%a.%s" VarName.format (Mark.remove modname)
|
|
|
|
(Mark.remove name)
|
2021-06-21 19:00:06 +03:00
|
|
|
|
2024-02-22 14:14:25 +03:00
|
|
|
let rec format_statement ctx (fmt : Format.formatter) (s : stmt Mark.pos) : unit
|
|
|
|
=
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove s with
|
2023-12-11 17:59:47 +03:00
|
|
|
| SInnerFuncDef { name; func = { func_params; func_body; _ } } ->
|
2024-08-07 18:43:14 +03:00
|
|
|
Format.fprintf fmt "@[<v 4>def %a(@[<hov>%a@]):@ %a@]" VarName.format
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.remove name)
|
2021-06-24 18:50:08 +03:00
|
|
|
(Format.pp_print_list
|
2024-07-08 15:44:55 +03:00
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
|
2021-06-24 18:50:08 +03:00
|
|
|
(fun fmt (var, typ) ->
|
2024-08-07 18:43:14 +03:00
|
|
|
Format.fprintf fmt "%a:%a" VarName.format (Mark.remove var)
|
2024-02-22 14:14:25 +03:00
|
|
|
(format_typ ctx) typ))
|
2021-06-24 18:50:08 +03:00
|
|
|
func_params (format_block ctx) func_body
|
2021-06-24 19:17:54 +03:00
|
|
|
| SLocalDecl _ ->
|
|
|
|
assert false (* We don't need to declare variables in Python *)
|
2023-12-12 20:21:20 +03:00
|
|
|
| SLocalDef { name = v; expr = e; _ } | SLocalInit { name = v; expr = e; _ }
|
|
|
|
->
|
2024-08-09 12:10:47 +03:00
|
|
|
Format.fprintf fmt "@[<hv 4>%a = (%a)@]" VarName.format (Mark.remove v)
|
2021-06-24 19:17:54 +03:00
|
|
|
(format_expression ctx) e
|
2024-08-20 16:39:11 +03:00
|
|
|
| SFatalError { pos_expr; error } ->
|
|
|
|
Format.fprintf fmt "@[<hov 4>raise %s(%a)@]"
|
|
|
|
(Runtime.error_to_string error)
|
|
|
|
(format_expression ctx) pos_expr
|
2023-12-11 17:59:47 +03:00
|
|
|
| SIfThenElse { if_expr = cond; then_block = b1; else_block = b2 } ->
|
2024-07-08 15:44:55 +03:00
|
|
|
Format.fprintf fmt "@[<v 4>if %a:@ %a@]@,@[<v 4>else:@ %a@]"
|
2021-06-24 18:50:08 +03:00
|
|
|
(format_expression ctx) cond (format_block ctx) b1 (format_block ctx) b2
|
2023-12-11 17:59:47 +03:00
|
|
|
| SSwitch
|
|
|
|
{
|
2024-08-08 16:06:03 +03:00
|
|
|
switch_var;
|
2023-12-11 17:59:47 +03:00
|
|
|
enum_name = e_name;
|
|
|
|
switch_cases =
|
|
|
|
[
|
|
|
|
{ case_block = case_none; _ };
|
2024-09-05 17:56:18 +03:00
|
|
|
{
|
|
|
|
case_block = case_some;
|
|
|
|
payload_var_name = case_some_var;
|
|
|
|
payload_var_typ;
|
|
|
|
};
|
2023-12-11 17:59:47 +03:00
|
|
|
];
|
2023-12-11 19:08:32 +03:00
|
|
|
_;
|
2023-12-11 17:59:47 +03:00
|
|
|
}
|
2023-05-17 14:26:47 +03:00
|
|
|
when EnumName.equal e_name Expr.option_enum ->
|
2022-02-25 14:25:42 +03:00
|
|
|
(* We translate the option type with an overloading by Python's [None] *)
|
2024-09-05 17:56:18 +03:00
|
|
|
let pos = Mark.get s in
|
2024-08-08 16:06:03 +03:00
|
|
|
Format.fprintf fmt "@[<v 4>if %a is None:@ %a@]@," VarName.format switch_var
|
2024-04-22 18:55:36 +03:00
|
|
|
(format_block ctx) case_none;
|
2024-09-05 17:56:18 +03:00
|
|
|
Format.fprintf fmt "@[<v 4>else:@ %a@]" (format_block ctx)
|
|
|
|
(Utils.subst_block case_some_var (EVar switch_var, pos) payload_var_typ
|
|
|
|
pos case_some)
|
2024-08-08 16:06:03 +03:00
|
|
|
| SSwitch { switch_var; enum_name = e_name; switch_cases = cases; _ } ->
|
2024-02-22 14:14:25 +03:00
|
|
|
let cons_map = EnumName.Map.find e_name ctx.decl_ctx.ctx_enums in
|
2024-09-05 17:56:18 +03:00
|
|
|
let pos = Mark.get s in
|
2021-06-24 18:50:08 +03:00
|
|
|
let cases =
|
|
|
|
List.map2
|
2023-12-11 17:59:47 +03:00
|
|
|
(fun x (cons, _) -> x, cons)
|
2021-06-24 18:50:08 +03:00
|
|
|
cases
|
2023-08-10 17:52:39 +03:00
|
|
|
(EnumConstructor.Map.bindings cons_map)
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2024-08-08 16:06:03 +03:00
|
|
|
Format.fprintf fmt "@[<hov 4>if %a@]"
|
2021-06-24 18:50:08 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@]@\n@[<hov 4>elif ")
|
2024-09-05 17:56:18 +03:00
|
|
|
(fun fmt (case, cons_name) ->
|
|
|
|
Format.fprintf fmt "%a.code == %a_Code.%a:@," VarName.format
|
2024-09-24 16:29:29 +03:00
|
|
|
switch_var (format_enum ctx) e_name EnumConstructor.format
|
|
|
|
cons_name;
|
2024-09-05 17:56:18 +03:00
|
|
|
format_block ctx fmt
|
|
|
|
(Utils.subst_block case.payload_var_name
|
|
|
|
(* Not a real catala struct, but will print as <var>.value *)
|
|
|
|
( EStructFieldAccess
|
|
|
|
{
|
|
|
|
e1 = EVar switch_var, pos;
|
|
|
|
field = StructField.fresh ("value", pos);
|
|
|
|
name = StructName.fresh [] ("Dummy", pos);
|
|
|
|
},
|
|
|
|
pos )
|
|
|
|
case.payload_var_typ pos case.case_block)))
|
2022-05-04 18:40:55 +03:00
|
|
|
cases
|
2021-06-24 18:50:08 +03:00
|
|
|
| SReturn e1 ->
|
|
|
|
Format.fprintf fmt "@[<hov 4>return %a@]" (format_expression ctx) e1
|
2024-08-20 16:39:11 +03:00
|
|
|
| SAssert { pos_expr; expr = e1 } ->
|
2024-08-26 16:44:00 +03:00
|
|
|
Format.fprintf fmt "@[<hv 4>if not (%a):@,raise AssertionFailed(%a)@]"
|
|
|
|
(format_expression ctx) e1 (format_expression ctx) pos_expr
|
2024-07-15 18:31:05 +03:00
|
|
|
| SSpecialOp _ -> .
|
2021-06-24 18:50:08 +03:00
|
|
|
|
2024-02-22 14:14:25 +03:00
|
|
|
and format_block ctx (fmt : Format.formatter) (b : block) : unit =
|
2024-07-08 15:44:55 +03:00
|
|
|
Format.pp_open_vbox fmt 0;
|
2024-08-09 12:10:47 +03:00
|
|
|
Format.pp_print_list (format_statement ctx) fmt
|
2021-06-24 19:17:54 +03:00
|
|
|
(List.filter
|
2023-05-17 16:44:57 +03:00
|
|
|
(fun s -> match Mark.remove s with SLocalDecl _ -> false | _ -> true)
|
2024-07-08 15:44:55 +03:00
|
|
|
b);
|
|
|
|
Format.pp_close_box fmt ()
|
2021-06-24 18:50:08 +03:00
|
|
|
|
2024-08-30 16:58:52 +03:00
|
|
|
let format_ctx (type_ordering : TypeIdent.t list) (fmt : Format.formatter) ctx :
|
|
|
|
unit =
|
2021-06-21 19:00:06 +03:00
|
|
|
let format_struct_decl fmt (struct_name, struct_fields) =
|
2022-11-21 12:12:45 +03:00
|
|
|
let fields = StructField.Map.bindings struct_fields in
|
2022-02-01 17:41:53 +03:00
|
|
|
Format.fprintf fmt
|
2024-07-08 15:44:55 +03:00
|
|
|
"class %a:@,\
|
|
|
|
\ def __init__(self, %a) -> None:@,\
|
|
|
|
%a@,\
|
|
|
|
@,\
|
|
|
|
\ def __eq__(self, other: object) -> bool:@,\
|
|
|
|
\ if isinstance(other, %a):@,\
|
|
|
|
\ return @[<hov>(%a)@]@,\
|
|
|
|
\ else:@,\
|
|
|
|
\ return False@,\
|
|
|
|
@,\
|
|
|
|
\ def __ne__(self, other: object) -> bool:@,\
|
|
|
|
\ return not (self == other)@,\
|
|
|
|
@,\
|
|
|
|
\ def __str__(self) -> str:@,\
|
2024-08-07 18:43:14 +03:00
|
|
|
\ @[<hov 4>return \"%a(%a)\".format(%a)@]" StructName.format
|
2022-02-01 17:41:53 +03:00
|
|
|
struct_name
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ", ")
|
2022-11-17 19:13:35 +03:00
|
|
|
(fun fmt (struct_field, struct_field_type) ->
|
2024-08-07 18:43:14 +03:00
|
|
|
Format.fprintf fmt "%a: %a" StructField.format struct_field
|
2024-02-22 14:14:25 +03:00
|
|
|
(format_typ ctx) struct_field_type))
|
2022-11-17 19:13:35 +03:00
|
|
|
fields
|
2022-11-21 12:12:45 +03:00
|
|
|
(if StructField.Map.is_empty struct_fields then fun fmt _ ->
|
2022-08-04 18:37:27 +03:00
|
|
|
Format.fprintf fmt " pass"
|
2022-02-01 17:41:53 +03:00
|
|
|
else
|
2024-07-08 15:44:55 +03:00
|
|
|
Format.pp_print_list (fun fmt (struct_field, _) ->
|
2024-08-07 18:43:14 +03:00
|
|
|
Format.fprintf fmt " self.%a = %a" StructField.format
|
|
|
|
struct_field StructField.format struct_field))
|
|
|
|
fields StructName.format struct_name
|
2022-11-21 12:12:45 +03:00
|
|
|
(if not (StructField.Map.is_empty struct_fields) then
|
2022-02-07 20:38:31 +03:00
|
|
|
Format.pp_print_list
|
2022-02-01 17:41:53 +03:00
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt " and@ ")
|
2022-11-17 19:13:35 +03:00
|
|
|
(fun fmt (struct_field, _) ->
|
2024-08-07 18:43:14 +03:00
|
|
|
Format.fprintf fmt "self.%a == other.%a" StructField.format
|
|
|
|
struct_field StructField.format struct_field)
|
2022-02-07 20:38:31 +03:00
|
|
|
else fun fmt _ -> Format.fprintf fmt "True")
|
2024-08-07 18:43:14 +03:00
|
|
|
fields StructName.format struct_name
|
2022-02-01 17:41:53 +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, _) ->
|
2024-08-07 18:43:14 +03:00
|
|
|
Format.fprintf fmt "%a={}" StructField.format struct_field))
|
2022-11-17 19:13:35 +03:00
|
|
|
fields
|
2022-02-01 17:41:53 +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, _) ->
|
2024-08-07 18:43:14 +03:00
|
|
|
Format.fprintf fmt "self.%a" StructField.format struct_field))
|
2022-11-17 19:13:35 +03:00
|
|
|
fields
|
2021-06-21 19:00:06 +03:00
|
|
|
in
|
|
|
|
let format_enum_decl fmt (enum_name, enum_cons) =
|
2022-11-21 12:12:45 +03:00
|
|
|
if EnumConstructor.Map.is_empty enum_cons then
|
2022-11-17 19:13:35 +03:00
|
|
|
failwith "no constructors in the enum"
|
2021-06-21 19:00:06 +03:00
|
|
|
else
|
2021-06-24 19:17:54 +03:00
|
|
|
Format.fprintf fmt
|
2024-07-08 15:44:55 +03:00
|
|
|
"@[<v 4>class %a_Code(Enum):@,\
|
|
|
|
%a@]@,\
|
|
|
|
@,\
|
|
|
|
class %a:@,\
|
|
|
|
\ def __init__(self, code: %a_Code, value: Any) -> None:@,\
|
|
|
|
\ self.code = code@,\
|
|
|
|
\ self.value = value@,\
|
|
|
|
@,\
|
|
|
|
@,\
|
|
|
|
\ def __eq__(self, other: object) -> bool:@,\
|
|
|
|
\ if isinstance(other, %a):@,\
|
2022-08-04 18:37:27 +03:00
|
|
|
\ return self.code == other.code and self.value == \
|
2024-07-08 15:44:55 +03:00
|
|
|
other.value@,\
|
|
|
|
\ else:@,\
|
|
|
|
\ return False@,\
|
|
|
|
@,\
|
|
|
|
@,\
|
|
|
|
\ def __ne__(self, other: object) -> bool:@,\
|
|
|
|
\ return not (self == other)@,\
|
|
|
|
@,\
|
|
|
|
\ def __str__(self) -> str:@,\
|
2022-08-04 18:37:27 +03:00
|
|
|
\ @[<hov 4>return \"{}({})\".format(self.code, self.value)@]"
|
2024-08-07 18:43:14 +03:00
|
|
|
EnumName.format enum_name
|
2024-07-08 15:44:55 +03:00
|
|
|
(Format.pp_print_list (fun fmt (i, enum_cons, _enum_cons_type) ->
|
2024-08-07 18:43:14 +03:00
|
|
|
Format.fprintf fmt "%a = %d" EnumConstructor.format enum_cons i))
|
2022-11-17 19:13:35 +03:00
|
|
|
(List.mapi
|
|
|
|
(fun i (x, y) -> i, x, y)
|
2022-11-21 12:12:45 +03:00
|
|
|
(EnumConstructor.Map.bindings enum_cons))
|
2024-08-07 18:43:14 +03:00
|
|
|
EnumName.format enum_name EnumName.format enum_name EnumName.format
|
|
|
|
enum_name
|
2021-06-21 19:00:06 +03:00
|
|
|
in
|
2021-06-24 22:55:20 +03:00
|
|
|
|
2021-06-21 19:00:06 +03:00
|
|
|
let is_in_type_ordering s =
|
|
|
|
List.exists
|
|
|
|
(fun struct_or_enum ->
|
|
|
|
match struct_or_enum with
|
2024-08-29 17:26:51 +03:00
|
|
|
| TypeIdent.Enum _ -> false
|
|
|
|
| TypeIdent.Struct s' -> s = s')
|
2021-06-21 19:00:06 +03:00
|
|
|
type_ordering
|
|
|
|
in
|
|
|
|
let scope_structs =
|
|
|
|
List.map
|
2024-08-29 17:26:51 +03:00
|
|
|
(fun (s, _) -> TypeIdent.Struct s)
|
2022-11-21 12:12:45 +03:00
|
|
|
(StructName.Map.bindings
|
|
|
|
(StructName.Map.filter
|
2021-06-21 19:00:06 +03:00
|
|
|
(fun s _ -> not (is_in_type_ordering s))
|
2024-02-22 14:14:25 +03:00
|
|
|
ctx.decl_ctx.ctx_structs))
|
2021-06-21 19:00:06 +03:00
|
|
|
in
|
|
|
|
List.iter
|
|
|
|
(fun struct_or_enum ->
|
|
|
|
match struct_or_enum with
|
2024-08-29 17:26:51 +03:00
|
|
|
| TypeIdent.Struct s ->
|
2024-02-22 14:14:25 +03:00
|
|
|
if StructName.path s = [] then
|
2024-07-08 15:44:55 +03:00
|
|
|
Format.fprintf fmt "%a@,@," format_struct_decl
|
2024-02-22 14:14:25 +03:00
|
|
|
(s, StructName.Map.find s ctx.decl_ctx.ctx_structs)
|
2024-08-29 17:26:51 +03:00
|
|
|
| TypeIdent.Enum e ->
|
2024-02-22 14:14:25 +03:00
|
|
|
if EnumName.path e = [] then
|
2024-07-08 15:44:55 +03:00
|
|
|
Format.fprintf fmt "%a@,@," format_enum_decl
|
2024-02-22 14:14:25 +03:00
|
|
|
(e, EnumName.Map.find e ctx.decl_ctx.ctx_enums))
|
2021-06-21 19:00:06 +03:00
|
|
|
(type_ordering @ scope_structs)
|
|
|
|
|
2024-02-22 14:14:25 +03:00
|
|
|
let format_code_item ctx fmt = function
|
2024-09-23 17:40:38 +03:00
|
|
|
| SVar { var; expr; typ = _; visibility = _ } ->
|
2024-08-07 18:43:14 +03:00
|
|
|
Format.fprintf fmt "@[<hv 4>%a = (@,%a@;<0 -4>)@]@," VarName.format var
|
2024-02-22 14:14:25 +03:00
|
|
|
(format_expression ctx) expr
|
2024-09-23 17:40:38 +03:00
|
|
|
| SFunc { var; func; visibility = _ }
|
2024-02-22 14:14:25 +03:00
|
|
|
| SScope { scope_body_var = var; scope_body_func = func; _ } ->
|
|
|
|
let { Ast.func_params; Ast.func_body; _ } = func in
|
2024-07-08 15:44:55 +03:00
|
|
|
Format.fprintf fmt "@[<v 4>@[<hov 2>def %a(@,%a@;<0 -2>):@]@ %a@]@,"
|
|
|
|
format_func_name var
|
2024-02-22 14:14:25 +03:00
|
|
|
(Format.pp_print_list
|
2024-07-08 15:44:55 +03:00
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
|
2024-02-22 14:14:25 +03:00
|
|
|
(fun fmt (var, typ) ->
|
2024-08-07 18:43:14 +03:00
|
|
|
Format.fprintf fmt "%a:%a" VarName.format (Mark.remove var)
|
2024-02-22 14:14:25 +03:00
|
|
|
(format_typ ctx) typ))
|
|
|
|
func_params (format_block ctx) func_body
|
|
|
|
|
2021-06-21 19:00:06 +03:00
|
|
|
let format_program
|
|
|
|
(fmt : Format.formatter)
|
|
|
|
(p : Ast.program)
|
2024-08-29 17:26:51 +03:00
|
|
|
(type_ordering : TypeIdent.t list) : unit =
|
2024-02-22 14:14:25 +03:00
|
|
|
Format.pp_open_vbox fmt 0;
|
|
|
|
let header =
|
|
|
|
[
|
|
|
|
"# This file has been generated by the Catala compiler, do not edit!";
|
|
|
|
"";
|
|
|
|
"from catala.runtime import *";
|
|
|
|
"from typing import Any, List, Callable, Tuple";
|
|
|
|
"from enum import Enum";
|
|
|
|
"";
|
|
|
|
]
|
|
|
|
in
|
|
|
|
Format.pp_print_list Format.pp_print_string fmt header;
|
|
|
|
ModuleName.Map.iter
|
|
|
|
(fun m v ->
|
2024-03-08 19:32:47 +03:00
|
|
|
Format.fprintf fmt "from . import %a as %a@," ModuleName.format m
|
2024-08-07 18:43:14 +03:00
|
|
|
VarName.format v)
|
2024-02-22 14:14:25 +03:00
|
|
|
p.ctx.modules;
|
|
|
|
Format.pp_print_cut fmt ();
|
|
|
|
format_ctx type_ordering fmt p.ctx;
|
|
|
|
Format.pp_print_cut fmt ();
|
|
|
|
Format.pp_print_list (format_code_item p.ctx) fmt p.code_items;
|
|
|
|
Format.pp_print_flush fmt ()
|