2022-03-08 17:03:14 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2020 Inria, contributor:
|
|
|
|
Denis Merigoux <denis.merigoux@inria.fr>
|
2020-11-27 18:27:10 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
the License at
|
2020-11-27 18:27:10 +03:00
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
License for the specific language governing permissions and limitations under
|
2020-11-27 18:27:10 +03:00
|
|
|
the License. *)
|
|
|
|
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2022-08-12 23:42:39 +03:00
|
|
|
open Shared_ast
|
2020-11-27 18:27:10 +03:00
|
|
|
open Ast
|
|
|
|
|
2022-08-25 13:09:51 +03:00
|
|
|
let struc
|
|
|
|
ctx
|
2022-03-08 17:03:14 +03:00
|
|
|
(fmt : Format.formatter)
|
2022-09-30 17:37:43 +03:00
|
|
|
(name : StructName.t)
|
2022-11-21 12:12:45 +03:00
|
|
|
(fields : typ StructField.Map.t) : unit =
|
2022-11-03 17:33:13 +03:00
|
|
|
Format.fprintf fmt "%a %a %a %a@\n@[<hov 2> %a@]@\n%a" Print.keyword "struct"
|
2022-08-17 17:14:14 +03:00
|
|
|
StructName.format_t 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) ->
|
2022-11-21 12:12:45 +03:00
|
|
|
Format.fprintf fmt "%a%a %a" StructField.format_t 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
|
2022-03-08 17:03:14 +03:00
|
|
|
(fmt : Format.formatter)
|
2022-09-30 17:37:43 +03:00
|
|
|
(name : EnumName.t)
|
2022-11-21 12:12:45 +03:00
|
|
|
(cases : typ EnumConstructor.Map.t) : unit =
|
2022-11-03 17:33:13 +03:00
|
|
|
Format.fprintf fmt "%a %a %a @\n@[<hov 2> %a@]" Print.keyword "enum"
|
2022-08-17 17:14:14 +03:00
|
|
|
EnumName.format_t 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 "|"
|
2022-08-25 13:09:51 +03:00
|
|
|
EnumConstructor.format_t field_name Print.punctuation ":"
|
|
|
|
(Print.typ ctx) typ))
|
2022-11-21 12:12:45 +03:00
|
|
|
(EnumConstructor.Map.bindings cases)
|
2021-05-29 15:15:23 +03:00
|
|
|
|
2022-08-25 13:09:51 +03:00
|
|
|
let scope ?(debug = false) ctx fmt (name, decl) =
|
2022-03-08 17:03:14 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ %a@ %a@ %a@ %a@]@\n@[<v 2> %a@]"
|
2022-08-17 17:14:14 +03:00
|
|
|
Print.keyword "let" Print.keyword "scope" ScopeName.format_t 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 "("
|
2022-08-25 13:09:51 +03:00
|
|
|
ScopeVar.format_t scope_var Print.punctuation ":" (Print.typ ctx) typ
|
2022-08-17 17:14:14 +03:00
|
|
|
Print.punctuation "|" Print.keyword
|
2022-11-07 15:50:28 +03:00
|
|
|
(match Marked.unmark vis.Desugared.Ast.io_input with
|
2022-02-14 19:01:34 +03:00
|
|
|
| NoInput -> "internal"
|
|
|
|
| OnlyInput -> "input"
|
|
|
|
| Reentrant -> "context")
|
2022-11-07 15:50:28 +03:00
|
|
|
(if Marked.unmark 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
|
2022-08-25 13:09:51 +03:00
|
|
|
"let" Print.location (Marked.unmark loc) Print.punctuation ":"
|
|
|
|
(Print.typ ctx) typ Print.punctuation "="
|
2022-05-04 18:40:55 +03:00
|
|
|
(fun fmt e ->
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark loc with
|
2023-02-13 17:00:23 +03:00
|
|
|
| SubScopeVar _ | ToplevelVar _ -> Print.expr ctx fmt e
|
2022-08-25 13:09:51 +03:00
|
|
|
| ScopelangScopeVar v -> (
|
2022-05-04 18:40:55 +03:00
|
|
|
match
|
2022-05-30 12:20:48 +03:00
|
|
|
Marked.unmark
|
2022-11-21 12:12:45 +03:00
|
|
|
(snd (ScopeVar.Map.find (Marked.unmark v) decl.scope_sig))
|
2022-05-04 18:40:55 +03:00
|
|
|
.io_input
|
|
|
|
with
|
|
|
|
| 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
|
2022-09-26 17:05:57 +03:00
|
|
|
"reentrant or by default" (Print.expr ~debug ctx) e
|
|
|
|
| _ -> Format.fprintf fmt "%a" (Print.expr ~debug ctx) e))
|
2022-05-04 18:40:55 +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"
|
2022-09-26 17:05:57 +03:00
|
|
|
(Print.expr ~debug ctx) e
|
2022-09-30 17:52:35 +03:00
|
|
|
| Call (scope_name, subscope_name, _) ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "%a %a%a%a%a" Print.keyword "call"
|
|
|
|
ScopeName.format_t scope_name Print.punctuation "["
|
|
|
|
SubScopeName.format_t 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 ();
|
|
|
|
TopdefName.format_t ppf name;
|
|
|
|
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 ();
|
|
|
|
Print.expr ctx ppf e;
|
|
|
|
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 ()
|