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. *)
|
|
|
|
|
2021-01-21 23:33:04 +03:00
|
|
|
open 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)
|
|
|
|
(fields : (StructFieldName.t * typ) list) : 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-02-09 17:01:24 +03:00
|
|
|
Format.fprintf fmt "%a%a %a" StructFieldName.format_t field_name
|
2022-08-25 13:09:51 +03:00
|
|
|
Print.punctuation ":" (Print.typ ctx) typ))
|
2022-08-17 17:14:14 +03:00
|
|
|
fields 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)
|
|
|
|
(cases : (EnumConstructor.t * typ) list) : 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))
|
2021-05-29 15:15:23 +03:00
|
|
|
cases
|
|
|
|
|
2022-08-25 13:09:51 +03:00
|
|
|
let scope ?(debug = false) ctx fmt (name, decl) =
|
2022-02-14 20:22:26 +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-05-30 12:20:48 +03:00
|
|
|
(match Marked.unmark vis.io_input with
|
2022-02-14 19:01:34 +03:00
|
|
|
| NoInput -> "internal"
|
|
|
|
| OnlyInput -> "input"
|
|
|
|
| Reentrant -> "context")
|
2022-05-30 12:20:48 +03:00
|
|
|
(if Marked.unmark vis.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 ")"))
|
2021-05-29 15:15:23 +03:00
|
|
|
(ScopeVarMap.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 "="
|
2021-05-29 15:15:23 +03:00
|
|
|
(fun fmt e ->
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark loc with
|
2022-09-26 17:05:57 +03:00
|
|
|
| SubScopeVar _ -> Print.expr ctx fmt e
|
2022-08-25 13:09:51 +03:00
|
|
|
| ScopelangScopeVar v -> (
|
2022-02-07 12:30:36 +03:00
|
|
|
match
|
2022-05-30 12:20:48 +03:00
|
|
|
Marked.unmark
|
|
|
|
(snd (ScopeVarMap.find (Marked.unmark v) decl.scope_sig))
|
2022-02-07 12:30:36 +03:00
|
|
|
.io_input
|
|
|
|
with
|
2022-02-09 17:01:24 +03:00
|
|
|
| Reentrant ->
|
2022-08-17 17:14:14 +03:00
|
|
|
Format.fprintf fmt "%a@ %a" Print.operator
|
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))
|
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"
|
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
|
|
|
|
|
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;
|
|
|
|
StructMap.iter
|
|
|
|
(fun n s ->
|
|
|
|
struc ctx fmt n s;
|
|
|
|
pp_sep fmt ())
|
|
|
|
ctx.ctx_structs;
|
|
|
|
EnumMap.iter
|
|
|
|
(fun n e ->
|
|
|
|
enum ctx fmt n e;
|
|
|
|
pp_sep fmt ())
|
|
|
|
ctx.ctx_enums;
|
|
|
|
Format.pp_print_list ~pp_sep (scope ~debug ctx) fmt
|
|
|
|
(ScopeMap.bindings p.program_scopes);
|
|
|
|
Format.pp_close_box fmt ()
|