2020-11-27 18:27:10 +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-12 23:42:39 +03:00
|
|
|
open Shared_ast
|
2020-11-27 18:27:10 +03:00
|
|
|
open Ast
|
|
|
|
|
2022-08-25 13:09:51 +03:00
|
|
|
let struc
|
|
|
|
ctx
|
2021-05-29 15:15:23 +03:00
|
|
|
(fmt : Format.formatter)
|
2022-09-30 17:37:43 +03:00
|
|
|
(name : StructName.t)
|
2023-08-16 01:04:45 +03:00
|
|
|
((path, fields) : path * typ StructField.Map.t) : unit =
|
|
|
|
Format.fprintf fmt "%a %a%a %a %a@\n@[<hov 2> %a@]@\n%a" Print.keyword
|
|
|
|
"struct" Print.path path StructName.format name Print.punctuation "="
|
|
|
|
Print.punctuation "{"
|
2021-05-29 15:15:23 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@\n")
|
|
|
|
(fun fmt (field_name, typ) ->
|
2023-07-12 12:48:46 +03:00
|
|
|
Format.fprintf fmt "%a%a %a" StructField.format field_name
|
2022-08-25 13:09:51 +03:00
|
|
|
Print.punctuation ":" (Print.typ ctx) typ))
|
2022-11-21 12:12:45 +03:00
|
|
|
(StructField.Map.bindings fields)
|
2022-11-17 19:13:35 +03:00
|
|
|
Print.punctuation "}"
|
2021-05-29 15:15:23 +03:00
|
|
|
|
2022-08-25 13:09:51 +03:00
|
|
|
let enum
|
|
|
|
ctx
|
2021-05-29 15:15:23 +03:00
|
|
|
(fmt : Format.formatter)
|
2022-09-30 17:37:43 +03:00
|
|
|
(name : EnumName.t)
|
2023-08-16 01:04:45 +03:00
|
|
|
((path, cases) : path * typ EnumConstructor.Map.t) : unit =
|
2023-08-10 17:52:39 +03:00
|
|
|
Format.fprintf fmt "%a %a%a %a @\n@[<hov 2> %a@]" Print.keyword "enum"
|
2023-08-16 01:04:45 +03:00
|
|
|
Print.path path EnumName.format name Print.punctuation "="
|
2021-05-29 15:15:23 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@\n")
|
|
|
|
(fun fmt (field_name, typ) ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "%a %a%a %a" Print.punctuation "|"
|
2023-07-12 12:48:46 +03:00
|
|
|
EnumConstructor.format field_name Print.punctuation ":"
|
2022-08-25 13:09:51 +03:00
|
|
|
(Print.typ ctx) typ))
|
2022-11-21 12:12:45 +03:00
|
|
|
(EnumConstructor.Map.bindings cases)
|
2021-05-29 15:15:23 +03:00
|
|
|
|
2023-08-10 17:52:39 +03:00
|
|
|
let scope ?debug ctx fmt (name, (decl, _pos)) =
|
2022-02-14 20:22:26 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ %a@ %a@ %a@ %a@]@\n@[<v 2> %a@]"
|
2023-07-12 12:48:46 +03:00
|
|
|
Print.keyword "let" Print.keyword "scope" ScopeName.format name
|
2022-08-25 13:09:51 +03:00
|
|
|
(Format.pp_print_list ~pp_sep:Format.pp_print_space
|
2022-02-04 16:34:25 +03:00
|
|
|
(fun fmt (scope_var, (typ, vis)) ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "%a%a%a %a%a%a%a%a" Print.punctuation "("
|
2023-07-12 12:48:46 +03:00
|
|
|
ScopeVar.format scope_var Print.punctuation ":" (Print.typ ctx) typ
|
2022-08-17 17:14:14 +03:00
|
|
|
Print.punctuation "|" Print.keyword
|
2023-05-17 16:44:57 +03:00
|
|
|
(match Mark.remove vis.Desugared.Ast.io_input with
|
2022-02-14 19:01:34 +03:00
|
|
|
| NoInput -> "internal"
|
|
|
|
| OnlyInput -> "input"
|
|
|
|
| Reentrant -> "context")
|
2023-05-17 16:44:57 +03:00
|
|
|
(if Mark.remove vis.Desugared.Ast.io_output then fun fmt () ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "%a@,%a" Print.punctuation "|" Print.keyword
|
|
|
|
"output"
|
2022-02-14 19:01:34 +03:00
|
|
|
else fun fmt () -> Format.fprintf fmt "@<0>")
|
2022-08-17 17:14:14 +03:00
|
|
|
() Print.punctuation ")"))
|
2022-11-21 12:12:45 +03:00
|
|
|
(ScopeVar.Map.bindings decl.scope_sig)
|
2022-08-17 17:14:14 +03:00
|
|
|
Print.punctuation "="
|
2021-05-29 15:15:23 +03:00
|
|
|
(Format.pp_print_list
|
2022-08-17 17:14:14 +03:00
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "%a@ " Print.punctuation ";")
|
2021-05-29 15:15:23 +03:00
|
|
|
(fun fmt rule ->
|
|
|
|
match rule with
|
2022-02-10 12:09:58 +03:00
|
|
|
| Definition (loc, typ, _, e) ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a %a %a %a %a@ %a@]" Print.keyword
|
2023-05-17 16:44:57 +03:00
|
|
|
"let" Print.location (Mark.remove loc) Print.punctuation ":"
|
2022-08-25 13:09:51 +03:00
|
|
|
(Print.typ ctx) typ Print.punctuation "="
|
2021-05-29 15:15:23 +03:00
|
|
|
(fun fmt e ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove loc with
|
2023-05-02 17:33:23 +03:00
|
|
|
| SubScopeVar _ | ToplevelVar _ -> Print.expr () fmt e
|
2023-08-10 17:52:39 +03:00
|
|
|
| ScopelangScopeVar { name = v } -> (
|
2022-02-07 12:30:36 +03:00
|
|
|
match
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.remove
|
|
|
|
(snd (ScopeVar.Map.find (Mark.remove v) decl.scope_sig))
|
2022-02-07 12:30:36 +03:00
|
|
|
.io_input
|
|
|
|
with
|
2022-02-09 17:01:24 +03:00
|
|
|
| Reentrant ->
|
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.fprintf fmt "%a@ %a" Print.op_style
|
2023-05-02 17:33:23 +03:00
|
|
|
"reentrant or by default" (Print.expr ?debug ()) e
|
|
|
|
| _ -> Format.fprintf fmt "%a" (Print.expr ?debug ()) e))
|
2021-05-29 15:15:23 +03:00
|
|
|
e
|
2022-02-09 17:01:24 +03:00
|
|
|
| Assertion e ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "%a %a" Print.keyword "assert"
|
2023-05-02 17:33:23 +03:00
|
|
|
(Print.expr ?debug ()) e
|
2023-08-10 17:52:39 +03:00
|
|
|
| Call ((scope_path, scope_name), subscope_name, _) ->
|
2023-08-16 01:04:45 +03:00
|
|
|
Format.fprintf fmt "%a %a%a%a%a%a" Print.keyword "call" Print.path
|
|
|
|
scope_path ScopeName.format scope_name Print.punctuation "["
|
2023-07-12 12:48:46 +03:00
|
|
|
SubScopeName.format subscope_name Print.punctuation "]"))
|
2021-05-29 15:15:23 +03:00
|
|
|
decl.scope_decl_rules
|
|
|
|
|
2023-02-08 18:55:07 +03:00
|
|
|
let print_topdef ctx ppf name (e, ty) =
|
|
|
|
Format.pp_open_vbox ppf 2;
|
|
|
|
let () =
|
|
|
|
Format.pp_open_hovbox ppf 2;
|
|
|
|
Print.keyword ppf "let";
|
|
|
|
Format.pp_print_space ppf ();
|
2023-07-12 12:48:46 +03:00
|
|
|
TopdefName.format ppf name;
|
2023-02-08 18:55:07 +03:00
|
|
|
Print.punctuation ppf ":";
|
|
|
|
Format.pp_print_space ppf ();
|
|
|
|
Print.typ ctx ppf ty;
|
|
|
|
Format.pp_print_space ppf ();
|
|
|
|
Print.punctuation ppf "=";
|
|
|
|
Format.pp_close_box ppf ()
|
|
|
|
in
|
|
|
|
Format.pp_print_cut ppf ();
|
2023-05-02 17:33:23 +03:00
|
|
|
Print.expr () ppf e;
|
2023-02-08 18:55:07 +03:00
|
|
|
Format.pp_close_box ppf ()
|
|
|
|
|
2022-09-23 18:43:48 +03:00
|
|
|
let program ?(debug : bool = false) (fmt : Format.formatter) (p : 'm program) :
|
2022-08-25 13:09:51 +03:00
|
|
|
unit =
|
|
|
|
let ctx = p.program_ctx in
|
|
|
|
let pp_sep fmt () =
|
|
|
|
Format.pp_print_cut fmt ();
|
|
|
|
Format.pp_print_cut fmt ()
|
|
|
|
in
|
2022-09-30 17:37:43 +03:00
|
|
|
Format.pp_open_vbox fmt 0;
|
2022-11-21 12:12:45 +03:00
|
|
|
StructName.Map.iter
|
2022-09-30 17:37:43 +03:00
|
|
|
(fun n s ->
|
|
|
|
struc ctx fmt n s;
|
|
|
|
pp_sep fmt ())
|
|
|
|
ctx.ctx_structs;
|
2022-11-21 12:12:45 +03:00
|
|
|
EnumName.Map.iter
|
2022-09-30 17:37:43 +03:00
|
|
|
(fun n e ->
|
|
|
|
enum ctx fmt n e;
|
|
|
|
pp_sep fmt ())
|
|
|
|
ctx.ctx_enums;
|
2023-02-08 18:55:07 +03:00
|
|
|
TopdefName.Map.iter
|
|
|
|
(fun name def ->
|
|
|
|
print_topdef ctx fmt name def;
|
|
|
|
pp_sep fmt ())
|
2023-02-13 17:00:23 +03:00
|
|
|
p.program_topdefs;
|
2022-09-30 17:37:43 +03:00
|
|
|
Format.pp_print_list ~pp_sep (scope ~debug ctx) fmt
|
2022-11-21 12:12:45 +03:00
|
|
|
(ScopeName.Map.bindings p.program_scopes);
|
2022-09-30 17:37:43 +03:00
|
|
|
Format.pp_close_box fmt ()
|