2022-08-17 17:14: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>
|
|
|
|
|
|
|
|
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. *)
|
|
|
|
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2022-08-22 19:53:30 +03:00
|
|
|
open Definitions
|
2022-08-17 17:14:14 +03:00
|
|
|
|
2022-08-25 18:29:00 +03:00
|
|
|
let typ_needs_parens (ty : typ) : bool =
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove ty with TArrow _ | TArray _ -> true | _ -> false
|
2022-08-17 17:14:14 +03:00
|
|
|
|
|
|
|
let uid_list (fmt : Format.formatter) (infos : Uid.MarkedString.info list) :
|
|
|
|
unit =
|
|
|
|
Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.pp_print_char fmt '.')
|
|
|
|
(fun fmt info ->
|
2023-06-07 19:10:50 +03:00
|
|
|
Format.fprintf fmt
|
|
|
|
(if String.begins_with_uppercase (Mark.remove info) then "@{<red>%s@}"
|
|
|
|
else "%s")
|
|
|
|
(Uid.MarkedString.to_string info))
|
2022-08-17 17:14:14 +03:00
|
|
|
fmt infos
|
|
|
|
|
2023-06-07 19:10:50 +03:00
|
|
|
let with_color f color fmt x =
|
|
|
|
(* equivalent to [Format.fprintf fmt "@{<color>%s@}" s] *)
|
|
|
|
Format.pp_open_stag fmt Ocolor_format.(Ocolor_style_tag (Fg (C4 color)));
|
|
|
|
f fmt x;
|
|
|
|
Format.pp_close_stag fmt ()
|
|
|
|
|
|
|
|
let pp_color_string = with_color Format.pp_print_string
|
|
|
|
|
2023-07-11 18:10:00 +03:00
|
|
|
(* Cyclic list used to choose nested paren colors *)
|
|
|
|
let rec colors =
|
|
|
|
let open Ocolor_types in
|
|
|
|
blue :: cyan :: green :: yellow :: red :: magenta :: colors
|
|
|
|
|
2022-08-17 17:14:14 +03:00
|
|
|
let keyword (fmt : Format.formatter) (s : string) : unit =
|
2023-06-07 19:10:50 +03:00
|
|
|
pp_color_string Ocolor_types.red fmt s
|
2022-08-17 17:14:14 +03:00
|
|
|
|
|
|
|
let base_type (fmt : Format.formatter) (s : string) : unit =
|
2023-06-07 19:10:50 +03:00
|
|
|
pp_color_string Ocolor_types.yellow fmt s
|
2022-08-17 17:14:14 +03:00
|
|
|
|
|
|
|
let punctuation (fmt : Format.formatter) (s : string) : unit =
|
2023-06-07 19:10:50 +03:00
|
|
|
with_color (fun fmt -> Format.pp_print_as fmt 1) Ocolor_types.cyan fmt s
|
2022-08-17 17:14:14 +03:00
|
|
|
|
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
|
|
|
let op_style (fmt : Format.formatter) (s : string) : unit =
|
2023-06-07 19:10:50 +03:00
|
|
|
pp_color_string Ocolor_types.green fmt s
|
2022-08-17 17:14:14 +03:00
|
|
|
|
|
|
|
let lit_style (fmt : Format.formatter) (s : string) : unit =
|
2023-06-07 19:10:50 +03:00
|
|
|
pp_color_string Ocolor_types.yellow fmt s
|
2022-08-17 17:14:14 +03:00
|
|
|
|
|
|
|
let tlit (fmt : Format.formatter) (l : typ_lit) : unit =
|
|
|
|
base_type fmt
|
|
|
|
(match l with
|
|
|
|
| TUnit -> "unit"
|
|
|
|
| TBool -> "bool"
|
|
|
|
| TInt -> "integer"
|
|
|
|
| TRat -> "decimal"
|
|
|
|
| TMoney -> "money"
|
|
|
|
| TDuration -> "duration"
|
|
|
|
| TDate -> "date")
|
|
|
|
|
2022-08-25 13:09:51 +03:00
|
|
|
let location (type a) (fmt : Format.formatter) (l : a glocation) : unit =
|
2022-08-17 19:14:30 +03:00
|
|
|
match l with
|
2023-08-10 17:52:39 +03:00
|
|
|
| DesugaredScopeVar { name; _ } -> ScopeVar.format fmt (Mark.remove name)
|
|
|
|
| ScopelangScopeVar { name; _ } -> ScopeVar.format fmt (Mark.remove name)
|
|
|
|
| ToplevelVar { name } -> TopdefName.format fmt (Mark.remove name)
|
2022-08-17 19:14:30 +03:00
|
|
|
|
2023-08-10 17:52:39 +03:00
|
|
|
let external_ref fmt er =
|
|
|
|
match Mark.remove er with
|
|
|
|
| External_value v -> TopdefName.format fmt v
|
|
|
|
| External_scope s -> ScopeName.format fmt s
|
|
|
|
|
2023-07-11 18:10:00 +03:00
|
|
|
let rec typ_gen
|
2023-06-13 09:59:33 +03:00
|
|
|
(ctx : decl_ctx option)
|
|
|
|
~(colors : Ocolor_types.color4 list)
|
|
|
|
(fmt : Format.formatter)
|
|
|
|
(ty : typ) : unit =
|
2023-07-11 18:10:00 +03:00
|
|
|
let typ = typ_gen ctx in
|
2023-06-13 09:59:33 +03:00
|
|
|
let typ_with_parens ~colors (fmt : Format.formatter) (t : typ) =
|
|
|
|
if typ_needs_parens t then (
|
|
|
|
Format.pp_open_hvbox fmt 1;
|
|
|
|
pp_color_string (List.hd colors) fmt "(";
|
|
|
|
typ ~colors:(List.tl colors) fmt t;
|
|
|
|
Format.pp_close_box fmt ();
|
|
|
|
pp_color_string (List.hd colors) fmt ")")
|
|
|
|
else typ ~colors fmt t
|
2022-08-17 17:14:14 +03:00
|
|
|
in
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove ty with
|
2022-08-17 17:14:14 +03:00
|
|
|
| TLit l -> tlit fmt l
|
2022-08-23 16:23:52 +03:00
|
|
|
| TTuple ts ->
|
2023-06-13 09:59:33 +03:00
|
|
|
Format.pp_open_hvbox fmt 2;
|
|
|
|
pp_color_string (List.hd colors) fmt "(";
|
|
|
|
(Format.pp_print_list
|
2024-06-19 17:42:45 +03:00
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "%a@ " op_style ",")
|
2023-06-13 09:59:33 +03:00
|
|
|
(typ ~colors:(List.tl colors)))
|
|
|
|
fmt ts;
|
|
|
|
Format.pp_close_box fmt ();
|
|
|
|
pp_color_string (List.hd colors) fmt ")"
|
2023-04-17 16:10:47 +03:00
|
|
|
| TStruct s -> (
|
|
|
|
match ctx with
|
2023-08-10 17:52:39 +03:00
|
|
|
| None -> StructName.format fmt s
|
2023-04-17 16:10:47 +03:00
|
|
|
| Some ctx ->
|
2023-08-30 18:49:29 +03:00
|
|
|
let fields = StructName.Map.find s ctx.ctx_structs in
|
2023-08-10 17:52:39 +03:00
|
|
|
if StructField.Map.is_empty fields then StructName.format fmt s
|
2023-05-02 17:33:23 +03:00
|
|
|
else
|
2023-08-30 18:49:29 +03:00
|
|
|
Format.fprintf fmt "@[<hv 2>%a %a@,%a@;<0 -2>%a@]" StructName.format s
|
2023-06-13 09:59:33 +03:00
|
|
|
(pp_color_string (List.hd colors))
|
|
|
|
"{"
|
2023-07-12 12:48:46 +03:00
|
|
|
(StructField.Map.format_bindings
|
2023-05-02 17:33:23 +03:00
|
|
|
~pp_sep:(fun fmt () ->
|
2023-06-13 09:59:33 +03:00
|
|
|
op_style fmt ";";
|
2023-05-02 17:33:23 +03:00
|
|
|
Format.pp_print_space fmt ())
|
2023-07-12 12:48:46 +03:00
|
|
|
(fun fmt pp_field_name field_typ ->
|
|
|
|
Format.fprintf fmt "@[<hv 2>%t%a@ %a@]" pp_field_name punctuation
|
|
|
|
":"
|
2023-06-13 09:59:33 +03:00
|
|
|
(typ ~colors:(List.tl colors))
|
|
|
|
field_typ))
|
2023-07-12 12:48:46 +03:00
|
|
|
fields
|
2023-06-13 09:59:33 +03:00
|
|
|
(pp_color_string (List.hd colors))
|
|
|
|
"}")
|
2023-04-17 16:10:47 +03:00
|
|
|
| TEnum e -> (
|
|
|
|
match ctx with
|
2023-07-12 12:48:46 +03:00
|
|
|
| None -> Format.fprintf fmt "@[<hov 2>%a@]" EnumName.format e
|
2023-04-17 16:10:47 +03:00
|
|
|
| Some ctx ->
|
2023-08-30 18:49:29 +03:00
|
|
|
let def = EnumName.Map.find e ctx.ctx_enums in
|
|
|
|
Format.fprintf fmt "@[<hov 2>%a%a%a%a@]" EnumName.format e punctuation "["
|
2023-07-12 12:48:46 +03:00
|
|
|
(EnumConstructor.Map.format_bindings
|
2023-04-17 16:10:47 +03:00
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@ %a@ " punctuation "|")
|
2023-07-12 12:48:46 +03:00
|
|
|
(fun fmt pp_case mty ->
|
|
|
|
Format.fprintf fmt "%t%a@ %a" pp_case punctuation ":" (typ ~colors)
|
|
|
|
mty))
|
2023-04-17 16:10:47 +03:00
|
|
|
def punctuation "]")
|
2023-06-13 09:59:33 +03:00
|
|
|
| TOption t ->
|
2023-06-13 10:19:14 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ %a@]" base_type "eoption" (typ ~colors) t
|
2023-02-22 13:52:22 +03:00
|
|
|
| TArrow ([t1], t2) ->
|
2023-06-13 09:59:33 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ %a@ %a@]" (typ_with_parens ~colors) t1
|
|
|
|
op_style "→" (typ ~colors) t2
|
2022-08-17 17:14:14 +03:00
|
|
|
| TArrow (t1, t2) ->
|
2023-06-13 09:59:33 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a%a%a@ %a@ %a@]"
|
|
|
|
(pp_color_string (List.hd colors))
|
|
|
|
"("
|
2023-01-11 13:23:08 +03:00
|
|
|
(Format.pp_print_list
|
2023-02-22 13:52:22 +03:00
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "%a@ " op_style ",")
|
2023-06-13 09:59:33 +03:00
|
|
|
(typ_with_parens ~colors:(List.tl colors)))
|
|
|
|
t1
|
|
|
|
(pp_color_string (List.hd colors))
|
|
|
|
")" op_style "→"
|
|
|
|
(typ ~colors:(List.tl colors))
|
|
|
|
t2
|
2022-09-26 17:05:57 +03:00
|
|
|
| TArray t1 ->
|
2023-12-05 17:06:28 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ %a@]" base_type "list of" (typ ~colors) t1
|
2023-10-31 13:58:54 +03:00
|
|
|
| TDefault t1 ->
|
|
|
|
punctuation fmt "⟨";
|
|
|
|
typ ~colors fmt t1;
|
|
|
|
punctuation fmt "⟩"
|
2022-08-17 17:14:14 +03:00
|
|
|
| TAny -> base_type fmt "any"
|
2023-06-15 12:11:56 +03:00
|
|
|
| TClosureEnv -> base_type fmt "closure_env"
|
2022-08-17 17:14:14 +03:00
|
|
|
|
2023-07-11 18:10:00 +03:00
|
|
|
let typ_debug = typ_gen None ~colors
|
|
|
|
let typ ctx = typ_gen (Some ctx) ~colors
|
|
|
|
|
2023-03-30 19:53:07 +03:00
|
|
|
let lit (fmt : Format.formatter) (l : lit) : unit =
|
2022-08-17 17:14:14 +03:00
|
|
|
match l with
|
|
|
|
| LBool b -> lit_style fmt (string_of_bool b)
|
|
|
|
| LInt i -> lit_style fmt (Runtime.integer_to_string i)
|
|
|
|
| LUnit -> lit_style fmt "()"
|
|
|
|
| LRat i ->
|
|
|
|
lit_style fmt
|
2024-03-19 17:23:06 +03:00
|
|
|
(Runtime.decimal_to_string ~max_prec_digits:Global.options.max_prec_digits
|
|
|
|
i)
|
2023-06-28 16:57:52 +03:00
|
|
|
| LMoney e ->
|
|
|
|
lit_style fmt (Format.asprintf "¤%s" (Runtime.money_to_string e))
|
2022-08-17 17:14:14 +03:00
|
|
|
| LDate d -> lit_style fmt (Runtime.date_to_string d)
|
|
|
|
| LDuration d -> lit_style fmt (Runtime.duration_to_string d)
|
|
|
|
|
|
|
|
let log_entry (fmt : Format.formatter) (entry : log_entry) : unit =
|
2023-06-07 19:10:50 +03:00
|
|
|
match entry with
|
|
|
|
| VarDef _ -> Format.fprintf fmt "@{<blue>@<1>%s @}" "≔"
|
|
|
|
| BeginCall -> Format.fprintf fmt "@{<yellow>@<1>%s @}" "→"
|
|
|
|
| EndCall -> Format.fprintf fmt "@{<yellow>@<1>%s @}" "←"
|
|
|
|
| PosRecordIfTrueBool -> Format.fprintf fmt "@{<green>@<1>%s @}" "☛"
|
2022-08-17 17:14:14 +03:00
|
|
|
|
2023-03-30 16:30:08 +03:00
|
|
|
let operator_to_string : type a. a Op.t -> string =
|
|
|
|
let open Op in
|
|
|
|
function
|
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
|
|
|
| Not -> "~"
|
|
|
|
| Length -> "length"
|
|
|
|
| GetDay -> "get_day"
|
|
|
|
| GetMonth -> "get_month"
|
|
|
|
| GetYear -> "get_year"
|
|
|
|
| FirstDayOfMonth -> "first_day_of_month"
|
|
|
|
| LastDayOfMonth -> "last_day_of_month"
|
2022-12-13 15:28:01 +03:00
|
|
|
| ToRat -> "to_rat"
|
|
|
|
| ToRat_int -> "to_rat_int"
|
|
|
|
| ToRat_mon -> "to_rat_mon"
|
|
|
|
| ToMoney -> "to_mon"
|
|
|
|
| ToMoney_rat -> "to_mon_rat"
|
|
|
|
| Round -> "round"
|
|
|
|
| Round_rat -> "round_rat"
|
|
|
|
| Round_mon -> "round_mon"
|
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
|
|
|
| Log _ -> "Log"
|
|
|
|
| Minus -> "-"
|
|
|
|
| Minus_int -> "-!"
|
|
|
|
| Minus_rat -> "-."
|
|
|
|
| Minus_mon -> "-$"
|
|
|
|
| Minus_dur -> "-^"
|
|
|
|
| And -> "&&"
|
|
|
|
| Or -> "||"
|
|
|
|
| Xor -> "xor"
|
|
|
|
| Eq -> "="
|
|
|
|
| Map -> "map"
|
2024-01-24 17:36:51 +03:00
|
|
|
| Map2 -> "map2"
|
2022-12-12 18:02:07 +03:00
|
|
|
| Reduce -> "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
|
|
|
| Concat -> "++"
|
|
|
|
| Filter -> "filter"
|
|
|
|
| Add -> "+"
|
|
|
|
| Add_int_int -> "+!"
|
|
|
|
| Add_rat_rat -> "+."
|
|
|
|
| Add_mon_mon -> "+$"
|
2023-01-20 20:18:53 +03:00
|
|
|
| Add_dat_dur AbortOnRound -> "+@"
|
|
|
|
| Add_dat_dur RoundUp -> "+@u"
|
|
|
|
| Add_dat_dur RoundDown -> "+@d"
|
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
|
|
|
| Add_dur_dur -> "+^"
|
|
|
|
| Sub -> "-"
|
|
|
|
| Sub_int_int -> "-!"
|
|
|
|
| Sub_rat_rat -> "-."
|
|
|
|
| Sub_mon_mon -> "-$"
|
|
|
|
| Sub_dat_dat -> "-@"
|
|
|
|
| Sub_dat_dur -> "-@^"
|
|
|
|
| Sub_dur_dur -> "-^"
|
|
|
|
| Mult -> "*"
|
|
|
|
| Mult_int_int -> "*!"
|
|
|
|
| Mult_rat_rat -> "*."
|
|
|
|
| Mult_mon_rat -> "*$"
|
|
|
|
| Mult_dur_int -> "*^"
|
|
|
|
| Div -> "/"
|
|
|
|
| Div_int_int -> "/!"
|
|
|
|
| Div_rat_rat -> "/."
|
|
|
|
| Div_mon_mon -> "/$"
|
|
|
|
| Div_mon_rat -> "/$."
|
2023-03-06 12:54:51 +03:00
|
|
|
| Div_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
|
|
|
| Lt -> "<"
|
|
|
|
| Lt_int_int -> "<!"
|
|
|
|
| Lt_rat_rat -> "<."
|
|
|
|
| Lt_mon_mon -> "<$"
|
|
|
|
| Lt_dur_dur -> "<^"
|
|
|
|
| Lt_dat_dat -> "<@"
|
|
|
|
| Lte -> "<="
|
|
|
|
| Lte_int_int -> "<=!"
|
|
|
|
| Lte_rat_rat -> "<=."
|
|
|
|
| Lte_mon_mon -> "<=$"
|
|
|
|
| Lte_dur_dur -> "<=^"
|
|
|
|
| Lte_dat_dat -> "<=@"
|
|
|
|
| Gt -> ">"
|
|
|
|
| Gt_int_int -> ">!"
|
|
|
|
| Gt_rat_rat -> ">."
|
|
|
|
| Gt_mon_mon -> ">$"
|
|
|
|
| Gt_dur_dur -> ">^"
|
|
|
|
| Gt_dat_dat -> ">@"
|
|
|
|
| Gte -> ">="
|
|
|
|
| Gte_int_int -> ">=!"
|
|
|
|
| Gte_rat_rat -> ">=."
|
|
|
|
| Gte_mon_mon -> ">=$"
|
|
|
|
| Gte_dur_dur -> ">=^"
|
|
|
|
| Gte_dat_dat -> ">=@"
|
|
|
|
| Eq_int_int -> "=!"
|
|
|
|
| Eq_rat_rat -> "=."
|
|
|
|
| Eq_mon_mon -> "=$"
|
|
|
|
| Eq_dur_dur -> "=^"
|
|
|
|
| Eq_dat_dat -> "=@"
|
|
|
|
| Fold -> "fold"
|
2023-04-07 17:32:43 +03:00
|
|
|
| HandleDefaultOpt -> "handle_default_opt"
|
2023-06-15 17:52:36 +03:00
|
|
|
| ToClosureEnv -> "to_closure_env"
|
|
|
|
| FromClosureEnv -> "from_closure_env"
|
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
|
|
|
|
2023-05-02 12:59:39 +03:00
|
|
|
let operator_to_shorter_string : type a. a Op.t -> string =
|
|
|
|
let open Op in
|
|
|
|
function
|
|
|
|
| Not -> "~"
|
|
|
|
| Length -> "length"
|
|
|
|
| GetDay -> "get_day"
|
|
|
|
| GetMonth -> "get_month"
|
|
|
|
| GetYear -> "get_year"
|
|
|
|
| FirstDayOfMonth -> "first_day_of_month"
|
|
|
|
| LastDayOfMonth -> "last_day_of_month"
|
|
|
|
| ToRat_int | ToRat_mon | ToRat -> "to_rat"
|
|
|
|
| ToMoney_rat | ToMoney -> "to_mon"
|
|
|
|
| Round_rat | Round_mon | Round -> "round"
|
|
|
|
| Log _ -> "Log"
|
|
|
|
| Minus_int | Minus_rat | Minus_mon | Minus_dur | Minus -> "-"
|
|
|
|
| And -> "&&"
|
|
|
|
| Or -> "||"
|
|
|
|
| Xor -> "xor"
|
|
|
|
| Eq_int_int | Eq_rat_rat | Eq_mon_mon | Eq_dur_dur | Eq_dat_dat | Eq -> "="
|
|
|
|
| Map -> "map"
|
2024-01-24 17:36:51 +03:00
|
|
|
| Map2 -> "map2"
|
2023-05-02 12:59:39 +03:00
|
|
|
| Reduce -> "reduce"
|
|
|
|
| Concat -> "++"
|
|
|
|
| Filter -> "filter"
|
|
|
|
| Add_int_int | Add_rat_rat | Add_mon_mon | Add_dat_dur _ | Add_dur_dur | Add
|
|
|
|
->
|
|
|
|
"+"
|
|
|
|
| Sub_int_int | Sub_rat_rat | Sub_mon_mon | Sub_dat_dat | Sub_dat_dur
|
|
|
|
| Sub_dur_dur | Sub ->
|
|
|
|
"-"
|
|
|
|
| Mult_int_int | Mult_rat_rat | Mult_mon_rat | Mult_dur_int | Mult -> "*"
|
|
|
|
| Div_int_int | Div_rat_rat | Div_mon_mon | Div_mon_rat | Div_dur_dur | Div ->
|
|
|
|
"/"
|
|
|
|
| Lt_int_int | Lt_rat_rat | Lt_mon_mon | Lt_dur_dur | Lt_dat_dat | Lt -> "<"
|
|
|
|
| Lte_int_int | Lte_rat_rat | Lte_mon_mon | Lte_dur_dur | Lte_dat_dat | Lte ->
|
|
|
|
"<="
|
|
|
|
| Gt_int_int | Gt_rat_rat | Gt_mon_mon | Gt_dur_dur | Gt_dat_dat | Gt -> ">"
|
|
|
|
| Gte_int_int | Gte_rat_rat | Gte_mon_mon | Gte_dur_dur | Gte_dat_dat | Gte ->
|
|
|
|
">="
|
|
|
|
| Fold -> "fold"
|
|
|
|
| HandleDefaultOpt -> "handle_default_opt"
|
2023-06-15 17:52:36 +03:00
|
|
|
| ToClosureEnv -> "to_closure_env"
|
|
|
|
| FromClosureEnv -> "from_closure_env"
|
2023-05-02 12:59:39 +03:00
|
|
|
|
|
|
|
let operator : type a. ?debug:bool -> Format.formatter -> a operator -> unit =
|
|
|
|
fun ?(debug = true) fmt op ->
|
2023-03-30 16:30:08 +03:00
|
|
|
let open Op in
|
2022-08-17 17:14:14 +03:00
|
|
|
match op with
|
|
|
|
| Log (entry, infos) ->
|
2023-06-07 19:10:50 +03:00
|
|
|
Format.fprintf fmt "@{<blue>#{@}%a%a@{<blue>}@}" log_entry entry
|
2022-08-17 17:14:14 +03:00
|
|
|
(Format.pp_print_list
|
2023-04-11 16:45:04 +03:00
|
|
|
~pp_sep:(fun fmt () -> punctuation fmt ".")
|
2023-06-07 19:10:50 +03:00
|
|
|
(fun fmt info ->
|
|
|
|
Format.fprintf fmt "@{<blue>%s@}" (Uid.MarkedString.to_string info)))
|
2022-08-17 17:14:14 +03:00
|
|
|
infos
|
2023-05-02 12:59:39 +03:00
|
|
|
| op ->
|
|
|
|
op_style fmt
|
|
|
|
(if debug then operator_to_string op else operator_to_shorter_string op)
|
2022-08-17 17:14:14 +03:00
|
|
|
|
2024-04-26 19:31:26 +03:00
|
|
|
let runtime_error ppf err =
|
|
|
|
Format.fprintf ppf "@{<red>%s@}" (Runtime.error_to_string err)
|
|
|
|
|
2022-10-12 14:45:04 +03:00
|
|
|
let var_debug fmt v =
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "%s_%d" (Bindlib.name_of v) (Bindlib.uid_of v)
|
|
|
|
|
2022-10-12 14:45:04 +03:00
|
|
|
let var fmt v = Format.pp_print_string fmt (Bindlib.name_of v)
|
|
|
|
|
2023-04-11 16:45:04 +03:00
|
|
|
(* Define precedence levels for auto parentheses *)
|
|
|
|
module Precedence = struct
|
|
|
|
type op = Xor | And | Or | Comp | Mul | Div | Add | Sub
|
|
|
|
|
|
|
|
type t =
|
|
|
|
| Contained
|
|
|
|
(* No parens needed, the term has unambiguous beginning and end *)
|
|
|
|
| Op of op
|
|
|
|
| App (* Function application, right-associative *)
|
|
|
|
| Abs (* lambda, *)
|
|
|
|
| Dot (* *)
|
|
|
|
|
|
|
|
let expr : type a. (a, 't) gexpr -> t =
|
|
|
|
fun e ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e with
|
2023-04-11 16:45:04 +03:00
|
|
|
| ELit _ -> Contained (* Todo: unop if < 0 *)
|
2023-12-18 12:25:00 +03:00
|
|
|
| EAppOp { op; _ } -> (
|
2024-04-30 17:35:08 +03:00
|
|
|
match Mark.remove op with
|
2023-04-11 16:45:04 +03:00
|
|
|
| Not | GetDay | GetMonth | GetYear | FirstDayOfMonth | LastDayOfMonth
|
|
|
|
| Length | Log _ | Minus | Minus_int | Minus_rat | Minus_mon | Minus_dur
|
|
|
|
| ToRat | ToRat_int | ToRat_mon | ToMoney | ToMoney_rat | Round
|
|
|
|
| Round_rat | Round_mon ->
|
|
|
|
App
|
|
|
|
| And -> Op And
|
|
|
|
| Or -> Op Or
|
|
|
|
| Xor -> Op Xor
|
|
|
|
| Eq | Eq_int_int | Eq_rat_rat | Eq_mon_mon | Eq_dur_dur | Eq_dat_dat ->
|
|
|
|
Op Comp
|
|
|
|
| Lt | Lt_int_int | Lt_rat_rat | Lt_mon_mon | Lt_dat_dat | Lt_dur_dur ->
|
|
|
|
Op Comp
|
|
|
|
| Lte | Lte_int_int | Lte_rat_rat | Lte_mon_mon | Lte_dat_dat
|
|
|
|
| Lte_dur_dur ->
|
|
|
|
Op Comp
|
|
|
|
| Gt | Gt_int_int | Gt_rat_rat | Gt_mon_mon | Gt_dat_dat | Gt_dur_dur ->
|
|
|
|
Op Comp
|
|
|
|
| Gte | Gte_int_int | Gte_rat_rat | Gte_mon_mon | Gte_dat_dat
|
|
|
|
| Gte_dur_dur ->
|
|
|
|
Op Comp
|
|
|
|
| Add | Add_int_int | Add_rat_rat | Add_mon_mon | Add_dat_dur _
|
|
|
|
| Add_dur_dur ->
|
|
|
|
Op Add
|
|
|
|
| Sub | Sub_int_int | Sub_rat_rat | Sub_mon_mon | Sub_dat_dat
|
|
|
|
| Sub_dat_dur | Sub_dur_dur ->
|
|
|
|
Op Sub
|
|
|
|
| Mult | Mult_int_int | Mult_rat_rat | Mult_mon_rat | Mult_dur_int ->
|
|
|
|
Op Mul
|
|
|
|
| Div | Div_int_int | Div_rat_rat | Div_mon_rat | Div_mon_mon
|
|
|
|
| Div_dur_dur ->
|
|
|
|
Op Div
|
2024-06-24 15:22:58 +03:00
|
|
|
| HandleDefaultOpt | Map | Map2 | Concat | Filter | Reduce | Fold
|
|
|
|
| ToClosureEnv | FromClosureEnv ->
|
2023-04-14 12:19:58 +03:00
|
|
|
App)
|
2023-04-11 16:45:04 +03:00
|
|
|
| EApp _ -> App
|
|
|
|
| EArray _ -> Contained
|
|
|
|
| EVar _ -> Contained
|
2023-04-19 19:26:50 +03:00
|
|
|
| EExternal _ -> Contained
|
2023-04-11 16:45:04 +03:00
|
|
|
| EAbs _ -> Abs
|
|
|
|
| EIfThenElse _ -> Contained
|
|
|
|
| EStruct _ -> Contained
|
|
|
|
| EInj _ -> App
|
|
|
|
| EMatch _ -> App
|
|
|
|
| ETuple _ -> Contained
|
|
|
|
| ETupleAccess _ -> Dot
|
|
|
|
| ELocation _ -> Contained
|
|
|
|
| EScopeCall _ -> App
|
2024-04-11 19:30:51 +03:00
|
|
|
| EDStructAmend _ -> App
|
2023-04-11 16:45:04 +03:00
|
|
|
| EDStructAccess _ | EStructAccess _ -> Dot
|
|
|
|
| EAssert _ -> App
|
2024-04-26 19:31:26 +03:00
|
|
|
| EFatalError _ -> App
|
2023-04-11 16:45:04 +03:00
|
|
|
| EDefault _ -> Contained
|
2023-11-08 20:25:50 +03:00
|
|
|
| EPureDefault _ -> Contained
|
2024-04-26 19:31:26 +03:00
|
|
|
| EEmpty -> Contained
|
2023-04-11 16:45:04 +03:00
|
|
|
| EErrorOnEmpty _ -> App
|
2024-04-26 19:31:26 +03:00
|
|
|
| ERaiseEmpty -> App
|
|
|
|
| ECatchEmpty _ -> App
|
2023-04-19 19:26:50 +03:00
|
|
|
| ECustom _ -> Contained
|
2023-04-11 16:45:04 +03:00
|
|
|
|
|
|
|
let needs_parens ~context ?(rhs = false) e =
|
|
|
|
match expr context, expr e with
|
|
|
|
| _, Contained -> false
|
2023-04-17 16:10:47 +03:00
|
|
|
| Dot, Dot -> rhs
|
2023-04-11 16:45:04 +03:00
|
|
|
| _, Dot -> false
|
|
|
|
| Dot, _ -> true
|
|
|
|
| App, App -> not rhs
|
|
|
|
| App, Op _ -> true
|
|
|
|
| App, Abs -> true
|
|
|
|
| Abs, _ -> false
|
|
|
|
| Op a, Op b -> (
|
|
|
|
match a, b with
|
|
|
|
| _, Xor -> true
|
|
|
|
| And, And | Or, Or -> false
|
|
|
|
| And, Or | Or, And -> true
|
|
|
|
| (And | Or | Xor), _ -> false
|
|
|
|
| _, (And | Or | Comp) -> true
|
|
|
|
| Comp, _ -> false
|
|
|
|
| Add, (Add | Sub) -> false
|
|
|
|
| Sub, (Add | Sub) -> rhs
|
|
|
|
| (Add | Sub), (Mul | Div) -> false
|
|
|
|
| (Mul | Div), (Add | Sub) -> true
|
|
|
|
| Mul, (Mul | Div) -> false
|
|
|
|
| Div, (Mul | Div) -> rhs)
|
2023-04-17 16:10:47 +03:00
|
|
|
| Op _, App -> not rhs
|
2023-04-11 16:45:04 +03:00
|
|
|
| Op _, _ -> true
|
|
|
|
| Contained, _ -> false
|
|
|
|
end
|
2022-08-25 13:09:51 +03:00
|
|
|
|
2023-07-11 18:10:00 +03:00
|
|
|
module type EXPR_PARAM = sig
|
|
|
|
val bypass : Format.formatter -> ('a, 't) gexpr -> bool
|
|
|
|
val operator : Format.formatter -> 'a operator -> unit
|
|
|
|
val var : Format.formatter -> ('a, 't) gexpr Var.t -> unit
|
|
|
|
val lit : Format.formatter -> lit -> unit
|
|
|
|
val pre_map : ('a, 't) gexpr -> ('a, 't) gexpr
|
|
|
|
end
|
|
|
|
|
|
|
|
module ExprGen (C : EXPR_PARAM) = struct
|
|
|
|
let rec expr_aux :
|
2024-02-06 17:33:40 +03:00
|
|
|
type a t.
|
2023-07-11 18:10:00 +03:00
|
|
|
Bindlib.ctxt ->
|
|
|
|
Ocolor_types.color4 list ->
|
|
|
|
Format.formatter ->
|
2024-02-06 17:33:40 +03:00
|
|
|
(a, t) gexpr ->
|
2023-07-11 18:10:00 +03:00
|
|
|
unit =
|
|
|
|
fun bnd_ctx colors fmt e ->
|
2024-02-06 17:33:40 +03:00
|
|
|
(* (* Uncomment for type annotations everywhere *)
|
|
|
|
* (fun f ->
|
|
|
|
* Format.fprintf fmt "@[<hv 1>(%a:@ %a)@]"
|
|
|
|
* f e
|
|
|
|
* typ_debug
|
|
|
|
* (match Mark.get e with Typed {ty; _} -> ty | _ -> TAny,Pos.no_pos))
|
|
|
|
* @@ fun fmt e -> *)
|
2023-07-11 18:10:00 +03:00
|
|
|
let exprb bnd_ctx colors e = expr_aux bnd_ctx colors e in
|
|
|
|
let exprc colors e = exprb bnd_ctx colors e in
|
|
|
|
let expr e = exprc colors e in
|
|
|
|
let var = C.var in
|
|
|
|
let operator = C.operator in
|
|
|
|
let e = C.pre_map e in
|
|
|
|
let paren ~rhs ?(colors = colors) expr fmt e1 =
|
|
|
|
if Precedence.needs_parens ~rhs ~context:e (C.pre_map e1) then (
|
|
|
|
Format.pp_open_hvbox fmt 1;
|
|
|
|
pp_color_string (List.hd colors) fmt "(";
|
|
|
|
expr (List.tl colors) fmt e1;
|
|
|
|
Format.pp_close_box fmt ();
|
|
|
|
pp_color_string (List.hd colors) fmt ")")
|
|
|
|
else expr colors fmt e1
|
|
|
|
in
|
|
|
|
let default_punct = with_color (fun fmt -> Format.pp_print_as fmt 1) in
|
|
|
|
let lhs ?(colors = colors) ex = paren ~colors ~rhs:false ex in
|
|
|
|
let rhs ex = paren ~rhs:true ex in
|
|
|
|
if C.bypass fmt e then ()
|
|
|
|
else
|
|
|
|
match Mark.remove e with
|
|
|
|
| EVar v -> var fmt v
|
2023-08-30 18:49:29 +03:00
|
|
|
| EExternal { name } -> external_ref fmt name
|
2023-07-11 18:10:00 +03:00
|
|
|
| ETuple es ->
|
|
|
|
Format.fprintf fmt "@[<hov 2>%a%a%a@]"
|
|
|
|
(pp_color_string (List.hd colors))
|
|
|
|
"("
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () ->
|
|
|
|
Format.fprintf fmt "%a@ " (pp_color_string (List.hd colors)) ",")
|
|
|
|
(fun fmt e -> lhs ~colors:(List.tl colors) exprc fmt e))
|
|
|
|
es
|
|
|
|
(pp_color_string (List.hd colors))
|
|
|
|
")"
|
|
|
|
| EArray es ->
|
2023-11-28 15:37:08 +03:00
|
|
|
Format.fprintf fmt "@[<hv 2>%a@,@[<hov>%a@]@;<0 -2>%a@]" punctuation "["
|
2023-07-11 18:10:00 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ";@ ")
|
|
|
|
(fun fmt e -> lhs exprc fmt e))
|
|
|
|
es punctuation "]"
|
|
|
|
| ETupleAccess { e; index; _ } ->
|
|
|
|
lhs exprc fmt e;
|
|
|
|
punctuation fmt ".";
|
|
|
|
Format.pp_print_int fmt index
|
|
|
|
| ELit l -> C.lit fmt l
|
|
|
|
| EApp { f = EAbs _, _; _ } ->
|
|
|
|
let rec pr bnd_ctx colors fmt = function
|
2023-12-18 12:25:00 +03:00
|
|
|
| EApp { f = EAbs { binder; tys }, _; args; _ }, _ ->
|
2023-07-11 18:10:00 +03:00
|
|
|
let xs, body, bnd_ctx = Bindlib.unmbind_in bnd_ctx binder in
|
|
|
|
let xs_tau = List.mapi (fun i tau -> xs.(i), tau) tys in
|
|
|
|
let xs_tau_arg =
|
|
|
|
List.map2 (fun (x, tau) arg -> x, tau, arg) xs_tau args
|
|
|
|
in
|
|
|
|
Format.pp_print_list
|
|
|
|
(fun fmt (x, tau, arg) ->
|
|
|
|
Format.fprintf fmt
|
|
|
|
"@[<hv 2>@[<hov 4>%a %a %a@ %a@ %a@]@ %a@;<1 -2>%a@]" keyword
|
|
|
|
"let" var x punctuation ":" (typ_gen None ~colors) tau
|
|
|
|
punctuation "=" (exprc colors) arg keyword "in")
|
|
|
|
fmt xs_tau_arg;
|
|
|
|
Format.pp_print_cut fmt ();
|
|
|
|
rhs (pr bnd_ctx) fmt body
|
|
|
|
| e -> rhs (exprb bnd_ctx) fmt e
|
|
|
|
in
|
|
|
|
Format.pp_open_vbox fmt 0;
|
|
|
|
pr bnd_ctx colors fmt e;
|
|
|
|
Format.pp_close_box fmt ()
|
|
|
|
| EAbs { binder; tys } ->
|
2023-04-18 11:09:58 +03:00
|
|
|
let xs, body, bnd_ctx = Bindlib.unmbind_in bnd_ctx binder in
|
2023-07-11 18:10:00 +03:00
|
|
|
let expr = exprb bnd_ctx in
|
2023-04-18 11:09:58 +03:00
|
|
|
let xs_tau = List.mapi (fun i tau -> xs.(i), tau) tys in
|
2023-07-11 18:10:00 +03:00
|
|
|
Format.fprintf fmt "@[<hv 0>%a @[<hv 2>%a@]@ @]%a@ %a" punctuation "λ"
|
|
|
|
(Format.pp_print_list ~pp_sep:Format.pp_print_space
|
|
|
|
(fun fmt (x, tau) ->
|
2024-02-09 18:48:02 +03:00
|
|
|
match tau with
|
|
|
|
| TLit TUnit, _ ->
|
|
|
|
punctuation fmt "(";
|
|
|
|
punctuation fmt ")"
|
|
|
|
| _ ->
|
|
|
|
punctuation fmt "(";
|
|
|
|
Format.pp_open_hvbox fmt 2;
|
|
|
|
var fmt x;
|
|
|
|
punctuation fmt ":";
|
|
|
|
Format.pp_print_space fmt ();
|
|
|
|
typ_gen None ~colors fmt tau;
|
|
|
|
Format.pp_close_box fmt ();
|
|
|
|
punctuation fmt ")"))
|
2023-07-11 18:10:00 +03:00
|
|
|
xs_tau punctuation "→" (rhs expr) body
|
2024-04-30 17:35:08 +03:00
|
|
|
| EAppOp { op = ((Map | Filter) as op), _; args = [arg1; arg2]; _ } ->
|
2023-07-11 18:10:00 +03:00
|
|
|
Format.fprintf fmt "@[<hv 2>%a %a@ %a@]" operator op (lhs exprc) arg1
|
|
|
|
(rhs exprc) arg2
|
2024-04-30 17:35:08 +03:00
|
|
|
| EAppOp { op = (Log _ as op), _; args = [arg1]; _ } ->
|
2023-07-11 18:10:00 +03:00
|
|
|
Format.fprintf fmt "@[<hv 0>%a@ %a@]" operator op (rhs exprc) arg1
|
2024-04-30 17:35:08 +03:00
|
|
|
| EAppOp { op = op0, _; args = [_; _]; _ } ->
|
2023-07-11 18:10:00 +03:00
|
|
|
let prec = Precedence.expr e in
|
|
|
|
let rec pr colors fmt = function
|
|
|
|
(* Flatten sequences of the same associative op *)
|
2024-04-30 17:35:08 +03:00
|
|
|
| EAppOp { op = op, _; args = [arg1; arg2]; _ }, _ when op = op0 -> (
|
2023-07-11 18:10:00 +03:00
|
|
|
(match prec with
|
|
|
|
| Op (And | Or | Mul | Add | Div | Sub) -> lhs pr fmt arg1
|
|
|
|
| _ -> lhs exprc fmt arg1);
|
|
|
|
Format.pp_print_space fmt ();
|
|
|
|
operator fmt op;
|
|
|
|
Format.pp_print_char fmt ' ';
|
|
|
|
match prec with
|
|
|
|
| Op (And | Or | Mul | Add) -> rhs pr fmt arg2
|
|
|
|
| _ -> rhs exprc fmt arg2)
|
|
|
|
| e -> exprc colors fmt e
|
2023-04-18 11:09:58 +03:00
|
|
|
in
|
2023-07-11 18:10:00 +03:00
|
|
|
Format.pp_open_hvbox fmt 0;
|
|
|
|
pr colors fmt e;
|
|
|
|
Format.pp_close_box fmt ()
|
2024-04-30 17:35:08 +03:00
|
|
|
| EAppOp { op = op, _; args = [arg1]; _ } ->
|
2023-07-11 18:10:00 +03:00
|
|
|
Format.fprintf fmt "@[<hv 2>%a@ %a@]" operator op (rhs exprc) arg1
|
2024-04-30 17:35:08 +03:00
|
|
|
| EAppOp { op = op, _; args; _ } ->
|
2023-12-18 12:25:00 +03:00
|
|
|
Format.fprintf fmt "@[<hv 2>%a@ %a@]" operator op
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@ ")
|
|
|
|
(rhs exprc))
|
|
|
|
args
|
|
|
|
| EApp { f; args; _ } ->
|
2023-07-11 18:10:00 +03:00
|
|
|
Format.fprintf fmt "@[<hv 2>%a@ %a@]" (lhs exprc) f
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@ ")
|
|
|
|
(rhs exprc))
|
|
|
|
args
|
|
|
|
| EIfThenElse _ ->
|
|
|
|
let rec pr els fmt = function
|
|
|
|
| EIfThenElse { cond; etrue; efalse }, _ ->
|
|
|
|
Format.fprintf fmt "@[<hv 2>@[<hv 2>%a@ %a@;<1 -2>%a@]@ %a@]@ %a"
|
|
|
|
keyword
|
|
|
|
(if els then "else if" else "if")
|
|
|
|
expr cond keyword "then" expr etrue (pr true) efalse
|
|
|
|
| e ->
|
|
|
|
Format.fprintf fmt "@[<hv 2>%a@ %a@]" keyword "else" (rhs exprc) e
|
|
|
|
in
|
|
|
|
Format.pp_open_hvbox fmt 0;
|
|
|
|
pr false fmt e;
|
|
|
|
Format.pp_close_box fmt ()
|
|
|
|
| EDefault { excepts; just; cons } ->
|
|
|
|
if List.length excepts = 0 then
|
|
|
|
Format.fprintf fmt "@[<hv 1>%a%a@ %a %a%a@]"
|
|
|
|
(default_punct (List.hd colors))
|
|
|
|
"⟨"
|
|
|
|
(exprc (List.tl colors))
|
|
|
|
just
|
|
|
|
(default_punct (List.hd colors))
|
|
|
|
"⊢"
|
|
|
|
(exprc (List.tl colors))
|
|
|
|
cons
|
|
|
|
(default_punct (List.hd colors))
|
|
|
|
"⟩"
|
|
|
|
else
|
|
|
|
Format.fprintf fmt
|
|
|
|
"@[<hv 0>@[<hov 2>%a %a@]@ @[<hov 2>%a %a@ %a %a@] %a@]"
|
|
|
|
(default_punct (List.hd colors))
|
|
|
|
"⟨"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () ->
|
|
|
|
Format.fprintf fmt "%a@ " (default_punct (List.hd colors)) ",")
|
|
|
|
(lhs ~colors:(List.tl colors) exprc))
|
|
|
|
excepts
|
|
|
|
(default_punct (List.hd colors))
|
|
|
|
"|"
|
|
|
|
(exprc (List.tl colors))
|
|
|
|
just
|
|
|
|
(default_punct (List.hd colors))
|
|
|
|
"⊢"
|
|
|
|
(exprc (List.tl colors))
|
|
|
|
cons
|
|
|
|
(default_punct (List.hd colors))
|
|
|
|
"⟩"
|
2023-11-08 20:25:50 +03:00
|
|
|
| EPureDefault e ->
|
|
|
|
Format.fprintf fmt "%a%a%a"
|
|
|
|
(default_punct (List.hd colors))
|
|
|
|
"⟨" expr e
|
|
|
|
(default_punct (List.hd colors))
|
|
|
|
"⟩"
|
2024-04-26 19:31:26 +03:00
|
|
|
| EEmpty -> lit_style fmt "∅"
|
2023-07-11 18:10:00 +03:00
|
|
|
| EErrorOnEmpty e' ->
|
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ %a@]" op_style "error_empty"
|
|
|
|
(rhs exprc) e'
|
|
|
|
| EAssert e' ->
|
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ %a%a%a@]" keyword "assert" punctuation
|
|
|
|
"(" (rhs exprc) e' punctuation ")"
|
2024-04-26 19:31:26 +03:00
|
|
|
| EFatalError err ->
|
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ @{<red>%s@}@]" keyword "error"
|
|
|
|
(Runtime.error_to_string err)
|
|
|
|
| ECatchEmpty { body; handler } ->
|
2023-07-11 18:10:00 +03:00
|
|
|
Format.fprintf fmt
|
|
|
|
"@[<hv 0>@[<hov 2>%a@ %a@]@ @[<hov 2>%a@ %a ->@ %a@]@]" keyword "try"
|
2024-04-29 14:42:40 +03:00
|
|
|
expr body keyword "with" op_style "Empty" (rhs exprc) handler
|
2024-04-26 19:31:26 +03:00
|
|
|
| ERaiseEmpty ->
|
2024-04-29 14:42:40 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ %a@]" keyword "raise" op_style "Empty"
|
2023-07-11 18:10:00 +03:00
|
|
|
| ELocation loc -> location fmt loc
|
|
|
|
| EDStructAccess { e; field; _ } ->
|
|
|
|
Format.fprintf fmt "@[<hv 2>%a%a@,%a%a%a@]" (lhs exprc) e punctuation
|
2023-07-12 12:48:46 +03:00
|
|
|
"." punctuation "\"" Ident.format field punctuation "\""
|
2024-04-11 19:30:51 +03:00
|
|
|
| EDStructAmend { e; fields; _ } ->
|
|
|
|
Format.fprintf fmt "@[<hv 2>@[<hov>%a %a@ with@]@ %a@;<1 -2>%a@]"
|
|
|
|
punctuation "{" (lhs exprc) e
|
|
|
|
(Ident.Map.format_bindings ~pp_sep:Format.pp_print_space
|
|
|
|
(fun fmt pp_field_name field_expr ->
|
|
|
|
Format.fprintf fmt "@[<hv 2>%t %a@ %a%a@]" pp_field_name
|
|
|
|
punctuation "=" (lhs exprc) field_expr punctuation ";"))
|
|
|
|
fields punctuation "}"
|
2023-07-11 18:10:00 +03:00
|
|
|
| EStruct { name; fields } ->
|
|
|
|
if StructField.Map.is_empty fields then (
|
|
|
|
punctuation fmt "{";
|
2023-07-12 12:48:46 +03:00
|
|
|
StructName.format fmt name;
|
2023-07-11 18:10:00 +03:00
|
|
|
punctuation fmt "}")
|
|
|
|
else
|
|
|
|
Format.fprintf fmt "@[<hv 2>%a %a@ %a@;<1 -2>%a@]" punctuation "{"
|
2023-07-12 12:48:46 +03:00
|
|
|
StructName.format name
|
|
|
|
(StructField.Map.format_bindings ~pp_sep:Format.pp_print_space
|
|
|
|
(fun fmt pp_field_name field_expr ->
|
|
|
|
Format.fprintf fmt "@[<hv 2>%t %a@ %a%a@]" pp_field_name
|
|
|
|
punctuation "=" (lhs exprc) field_expr punctuation ";"))
|
|
|
|
fields punctuation "}"
|
2023-07-11 18:10:00 +03:00
|
|
|
| EStructAccess { e; field; _ } ->
|
|
|
|
Format.fprintf fmt "@[<hv 2>%a%a@,%a@]" (lhs exprc) e punctuation "."
|
2023-09-01 11:43:46 +03:00
|
|
|
StructField.format field
|
2023-07-11 18:10:00 +03:00
|
|
|
| EInj { e; cons; _ } ->
|
2023-07-12 12:48:46 +03:00
|
|
|
Format.fprintf fmt "@[<hv 2>%a@ %a@]" EnumConstructor.format cons
|
2023-12-19 16:44:35 +03:00
|
|
|
(paren ~rhs:false exprc) e
|
2023-07-11 18:10:00 +03:00
|
|
|
| EMatch { e; cases; _ } ->
|
2023-11-28 15:37:08 +03:00
|
|
|
Format.fprintf fmt "@[<v 0>@[<hv 2>%a@ %a@;<1 -2>%a@]@ %a@]" keyword
|
|
|
|
"match" (lhs exprc) e keyword "with"
|
2023-07-12 12:48:46 +03:00
|
|
|
(EnumConstructor.Map.format_bindings
|
|
|
|
(fun fmt pp_cons_name case_expr ->
|
2023-07-11 18:10:00 +03:00
|
|
|
match case_expr with
|
2024-02-07 19:54:12 +03:00
|
|
|
| EAbs { binder; tys; _ }, _ ->
|
2023-07-11 18:10:00 +03:00
|
|
|
let xs, body, bnd_ctx = Bindlib.unmbind_in bnd_ctx binder in
|
|
|
|
let expr = exprb bnd_ctx in
|
2024-02-09 18:48:02 +03:00
|
|
|
let pp_args fmt =
|
|
|
|
match tys with
|
|
|
|
| [(TLit TUnit, _)] -> ()
|
2024-02-07 19:54:12 +03:00
|
|
|
| _ ->
|
|
|
|
Format.pp_print_seq ~pp_sep:Format.pp_print_space var fmt
|
|
|
|
(Array.to_seq xs);
|
|
|
|
Format.pp_print_space fmt ()
|
|
|
|
in
|
2024-02-09 18:48:02 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a %t@ %t%a@ %a@]" punctuation "|"
|
|
|
|
pp_cons_name pp_args punctuation "→" (rhs expr) body
|
2023-07-11 18:10:00 +03:00
|
|
|
| e ->
|
2023-07-12 12:48:46 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a %t@ %a@ %a@]" punctuation "|"
|
|
|
|
pp_cons_name punctuation "→" (rhs exprc) e))
|
|
|
|
cases
|
2023-08-30 18:49:29 +03:00
|
|
|
| EScopeCall { scope; args } ->
|
2023-07-11 18:10:00 +03:00
|
|
|
Format.pp_open_hovbox fmt 2;
|
2023-07-12 12:48:46 +03:00
|
|
|
ScopeName.format fmt scope;
|
2023-04-17 16:10:47 +03:00
|
|
|
Format.pp_print_space fmt ();
|
2023-07-11 18:10:00 +03:00
|
|
|
keyword fmt "of";
|
|
|
|
Format.pp_print_space fmt ();
|
|
|
|
Format.pp_open_hvbox fmt 2;
|
|
|
|
punctuation fmt "{";
|
2023-07-12 12:48:46 +03:00
|
|
|
ScopeVar.Map.format_bindings
|
2023-07-11 18:10:00 +03:00
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "%a@ " punctuation ";")
|
2023-07-12 12:48:46 +03:00
|
|
|
(fun fmt pp_field_name field_expr ->
|
|
|
|
Format.fprintf fmt "%a%t%a%a@ %a" punctuation "\"" pp_field_name
|
|
|
|
punctuation "\"" punctuation "=" (rhs exprc) field_expr)
|
|
|
|
fmt args;
|
2023-07-11 18:10:00 +03:00
|
|
|
Format.pp_close_box fmt ();
|
|
|
|
punctuation fmt "}";
|
|
|
|
Format.pp_close_box fmt ()
|
|
|
|
| ECustom _ -> Format.pp_print_string fmt "<obj>"
|
2022-09-30 17:37:43 +03:00
|
|
|
|
2023-07-11 18:10:00 +03:00
|
|
|
let expr ppf e = expr_aux Bindlib.empty_ctxt colors ppf e
|
|
|
|
end
|
|
|
|
|
|
|
|
module ExprConciseParam = struct
|
|
|
|
let bypass _ _ = false
|
|
|
|
let operator o = operator ~debug:false o
|
|
|
|
let var = var
|
|
|
|
let lit = lit
|
2023-04-24 15:35:34 +03:00
|
|
|
|
2023-07-11 18:10:00 +03:00
|
|
|
let rec pre_map : type a. (a, 't) gexpr -> (a, 't) gexpr = function
|
2024-04-30 17:35:08 +03:00
|
|
|
| EAppOp { op = Log _, _; args = [e]; _ }, _ -> pre_map e
|
2023-07-11 18:10:00 +03:00
|
|
|
| e -> e
|
|
|
|
end
|
|
|
|
|
|
|
|
module ExprConcise = ExprGen (ExprConciseParam)
|
|
|
|
|
|
|
|
module ExprDebugParam = struct
|
|
|
|
let bypass _ _ = false
|
|
|
|
let operator o = operator ~debug:true o
|
|
|
|
let var = var_debug
|
|
|
|
let lit = lit
|
|
|
|
let pre_map e = e
|
|
|
|
end
|
2023-04-27 16:07:04 +03:00
|
|
|
|
2023-07-11 18:10:00 +03:00
|
|
|
module ExprDebug = ExprGen (ExprDebugParam)
|
|
|
|
|
2024-03-15 16:23:30 +03:00
|
|
|
let expr ?(debug = Global.options.debug) () ppf e =
|
2023-07-11 18:10:00 +03:00
|
|
|
if debug then ExprDebug.expr ppf e else ExprConcise.expr ppf e
|
2023-04-07 12:26:10 +03:00
|
|
|
|
2023-04-21 15:51:15 +03:00
|
|
|
let scope_let_kind ?debug:(_debug = true) _ctx fmt k =
|
2023-04-07 12:26:10 +03:00
|
|
|
match k with
|
|
|
|
| DestructuringInputStruct -> keyword fmt "get"
|
|
|
|
| ScopeVarDefinition -> keyword fmt "set"
|
|
|
|
| SubScopeVarDefinition -> keyword fmt "sub_set"
|
|
|
|
| CallingSubScope -> keyword fmt "call"
|
|
|
|
| DestructuringSubScopeResults -> keyword fmt "sub_get"
|
|
|
|
| Assertion -> keyword fmt "assert"
|
|
|
|
|
2024-02-09 18:48:02 +03:00
|
|
|
let[@ocamlformat "disable"]
|
2023-05-02 17:33:23 +03:00
|
|
|
scope_body_expr ?(debug = false) ctx fmt b : unit =
|
2024-02-09 18:48:02 +03:00
|
|
|
let print_scope_let x sl =
|
2023-05-02 17:33:23 +03:00
|
|
|
Format.fprintf fmt
|
2024-02-09 18:48:02 +03:00
|
|
|
"@[<hv 2>@[<hov 4>%a %a %a %a@ %a@ %a@]@ %a@;<1 -2>%a@]@,"
|
2023-05-02 17:33:23 +03:00
|
|
|
keyword "let"
|
2024-02-09 18:48:02 +03:00
|
|
|
(scope_let_kind ~debug ctx) sl.scope_let_kind
|
2023-05-02 17:33:23 +03:00
|
|
|
(if debug then var_debug else var) x
|
|
|
|
punctuation ":"
|
2024-02-09 18:48:02 +03:00
|
|
|
(typ ctx) sl.scope_let_typ
|
2023-05-02 17:33:23 +03:00
|
|
|
punctuation "="
|
2024-02-09 18:48:02 +03:00
|
|
|
(expr ~debug ()) sl.scope_let_expr
|
2023-05-02 17:33:23 +03:00
|
|
|
keyword "in"
|
2024-02-09 18:48:02 +03:00
|
|
|
in
|
|
|
|
let last = BoundList.iter ~f:print_scope_let b in
|
|
|
|
Format.fprintf fmt "%a %a" keyword "return" (expr ~debug ()) last
|
2023-04-07 12:26:10 +03:00
|
|
|
|
|
|
|
let scope_body ?(debug = false) ctx fmt (n, l) : unit =
|
|
|
|
let {
|
|
|
|
scope_body_input_struct;
|
|
|
|
scope_body_output_struct;
|
|
|
|
scope_body_expr = body;
|
|
|
|
} =
|
|
|
|
l
|
|
|
|
in
|
|
|
|
|
|
|
|
let input_typ = TStruct scope_body_input_struct, Pos.no_pos in
|
|
|
|
let output_typ = TStruct scope_body_output_struct, Pos.no_pos in
|
|
|
|
|
|
|
|
let x, body = Bindlib.unbind body in
|
|
|
|
|
2023-05-02 17:33:23 +03:00
|
|
|
let () =
|
2023-04-07 12:57:14 +03:00
|
|
|
Format.pp_open_vbox fmt 2;
|
2023-05-02 17:33:23 +03:00
|
|
|
let () =
|
|
|
|
Format.pp_open_hvbox fmt 2;
|
|
|
|
let () =
|
|
|
|
Format.pp_open_hovbox fmt 4;
|
|
|
|
keyword fmt "let scope";
|
|
|
|
Format.pp_print_space fmt ();
|
2023-07-12 12:48:46 +03:00
|
|
|
ScopeName.format fmt n;
|
2023-05-02 17:33:23 +03:00
|
|
|
Format.pp_close_box fmt ()
|
|
|
|
in
|
2023-04-07 12:57:14 +03:00
|
|
|
Format.pp_print_space fmt ();
|
|
|
|
punctuation fmt "(";
|
2023-05-02 17:33:23 +03:00
|
|
|
let () =
|
|
|
|
Format.pp_open_hvbox fmt 2;
|
|
|
|
(if debug then var_debug else var) fmt x;
|
|
|
|
punctuation fmt ":";
|
|
|
|
Format.pp_print_space fmt ();
|
|
|
|
(if debug then typ_debug else typ ctx) fmt input_typ;
|
|
|
|
punctuation fmt ")";
|
|
|
|
Format.pp_close_box fmt ()
|
|
|
|
in
|
|
|
|
Format.pp_print_cut fmt ();
|
2023-04-07 12:57:14 +03:00
|
|
|
punctuation fmt ":";
|
2023-05-02 17:33:23 +03:00
|
|
|
Format.pp_print_string fmt " ";
|
|
|
|
let () =
|
|
|
|
Format.pp_open_hvbox fmt 2;
|
|
|
|
(if debug then typ_debug else typ ctx) fmt output_typ;
|
|
|
|
Format.pp_close_box fmt ()
|
|
|
|
in
|
2023-04-07 12:57:14 +03:00
|
|
|
Format.pp_print_space fmt ();
|
|
|
|
punctuation fmt "=";
|
2023-04-07 12:26:10 +03:00
|
|
|
Format.pp_close_box fmt ()
|
|
|
|
in
|
2023-04-07 12:57:14 +03:00
|
|
|
|
|
|
|
Format.pp_print_cut fmt ();
|
|
|
|
|
|
|
|
scope_body_expr ~debug ctx fmt body;
|
2023-04-07 12:26:10 +03:00
|
|
|
Format.pp_close_box fmt ()
|
|
|
|
in
|
2023-04-07 12:57:14 +03:00
|
|
|
()
|
2023-04-07 12:26:10 +03:00
|
|
|
|
|
|
|
let enum
|
|
|
|
?(debug = false)
|
|
|
|
decl_ctx
|
|
|
|
fmt
|
2023-07-12 12:48:46 +03:00
|
|
|
(pp_name : Format.formatter -> unit)
|
2023-08-30 18:49:29 +03:00
|
|
|
(c : typ EnumConstructor.Map.t) =
|
|
|
|
Format.fprintf fmt "@[<h 0>%a %t %a@ %a@]" keyword "type" pp_name punctuation
|
2023-08-10 17:52:39 +03:00
|
|
|
"="
|
2023-07-12 12:48:46 +03:00
|
|
|
(EnumConstructor.Map.format_bindings
|
|
|
|
~pp_sep:(fun _ _ -> ())
|
|
|
|
(fun fmt pp_n ty ->
|
2024-06-24 16:40:41 +03:00
|
|
|
Format.fprintf fmt "@[<hov2> %a %t %a %a@]@," punctuation "|" pp_n
|
2023-07-12 12:48:46 +03:00
|
|
|
keyword "of"
|
|
|
|
(if debug then typ_debug else typ decl_ctx)
|
|
|
|
ty))
|
|
|
|
c
|
2023-04-07 12:26:10 +03:00
|
|
|
|
|
|
|
let struct_
|
|
|
|
?(debug = false)
|
|
|
|
decl_ctx
|
|
|
|
fmt
|
2023-07-12 12:48:46 +03:00
|
|
|
(pp_name : Format.formatter -> unit)
|
2023-08-30 18:49:29 +03:00
|
|
|
(c : typ StructField.Map.t) =
|
2024-01-17 16:02:32 +03:00
|
|
|
Format.fprintf fmt "@[<hv 2>%a %t %a %a@ %a@;<1 -2>%a@]" keyword "type"
|
2023-11-27 17:11:55 +03:00
|
|
|
pp_name punctuation "=" punctuation "{"
|
|
|
|
(StructField.Map.format_bindings ~pp_sep:Format.pp_print_space
|
2023-07-12 12:48:46 +03:00
|
|
|
(fun fmt pp_n ty ->
|
2023-11-27 17:11:55 +03:00
|
|
|
Format.fprintf fmt "@[<h 2>%t%a %a%a@]" pp_n keyword ":"
|
2023-07-12 12:48:46 +03:00
|
|
|
(if debug then typ_debug else typ decl_ctx)
|
|
|
|
ty punctuation ";"))
|
|
|
|
c punctuation "}"
|
2023-04-07 12:26:10 +03:00
|
|
|
|
|
|
|
let decl_ctx ?(debug = false) decl_ctx (fmt : Format.formatter) (ctx : decl_ctx)
|
|
|
|
: unit =
|
|
|
|
let { ctx_enums; ctx_structs; _ } = ctx in
|
2024-06-24 16:40:41 +03:00
|
|
|
Format.fprintf fmt "@[<v>%a@,%a@,@,@]"
|
|
|
|
(EnumName.Map.format_bindings (enum ~debug decl_ctx))
|
2023-07-12 12:48:46 +03:00
|
|
|
ctx_enums
|
2024-06-24 16:40:41 +03:00
|
|
|
(StructName.Map.format_bindings (struct_ ~debug decl_ctx))
|
2023-07-12 12:48:46 +03:00
|
|
|
ctx_structs
|
2023-04-07 12:26:10 +03:00
|
|
|
|
|
|
|
let scope
|
|
|
|
?(debug : bool = false)
|
|
|
|
(ctx : decl_ctx)
|
|
|
|
(fmt : Format.formatter)
|
|
|
|
((n, s) : ScopeName.t * 'm scope_body) : unit =
|
2023-05-02 17:33:23 +03:00
|
|
|
Format.pp_open_vbox fmt 0;
|
|
|
|
scope_body ~debug ctx fmt (n, s);
|
|
|
|
Format.pp_close_box fmt ()
|
2023-04-07 12:26:10 +03:00
|
|
|
|
2023-11-27 17:11:55 +03:00
|
|
|
let code_item ?(debug = false) ?name decl_ctx fmt c =
|
2023-04-07 12:26:10 +03:00
|
|
|
match c with
|
2023-11-27 17:11:55 +03:00
|
|
|
| ScopeDef (n, b) ->
|
|
|
|
let n =
|
|
|
|
match debug, name with
|
|
|
|
| true, Some n -> ScopeName.fresh [] (n, Pos.no_pos)
|
|
|
|
| _ -> n
|
|
|
|
in
|
|
|
|
scope ~debug decl_ctx fmt (n, b)
|
2023-04-07 12:26:10 +03:00
|
|
|
| Topdef (n, ty, e) ->
|
2023-11-27 17:11:55 +03:00
|
|
|
let n =
|
|
|
|
match debug, name with
|
|
|
|
| true, Some n -> TopdefName.fresh [] (n, Pos.no_pos)
|
|
|
|
| _ -> n
|
|
|
|
in
|
2023-06-18 19:08:18 +03:00
|
|
|
Format.fprintf fmt "@[<v 2>@[<hov 2>%a@ %a@ %a@ %a@ %a@]@ %a@]" keyword
|
2023-07-12 12:48:46 +03:00
|
|
|
"let topval" TopdefName.format n op_style ":" (typ decl_ctx) ty op_style
|
2023-06-18 19:08:18 +03:00
|
|
|
"=" (expr ~debug ()) e
|
2023-04-07 12:26:10 +03:00
|
|
|
|
2024-02-09 18:48:02 +03:00
|
|
|
let code_item_list ?(debug = false) decl_ctx fmt c =
|
|
|
|
BoundList.iter c ~f:(fun x item ->
|
|
|
|
code_item ~debug
|
|
|
|
~name:(Format.asprintf "%a" var_debug x)
|
|
|
|
decl_ctx fmt item;
|
|
|
|
Format.pp_print_newline fmt ())
|
2023-04-07 12:26:10 +03:00
|
|
|
|
|
|
|
let program ?(debug = false) fmt p =
|
|
|
|
decl_ctx ~debug p.decl_ctx fmt p.decl_ctx;
|
|
|
|
code_item_list ~debug p.decl_ctx fmt p.code_items
|
2023-07-03 17:14:22 +03:00
|
|
|
|
|
|
|
(* - User-facing value printer - *)
|
|
|
|
|
2023-12-18 12:25:00 +03:00
|
|
|
(* This function is re-exported from module [Expr], but defined here where it's
|
|
|
|
first needed *)
|
|
|
|
let rec skip_wrappers : type a. (a, 'm) gexpr -> (a, 'm) gexpr = function
|
2024-04-30 17:35:08 +03:00
|
|
|
| EAppOp { op = Log _, _; args = [e]; tys = _ }, _ -> skip_wrappers e
|
|
|
|
| EApp { f = EAppOp { op = Log _, _; args = [f]; _ }, _; args; tys }, m ->
|
2023-12-18 12:25:00 +03:00
|
|
|
skip_wrappers (EApp { f; args; tys }, m)
|
|
|
|
| EErrorOnEmpty e, _ -> skip_wrappers e
|
|
|
|
| EDefault { excepts = []; just = ELit (LBool true), _; cons = e }, _ ->
|
|
|
|
skip_wrappers e
|
|
|
|
| EPureDefault e, _ -> skip_wrappers e
|
|
|
|
| e -> e
|
|
|
|
|
2023-07-03 17:14:22 +03:00
|
|
|
module UserFacing = struct
|
|
|
|
(* Refs:
|
|
|
|
https://en.wikipedia.org/wiki/Wikipedia:Manual_of_Style/Dates_and_numbers#Grouping_of_digits
|
|
|
|
https://fr.wikipedia.org/wiki/Wikip%C3%A9dia:Conventions_concernant_les_nombres#Pour_un_comptage_ou_une_mesure *)
|
2024-03-15 16:23:30 +03:00
|
|
|
let bigsep (lang : Global.backend_lang) =
|
2023-07-03 17:14:22 +03:00
|
|
|
match lang with En -> ",", 3 | Fr -> " ", 3 | Pl -> ",", 3
|
|
|
|
|
2024-03-15 16:23:30 +03:00
|
|
|
let decsep (lang : Global.backend_lang) =
|
2023-07-03 17:14:22 +03:00
|
|
|
match lang with En -> "." | Fr -> "," | Pl -> "."
|
|
|
|
|
2024-03-19 17:23:06 +03:00
|
|
|
let unit (_lang : Global.backend_lang) ppf () =
|
|
|
|
Format.pp_print_string ppf "()"
|
2023-07-03 17:14:22 +03:00
|
|
|
|
2024-03-15 16:23:30 +03:00
|
|
|
let bool (lang : Global.backend_lang) ppf b =
|
2023-07-03 17:14:22 +03:00
|
|
|
let s =
|
|
|
|
match lang, b with
|
|
|
|
| En, true -> "true"
|
|
|
|
| En, false -> "false"
|
|
|
|
| Fr, true -> "vrai"
|
|
|
|
| Fr, false -> "faux"
|
|
|
|
| Pl, true -> "prawda"
|
|
|
|
| Pl, false -> "falsz"
|
|
|
|
in
|
|
|
|
Format.pp_print_string ppf s
|
|
|
|
|
2024-03-15 16:23:30 +03:00
|
|
|
let integer (lang : Global.backend_lang) ppf n =
|
2023-07-03 17:14:22 +03:00
|
|
|
let sep, nsep = bigsep lang in
|
|
|
|
let nsep = Z.pow (Z.of_int 10) nsep in
|
|
|
|
if Z.sign n < 0 then Format.pp_print_char ppf '-';
|
|
|
|
let rec aux n =
|
|
|
|
let a, b = Z.div_rem n nsep in
|
|
|
|
if Z.equal a Z.zero then Z.pp_print ppf b
|
|
|
|
else (
|
|
|
|
aux a;
|
|
|
|
Format.fprintf ppf "%s%03d" sep (Z.to_int b))
|
|
|
|
in
|
|
|
|
aux (Z.abs n)
|
|
|
|
|
2024-03-15 16:23:30 +03:00
|
|
|
let money (lang : Global.backend_lang) ppf n =
|
2024-01-16 13:40:10 +03:00
|
|
|
let num = Z.abs n in
|
|
|
|
let units, cents = Z.div_rem num (Z.of_int 100) in
|
|
|
|
if Z.sign n < 0 then Format.pp_print_char ppf '-';
|
2023-07-03 17:14:22 +03:00
|
|
|
(match lang with En -> Format.pp_print_string ppf "$" | Fr | Pl -> ());
|
|
|
|
integer lang ppf units;
|
|
|
|
Format.pp_print_string ppf (decsep lang);
|
|
|
|
Format.fprintf ppf "%02d" (Z.to_int (Z.abs cents));
|
|
|
|
match lang with
|
|
|
|
| En -> ()
|
|
|
|
| Fr -> Format.pp_print_string ppf " €"
|
|
|
|
| Pl -> Format.pp_print_string ppf " PLN"
|
|
|
|
|
2024-03-15 16:23:30 +03:00
|
|
|
let decimal (lang : Global.backend_lang) ppf r =
|
2023-07-03 17:14:22 +03:00
|
|
|
let den = Q.den r in
|
2023-12-19 15:39:24 +03:00
|
|
|
let num = Z.abs (Q.num r) in
|
|
|
|
let int_part, rem = Z.div_rem num den in
|
2023-07-03 17:14:22 +03:00
|
|
|
let rem = Z.abs rem in
|
|
|
|
(* Printing the integer part *)
|
2023-12-19 15:39:24 +03:00
|
|
|
if Q.sign r < 0 then Format.pp_print_char ppf '-';
|
2023-07-03 17:14:22 +03:00
|
|
|
integer lang ppf int_part;
|
|
|
|
(* Printing the decimals *)
|
|
|
|
let bigsep, nsep = bigsep lang in
|
|
|
|
let rec aux ndigit rem_digits rem =
|
|
|
|
let n, rem = Z.div_rem (Z.mul rem (Z.of_int 10)) den in
|
|
|
|
let rem_digits, stop =
|
|
|
|
match rem_digits with
|
|
|
|
| None ->
|
|
|
|
if Z.equal n Z.zero then None, false
|
|
|
|
else
|
2024-03-15 16:23:30 +03:00
|
|
|
let r = Global.options.max_prec_digits in
|
2023-07-03 17:14:22 +03:00
|
|
|
Some (r - 1), r <= 1
|
|
|
|
| Some r -> Some (r - 1), r <= 1
|
|
|
|
in
|
|
|
|
if ndigit mod nsep = 0 then
|
|
|
|
Format.pp_print_string ppf (if ndigit = 0 then decsep lang else bigsep);
|
|
|
|
Format.pp_print_int ppf (Z.to_int n);
|
|
|
|
if Z.gt rem Z.zero then
|
|
|
|
if stop then Format.pp_print_as ppf 1 "…"
|
|
|
|
else aux (ndigit + 1) rem_digits rem
|
|
|
|
in
|
|
|
|
let rec ndigits n =
|
|
|
|
if Z.equal n Z.zero then 0 else 1 + ndigits (Z.div n (Z.of_int 10))
|
|
|
|
in
|
|
|
|
aux 0
|
2023-07-03 17:38:54 +03:00
|
|
|
(if Z.equal int_part Z.zero then None
|
2024-03-15 16:23:30 +03:00
|
|
|
else Some (Global.options.max_prec_digits - ndigits int_part))
|
2023-07-03 17:14:22 +03:00
|
|
|
rem
|
|
|
|
(* It would be nice to print ratios as % but that's impossible to guess.
|
|
|
|
Trying would lead to inconsistencies where some comparable numbers are in %
|
|
|
|
and some others not, adding confusion. *)
|
|
|
|
|
2024-03-15 16:23:30 +03:00
|
|
|
let date (lang : Global.backend_lang) ppf d =
|
2024-05-02 16:57:19 +03:00
|
|
|
let y, m, d = Runtime.date_to_years_months_days d in
|
2023-07-03 17:14:22 +03:00
|
|
|
match lang with
|
|
|
|
| En | Pl -> Format.fprintf ppf "%04d-%02d-%02d" y m d
|
|
|
|
| Fr -> Format.fprintf ppf "%02d/%02d/%04d" d m y
|
|
|
|
|
2024-03-15 16:23:30 +03:00
|
|
|
let duration (lang : Global.backend_lang) ppf dr =
|
2024-05-02 16:57:19 +03:00
|
|
|
let y, m, d = Runtime.duration_to_years_months_days dr in
|
2023-07-03 17:14:22 +03:00
|
|
|
let rec filter0 = function
|
|
|
|
| (0, _) :: (_ :: _ as r) -> filter0 r
|
|
|
|
| x :: r -> x :: List.filter (fun (n, _) -> n <> 0) r
|
|
|
|
| [] -> []
|
|
|
|
in
|
|
|
|
let splur n s = if abs n > 1 then n, s ^ "s" else n, s in
|
|
|
|
Format.pp_print_char ppf '[';
|
|
|
|
(match lang with
|
|
|
|
| En -> [splur y "year"; splur m "month"; splur d "day"]
|
|
|
|
| Fr -> [splur y "an"; m, "mois"; splur d "jour"]
|
|
|
|
| Pl -> [y, "rok"; m, "miesiac"; d, "dzien"])
|
|
|
|
|> filter0
|
|
|
|
|> Format.pp_print_list
|
|
|
|
~pp_sep:(fun ppf () -> Format.pp_print_string ppf ", ")
|
|
|
|
(fun ppf (n, s) -> Format.fprintf ppf "%d %s" n s)
|
|
|
|
ppf;
|
|
|
|
Format.pp_print_char ppf ']'
|
|
|
|
|
2024-03-15 16:23:30 +03:00
|
|
|
let lit_raw (lang : Global.backend_lang) ppf lit : unit =
|
2023-07-03 17:14:22 +03:00
|
|
|
match lit with
|
|
|
|
| LUnit -> unit lang ppf ()
|
|
|
|
| LBool b -> bool lang ppf b
|
|
|
|
| LInt i -> integer lang ppf i
|
|
|
|
| LRat r -> decimal lang ppf r
|
|
|
|
| LMoney e -> money lang ppf e
|
|
|
|
| LDate d -> date lang ppf d
|
|
|
|
| LDuration dr -> duration lang ppf dr
|
|
|
|
|
2024-03-15 16:23:30 +03:00
|
|
|
let lit_to_string (lang : Global.backend_lang) lit =
|
2023-07-03 17:14:22 +03:00
|
|
|
let buf = Buffer.create 32 in
|
|
|
|
let ppf = Format.formatter_of_buffer buf in
|
|
|
|
lit_raw lang ppf lit;
|
|
|
|
Format.pp_print_flush ppf ();
|
|
|
|
Buffer.contents buf
|
|
|
|
|
2024-03-15 16:23:30 +03:00
|
|
|
let lit (lang : Global.backend_lang) ppf lit : unit =
|
2023-07-03 17:14:22 +03:00
|
|
|
with_color (lit_raw lang) Ocolor_types.yellow ppf lit
|
|
|
|
|
|
|
|
let rec value :
|
2023-07-11 18:10:00 +03:00
|
|
|
type a.
|
|
|
|
?fallback:(Format.formatter -> (a, 't) gexpr -> unit) ->
|
2024-03-15 16:23:30 +03:00
|
|
|
Global.backend_lang ->
|
2023-07-03 17:14:22 +03:00
|
|
|
Format.formatter ->
|
2023-07-11 18:10:00 +03:00
|
|
|
(a, 't) gexpr ->
|
2023-07-03 17:14:22 +03:00
|
|
|
unit =
|
2023-07-11 18:10:00 +03:00
|
|
|
fun ?(fallback = fun _ _ -> invalid_arg "UserPrint.value: not a value") lang
|
|
|
|
ppf e ->
|
2023-07-03 17:14:22 +03:00
|
|
|
match Mark.remove e with
|
|
|
|
| ELit l -> lit lang ppf l
|
2024-01-25 19:37:00 +03:00
|
|
|
| EArray l ->
|
2023-10-13 18:25:21 +03:00
|
|
|
Format.fprintf ppf "@[<hv 2>[@,@[<hov>%a@]@;<0 -2>]@]"
|
2023-07-03 17:14:22 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun ppf () -> Format.fprintf ppf ";@ ")
|
2023-07-11 18:10:00 +03:00
|
|
|
(value ~fallback lang))
|
2023-07-03 17:14:22 +03:00
|
|
|
l
|
2024-06-21 13:17:31 +03:00
|
|
|
| ETuple [(EAbs { tys = (TClosureEnv, _) :: _; _ }, _); _] ->
|
2024-06-21 12:21:17 +03:00
|
|
|
Format.pp_print_string ppf "<function>"
|
2024-01-25 19:37:00 +03:00
|
|
|
| ETuple l ->
|
|
|
|
Format.fprintf ppf "@[<hv 2>(@,@[<hov>%a@]@;<0 -2>)@]"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun ppf () -> Format.fprintf ppf ",@ ")
|
|
|
|
(value ~fallback lang))
|
|
|
|
l
|
2023-07-03 17:14:22 +03:00
|
|
|
| EStruct { name; fields } ->
|
2023-07-12 12:48:46 +03:00
|
|
|
Format.fprintf ppf "@[<hv 2>%a {@ %a@;<1 -2>}@]" StructName.format name
|
|
|
|
(StructField.Map.format_bindings ~pp_sep:Format.pp_print_space
|
|
|
|
(fun ppf pp_fld e ->
|
2023-10-13 18:25:21 +03:00
|
|
|
Format.fprintf ppf "@[<hov 2>-- %t:@ %a@]" pp_fld
|
|
|
|
(value ~fallback lang) e))
|
2023-07-12 12:48:46 +03:00
|
|
|
fields
|
2023-07-03 17:14:22 +03:00
|
|
|
| EInj { name = _; cons; e } ->
|
2023-10-13 18:25:21 +03:00
|
|
|
Format.fprintf ppf "@[<hov 2>%a@ %a@]" EnumConstructor.format cons
|
2023-07-11 18:10:00 +03:00
|
|
|
(value ~fallback lang) e
|
2024-04-26 19:31:26 +03:00
|
|
|
| EEmpty -> Format.pp_print_string ppf "ø"
|
2023-07-03 17:14:22 +03:00
|
|
|
| EAbs _ -> Format.pp_print_string ppf "<function>"
|
|
|
|
| EExternal _ -> Format.pp_print_string ppf "<external>"
|
2023-12-18 12:25:00 +03:00
|
|
|
| EApp _ | EAppOp _ | EVar _ | EIfThenElse _ | EMatch _ | ETupleAccess _
|
2024-04-26 19:31:26 +03:00
|
|
|
| EStructAccess _ | EAssert _ | EFatalError _ | EDefault _ | EPureDefault _
|
|
|
|
| EErrorOnEmpty _ | ERaiseEmpty | ECatchEmpty _ | ELocation _ | EScopeCall _
|
2024-04-11 19:30:51 +03:00
|
|
|
| EDStructAmend _ | EDStructAccess _ | ECustom _ ->
|
2023-07-11 18:10:00 +03:00
|
|
|
fallback ppf e
|
|
|
|
|
|
|
|
let expr :
|
2024-03-15 16:23:30 +03:00
|
|
|
type a. Global.backend_lang -> Format.formatter -> (a, 't) gexpr -> unit =
|
2023-07-11 18:10:00 +03:00
|
|
|
fun lang ->
|
|
|
|
let rec aux_value : type a t. Format.formatter -> (a, t) gexpr -> unit =
|
|
|
|
fun ppf e -> value ~fallback lang ppf e
|
|
|
|
and fallback : type a t. Format.formatter -> (a, t) gexpr -> unit =
|
|
|
|
fun ppf e ->
|
|
|
|
let module E = ExprGen (struct
|
|
|
|
let bypass : type a t. Format.formatter -> (a, t) gexpr -> bool =
|
|
|
|
fun ppf e ->
|
|
|
|
match Mark.remove e with
|
2024-04-26 19:31:26 +03:00
|
|
|
| EArray _ | ETuple _ | EStruct _ | EInj _ | EEmpty | EAbs _
|
2023-07-11 18:10:00 +03:00
|
|
|
| EExternal _ ->
|
|
|
|
aux_value ppf e;
|
|
|
|
true
|
|
|
|
| _ -> false
|
|
|
|
|
|
|
|
let operator o = operator ~debug:false o
|
|
|
|
let var = var
|
|
|
|
let lit = lit lang
|
|
|
|
let pre_map = skip_wrappers
|
|
|
|
end) in
|
|
|
|
E.expr ppf e
|
|
|
|
in
|
|
|
|
aux_value
|
2023-07-03 17:14:22 +03:00
|
|
|
end
|