2022-02-14 20:22:26 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2022 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-12 23:42:39 +03:00
|
|
|
open Shared_ast
|
2022-02-14 20:22:26 +03:00
|
|
|
open Ast
|
|
|
|
|
2022-08-25 18:29:00 +03:00
|
|
|
let needs_parens (_e : expr) : bool = false
|
2022-02-14 20:22:26 +03:00
|
|
|
|
2023-02-10 20:56:40 +03:00
|
|
|
let format_var_name (fmt : Format.formatter) (v : VarName.t) : unit =
|
|
|
|
Format.fprintf fmt "%a_%s" VarName.format_t v (string_of_int (VarName.hash v))
|
2022-02-14 20:22:26 +03:00
|
|
|
|
2023-02-13 17:00:23 +03:00
|
|
|
let format_func_name (fmt : Format.formatter) (v : FuncName.t) : unit =
|
|
|
|
Format.fprintf fmt "%a_%s" FuncName.format_t v
|
|
|
|
(string_of_int (FuncName.hash v))
|
|
|
|
|
2022-02-14 20:22:26 +03:00
|
|
|
let rec format_expr
|
2022-08-12 23:42:39 +03:00
|
|
|
(decl_ctx : decl_ctx)
|
2022-02-14 20:22:26 +03:00
|
|
|
?(debug : bool = false)
|
|
|
|
(fmt : Format.formatter)
|
2022-08-25 18:29:00 +03:00
|
|
|
(e : expr) : unit =
|
2022-02-14 20:22:26 +03:00
|
|
|
let format_expr = format_expr decl_ctx ~debug in
|
2022-08-25 18:29:00 +03:00
|
|
|
let format_with_parens (fmt : Format.formatter) (e : expr) =
|
2022-02-14 20:22:26 +03:00
|
|
|
if needs_parens e then
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "%a%a%a" Print.punctuation "(" format_expr e
|
|
|
|
Print.punctuation ")"
|
2022-02-14 20:22:26 +03:00
|
|
|
else Format.fprintf fmt "%a" format_expr e
|
|
|
|
in
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark e with
|
2023-02-10 20:56:40 +03:00
|
|
|
| EVar v -> Format.fprintf fmt "%a" format_var_name v
|
2023-02-13 17:00:23 +03:00
|
|
|
| EFunc v -> Format.fprintf fmt "%a" format_func_name v
|
2022-02-14 20:22:26 +03:00
|
|
|
| EStruct (es, s) ->
|
2022-08-12 23:42:39 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ %a%a%a@]" StructName.format_t s
|
2022-08-17 17:14:14 +03:00
|
|
|
Print.punctuation "{"
|
2022-02-14 20:22:26 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
|
2022-11-17 19:13:35 +03:00
|
|
|
(fun fmt (e, (struct_field, _)) ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "%a%a%a%a %a" Print.punctuation "\""
|
2022-11-21 12:12:45 +03:00
|
|
|
StructField.format_t struct_field Print.punctuation "\""
|
2022-08-17 17:14:14 +03:00
|
|
|
Print.punctuation ":" format_expr e))
|
2022-11-17 19:13:35 +03:00
|
|
|
(List.combine es
|
2022-11-21 12:12:45 +03:00
|
|
|
(StructField.Map.bindings (StructName.Map.find s decl_ctx.ctx_structs)))
|
2022-08-17 17:14:14 +03:00
|
|
|
Print.punctuation "}"
|
2022-02-14 20:22:26 +03:00
|
|
|
| EArray es ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a%a%a@]" Print.punctuation "["
|
2022-02-14 20:22:26 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ";@ ")
|
|
|
|
(fun fmt e -> Format.fprintf fmt "%a" format_expr e))
|
2022-08-17 17:14:14 +03:00
|
|
|
es Print.punctuation "]"
|
2022-11-17 19:13:35 +03:00
|
|
|
| EStructFieldAccess (e1, field, _) ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "%a%a%a%a%a" format_expr e1 Print.punctuation "."
|
2022-11-21 12:12:45 +03:00
|
|
|
Print.punctuation "\"" StructField.format_t field Print.punctuation "\""
|
2022-11-17 19:13:35 +03:00
|
|
|
| EInj (e, cons, _) ->
|
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ %a@]" Print.enum_constructor cons
|
2022-02-14 20:22:26 +03:00
|
|
|
format_expr e
|
2022-08-17 17:14:14 +03:00
|
|
|
| ELit l -> Print.lit fmt l
|
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
|
|
|
| EApp ((EOp ((Map | Filter) as op), _), [arg1; arg2]) ->
|
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ %a@ %a@]" Print.operator op
|
|
|
|
format_with_parens arg1 format_with_parens arg2
|
|
|
|
| EApp ((EOp op, _), [arg1; arg2]) ->
|
2022-02-14 20:22:26 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ %a@ %a@]" format_with_parens arg1
|
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
|
|
|
Print.operator op format_with_parens arg2
|
|
|
|
| EApp ((EOp (Log _), _), [arg1]) when not debug ->
|
2022-02-14 20:22:26 +03:00
|
|
|
Format.fprintf fmt "%a" format_with_parens arg1
|
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
|
|
|
| EApp ((EOp op, _), [arg1]) ->
|
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ %a@]" Print.operator op format_with_parens
|
|
|
|
arg1
|
2023-02-10 20:56:40 +03:00
|
|
|
| EApp (f, []) -> Format.fprintf fmt "@[<hov 2>%a@ ()@]" format_expr f
|
2022-02-14 20:22:26 +03:00
|
|
|
| EApp (f, args) ->
|
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ %a@]" format_expr f
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@ ")
|
|
|
|
format_with_parens)
|
|
|
|
args
|
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
|
|
|
| EOp op -> Format.fprintf fmt "%a" Print.operator op
|
2022-02-14 20:22:26 +03:00
|
|
|
|
|
|
|
let rec format_statement
|
2022-08-12 23:42:39 +03:00
|
|
|
(decl_ctx : decl_ctx)
|
2022-02-14 20:22:26 +03:00
|
|
|
?(debug : bool = false)
|
|
|
|
(fmt : Format.formatter)
|
2022-05-30 12:20:48 +03:00
|
|
|
(stmt : stmt Marked.pos) : unit =
|
2022-02-14 20:22:26 +03:00
|
|
|
if debug then () else ();
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark stmt with
|
2022-02-14 20:22:26 +03:00
|
|
|
| SInnerFuncDef (name, func) ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ %a@ %a@ %a@]@\n@[<v 2> %a@]" Print.keyword
|
2023-02-10 20:56:40 +03:00
|
|
|
"let" format_var_name (Marked.unmark name)
|
2022-02-14 20:22:26 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@ ")
|
|
|
|
(fun fmt ((name, _), typ) ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "%a%a %a@ %a%a" Print.punctuation "("
|
2023-02-10 20:56:40 +03:00
|
|
|
format_var_name name Print.punctuation ":" (Print.typ decl_ctx) typ
|
|
|
|
Print.punctuation ")"))
|
2022-08-17 17:14:14 +03:00
|
|
|
func.func_params Print.punctuation "="
|
2022-02-14 20:22:26 +03:00
|
|
|
(format_block decl_ctx ~debug)
|
|
|
|
func.func_body
|
|
|
|
| SLocalDecl (name, typ) ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a %a %a@ %a@]" Print.keyword "decl"
|
2023-02-10 20:56:40 +03:00
|
|
|
format_var_name (Marked.unmark name) Print.punctuation ":"
|
2022-08-25 13:09:51 +03:00
|
|
|
(Print.typ decl_ctx) typ
|
2022-08-25 17:35:08 +03:00
|
|
|
| SLocalDef (name, naked_expr) ->
|
2023-02-10 20:56:40 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a %a@ %a@]" format_var_name
|
2022-08-17 17:14:14 +03:00
|
|
|
(Marked.unmark name) Print.punctuation "="
|
2022-02-14 20:22:26 +03:00
|
|
|
(format_expr decl_ctx ~debug)
|
2022-08-25 17:35:08 +03:00
|
|
|
naked_expr
|
2022-02-14 20:22:26 +03:00
|
|
|
| STryExcept (b_try, except, b_with) ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "@[<v 2>%a%a@ %a@]@\n@[<v 2>%a %a%a@ %a@]" Print.keyword
|
|
|
|
"try" Print.punctuation ":"
|
2022-02-14 20:22:26 +03:00
|
|
|
(format_block decl_ctx ~debug)
|
2022-08-17 17:14:14 +03:00
|
|
|
b_try Print.keyword "with" Print.except except Print.punctuation ":"
|
2022-02-14 20:22:26 +03:00
|
|
|
(format_block decl_ctx ~debug)
|
|
|
|
b_with
|
|
|
|
| SRaise except ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a %a@]" Print.keyword "raise" Print.except
|
|
|
|
except
|
2022-02-14 20:22:26 +03:00
|
|
|
| SIfThenElse (e_if, b_true, b_false) ->
|
|
|
|
Format.fprintf fmt "@[<v 2>%a @[<hov 2>%a@]%a@ %a@ @]@[<v 2>%a%a@ %a@]"
|
2022-08-17 17:14:14 +03:00
|
|
|
Print.keyword "if"
|
2022-02-14 20:22:26 +03:00
|
|
|
(format_expr decl_ctx ~debug)
|
2022-08-17 17:14:14 +03:00
|
|
|
e_if Print.punctuation ":"
|
2022-02-14 20:22:26 +03:00
|
|
|
(format_block decl_ctx ~debug)
|
2022-08-17 17:14:14 +03:00
|
|
|
b_true Print.keyword "else" Print.punctuation ":"
|
2022-02-14 20:22:26 +03:00
|
|
|
(format_block decl_ctx ~debug)
|
|
|
|
b_false
|
|
|
|
| SReturn ret ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a %a@]" Print.keyword "return"
|
2022-02-14 20:22:26 +03:00
|
|
|
(format_expr decl_ctx ~debug)
|
2022-05-30 12:20:48 +03:00
|
|
|
(ret, Marked.get_mark stmt)
|
2022-08-25 17:35:08 +03:00
|
|
|
| SAssert naked_expr ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a %a@]" Print.keyword "assert"
|
2022-02-14 20:22:26 +03:00
|
|
|
(format_expr decl_ctx ~debug)
|
2022-08-25 17:35:08 +03:00
|
|
|
(naked_expr, Marked.get_mark stmt)
|
2022-02-14 20:22:26 +03:00
|
|
|
| SSwitch (e_switch, enum, arms) ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "@[<v 0>%a @[<hov 2>%a@]%a@]%a" Print.keyword "switch"
|
2022-02-14 20:22:26 +03:00
|
|
|
(format_expr decl_ctx ~debug)
|
2022-08-17 17:14:14 +03:00
|
|
|
e_switch Print.punctuation ":"
|
2022-02-14 20:22:26 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@\n")
|
|
|
|
(fun fmt ((case, _), (arm_block, payload_name)) ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "%a %a%a@ %a @[<v 2>%a@ %a@]" Print.punctuation
|
|
|
|
"|" Print.enum_constructor case Print.punctuation ":"
|
2023-02-10 20:56:40 +03:00
|
|
|
format_var_name payload_name Print.punctuation "→"
|
2022-02-14 20:22:26 +03:00
|
|
|
(format_block decl_ctx ~debug)
|
|
|
|
arm_block))
|
2022-11-17 19:13:35 +03:00
|
|
|
(List.combine
|
2022-11-21 12:12:45 +03:00
|
|
|
(EnumConstructor.Map.bindings
|
|
|
|
(EnumName.Map.find enum decl_ctx.ctx_enums))
|
2022-11-17 19:13:35 +03:00
|
|
|
arms)
|
2022-02-14 20:22:26 +03:00
|
|
|
|
|
|
|
and format_block
|
2022-08-12 23:42:39 +03:00
|
|
|
(decl_ctx : decl_ctx)
|
2022-02-14 20:22:26 +03:00
|
|
|
?(debug : bool = false)
|
|
|
|
(fmt : Format.formatter)
|
|
|
|
(block : block) : unit =
|
|
|
|
Format.pp_print_list
|
2022-08-17 17:14:14 +03:00
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "%a@ " Print.punctuation ";")
|
2022-02-14 20:22:26 +03:00
|
|
|
(format_statement decl_ctx ~debug)
|
|
|
|
fmt block
|
|
|
|
|
2023-02-13 17:00:23 +03:00
|
|
|
let format_item decl_ctx ?debug ppf def =
|
2023-02-10 20:56:40 +03:00
|
|
|
Format.pp_open_hvbox ppf 2;
|
|
|
|
Format.pp_open_hovbox ppf 4;
|
|
|
|
Print.keyword ppf "let ";
|
|
|
|
let () =
|
|
|
|
match def with
|
2023-02-13 17:00:23 +03:00
|
|
|
| SVar { var; expr } ->
|
|
|
|
format_var_name ppf var;
|
2023-02-10 20:56:40 +03:00
|
|
|
Print.punctuation ppf " =";
|
|
|
|
Format.pp_close_box ppf ();
|
|
|
|
Format.pp_print_space ppf ();
|
|
|
|
format_expr decl_ctx ?debug ppf expr
|
2023-02-13 17:00:23 +03:00
|
|
|
| SScope { scope_body_var = var; scope_body_func = func; _ }
|
|
|
|
| SFunc { var; func } ->
|
|
|
|
format_func_name ppf var;
|
2023-02-10 20:56:40 +03:00
|
|
|
Format.pp_print_list
|
|
|
|
(fun ppf (arg, ty) ->
|
2023-02-13 17:00:23 +03:00
|
|
|
Format.fprintf ppf "@ (%a: %a)" format_var_name (Marked.unmark arg)
|
2023-02-10 20:56:40 +03:00
|
|
|
(Print.typ decl_ctx) ty)
|
2023-02-13 17:00:23 +03:00
|
|
|
ppf func.func_params;
|
2023-02-10 20:56:40 +03:00
|
|
|
Print.punctuation ppf " =";
|
|
|
|
Format.pp_close_box ppf ();
|
|
|
|
Format.pp_print_space ppf ();
|
2023-02-13 17:00:23 +03:00
|
|
|
format_block decl_ctx ?debug ppf func.func_body
|
2023-02-10 20:56:40 +03:00
|
|
|
in
|
|
|
|
Format.pp_close_box ppf ();
|
|
|
|
Format.pp_print_cut ppf ()
|
|
|
|
|
|
|
|
let format_program decl_ctx ?debug ppf prg =
|
|
|
|
Format.pp_open_vbox ppf 0;
|
2023-02-13 17:00:23 +03:00
|
|
|
Format.pp_print_list (format_item decl_ctx ?debug) ppf prg.code_items;
|
2023-02-10 20:56:40 +03:00
|
|
|
Format.pp_close_box ppf ()
|