2022-03-04 20:32:39 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
2022-07-21 17:18:36 +03:00
|
|
|
and social benefits computation rules. Copyright (C) 2020 Inria,
|
|
|
|
contributors: Emile Rolley <emile.rolley@tuta.io>, Louis Gesbert
|
|
|
|
<louis.gesbert@inria.fr>.
|
2022-03-04 20:32:39 +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
|
|
|
|
|
|
|
|
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-07-27 13:29:09 +03:00
|
|
|
(** Catala plugin for generating web APIs.
|
|
|
|
|
|
|
|
It generates:
|
|
|
|
|
|
|
|
- the OCaml code,
|
2022-07-29 14:39:33 +03:00
|
|
|
- the associated [js_of_ocaml] wrapper. *)
|
2022-03-04 20:32:39 +03:00
|
|
|
|
2022-07-18 20:19:56 +03:00
|
|
|
open Utils
|
2022-07-19 18:59:45 +03:00
|
|
|
open Lcalc
|
2022-07-18 20:19:56 +03:00
|
|
|
open Lcalc.Ast
|
|
|
|
open Lcalc.Backends
|
2022-07-19 18:59:45 +03:00
|
|
|
open Lcalc.To_ocaml
|
2022-07-18 20:19:56 +03:00
|
|
|
module D = Dcalc.Ast
|
|
|
|
|
2022-07-29 14:39:33 +03:00
|
|
|
let name = "api_web"
|
2022-07-22 13:34:46 +03:00
|
|
|
let extension = ".ml"
|
2022-07-20 15:20:21 +03:00
|
|
|
|
2022-07-27 17:09:37 +03:00
|
|
|
(** Contains all format functions used to generating the [js_of_ocaml] wrapper
|
|
|
|
of the corresponding Catala program. *)
|
2022-07-18 20:19:56 +03:00
|
|
|
module To_jsoo = struct
|
2022-07-29 14:39:33 +03:00
|
|
|
let to_camel_case (s : string) : string =
|
|
|
|
String.split_on_char '_' s
|
|
|
|
|> (function
|
|
|
|
| hd :: tl -> hd :: List.map String.capitalize_ascii tl | l -> l)
|
|
|
|
|> String.concat ""
|
|
|
|
|
|
|
|
let format_struct_field_name_camel_case
|
|
|
|
(fmt : Format.formatter)
|
|
|
|
(v : Dcalc.Ast.StructFieldName.t) : unit =
|
|
|
|
let s =
|
|
|
|
Format.asprintf "%a" Dcalc.Ast.StructFieldName.format_t v
|
|
|
|
|> to_ascii |> to_lowercase |> avoid_keywords |> to_camel_case
|
|
|
|
in
|
|
|
|
Format.fprintf fmt "%s" s
|
|
|
|
|
2022-07-19 18:59:45 +03:00
|
|
|
let format_tlit (fmt : Format.formatter) (l : Dcalc.Ast.typ_lit) : unit =
|
|
|
|
Dcalc.Print.format_base_type fmt
|
|
|
|
(match l with
|
2022-07-22 20:26:56 +03:00
|
|
|
| TUnit -> "unit"
|
2022-07-19 18:59:45 +03:00
|
|
|
| TInt -> "int"
|
2022-07-27 17:09:37 +03:00
|
|
|
| TRat | TMoney -> "Js.number Js.t"
|
2022-07-22 20:26:56 +03:00
|
|
|
| TDuration -> "Runtime_jsoo.Runtime.duration Js.t"
|
2022-07-19 18:59:45 +03:00
|
|
|
| TBool -> "bool Js.t"
|
2022-07-26 18:02:00 +03:00
|
|
|
| TDate -> "Js.js_string Js.t")
|
2022-07-18 20:19:56 +03:00
|
|
|
|
2022-07-20 13:00:33 +03:00
|
|
|
let rec format_typ (fmt : Format.formatter) (typ : Dcalc.Ast.typ Marked.pos) :
|
2022-07-18 20:19:56 +03:00
|
|
|
unit =
|
|
|
|
let format_typ_with_parens
|
|
|
|
(fmt : Format.formatter)
|
2022-07-20 13:00:33 +03:00
|
|
|
(t : Dcalc.Ast.typ Marked.pos) =
|
2022-07-18 20:19:56 +03:00
|
|
|
if typ_needs_parens t then Format.fprintf fmt "(%a)" format_typ t
|
|
|
|
else Format.fprintf fmt "%a" format_typ t
|
|
|
|
in
|
2022-07-20 13:00:33 +03:00
|
|
|
match Marked.unmark typ with
|
2022-07-18 20:19:56 +03:00
|
|
|
| TLit l -> Format.fprintf fmt "%a" format_tlit l
|
|
|
|
| TTuple (_, Some s) -> Format.fprintf fmt "%a Js.t" format_struct_name s
|
|
|
|
| TTuple (_, None) ->
|
|
|
|
(* Tuples are encoded as an javascript polymorphic array. *)
|
|
|
|
Format.fprintf fmt "Js.Unsafe.any_js_array Js.t "
|
|
|
|
| TEnum ([t], e) when D.EnumName.compare e option_enum = 0 ->
|
|
|
|
Format.fprintf fmt "@[<hov 2>(%a)@] %a" format_typ_with_parens t
|
|
|
|
format_enum_name e
|
|
|
|
| TEnum (_, e) when D.EnumName.compare e option_enum = 0 ->
|
2022-07-20 13:00:33 +03:00
|
|
|
Errors.raise_spanned_error (Marked.get_mark typ)
|
2022-07-18 20:19:56 +03:00
|
|
|
"Internal Error: found an typing parameter for an eoption type of the \
|
2022-07-22 19:04:16 +03:00
|
|
|
wrong length."
|
2022-07-18 20:19:56 +03:00
|
|
|
| TEnum (_, e) -> Format.fprintf fmt "%a Js.t" format_enum_name e
|
|
|
|
| TArray t1 ->
|
|
|
|
Format.fprintf fmt "@[%a@ Js.js_array Js.t@]" format_typ_with_parens t1
|
|
|
|
| TAny -> Format.fprintf fmt "Js.Unsafe.any Js.t"
|
|
|
|
| TArrow (t1, t2) ->
|
2022-07-21 16:52:35 +03:00
|
|
|
Format.fprintf fmt "(@[<hov 2>%a, @ %a@]) Js.meth_callback"
|
|
|
|
format_typ_with_parens t1 format_typ_with_parens t2
|
|
|
|
|
|
|
|
let rec format_typ_to_jsoo fmt typ =
|
|
|
|
match Marked.unmark typ with
|
|
|
|
| Dcalc.Ast.TLit TBool -> Format.fprintf fmt "Js.bool"
|
|
|
|
| Dcalc.Ast.TLit TInt -> Format.fprintf fmt "integer_to_int"
|
2022-07-23 19:00:04 +03:00
|
|
|
| Dcalc.Ast.TLit TRat ->
|
|
|
|
Format.fprintf fmt "Js.number_of_float %@%@ decimal_to_float"
|
|
|
|
| Dcalc.Ast.TLit TMoney ->
|
|
|
|
Format.fprintf fmt "Js.number_of_float %@%@ money_to_float"
|
2022-07-21 16:52:35 +03:00
|
|
|
| Dcalc.Ast.TLit TDuration -> Format.fprintf fmt "duration_to_jsoo"
|
|
|
|
| Dcalc.Ast.TLit TDate -> Format.fprintf fmt "date_to_jsoo"
|
|
|
|
| Dcalc.Ast.TEnum (_, ename) ->
|
|
|
|
Format.fprintf fmt "%a_to_jsoo" format_enum_name ename
|
|
|
|
| Dcalc.Ast.TTuple (_, Some sname) ->
|
|
|
|
Format.fprintf fmt "%a_to_jsoo" format_struct_name sname
|
|
|
|
| Dcalc.Ast.TArray t ->
|
2022-07-23 19:00:04 +03:00
|
|
|
Format.fprintf fmt "Js.array %@%@ Array.map (fun x -> %a x)"
|
|
|
|
format_typ_to_jsoo t
|
2022-07-21 16:52:35 +03:00
|
|
|
| Dcalc.Ast.TAny | Dcalc.Ast.TTuple (_, None) ->
|
|
|
|
Format.fprintf fmt "Js.Unsafe.inject"
|
|
|
|
| _ -> Format.fprintf fmt ""
|
|
|
|
|
|
|
|
let rec format_typ_of_jsoo fmt typ =
|
|
|
|
match Marked.unmark typ with
|
|
|
|
| Dcalc.Ast.TLit TBool -> Format.fprintf fmt "Js.to_bool"
|
|
|
|
| Dcalc.Ast.TLit TInt -> Format.fprintf fmt "integer_of_int"
|
2022-07-23 19:00:04 +03:00
|
|
|
| Dcalc.Ast.TLit TRat ->
|
|
|
|
Format.fprintf fmt "decimal_of_float %@%@ Js.float_of_number"
|
2022-07-21 17:18:36 +03:00
|
|
|
| Dcalc.Ast.TLit TMoney ->
|
2022-07-23 19:00:04 +03:00
|
|
|
Format.fprintf fmt
|
|
|
|
"money_of_decimal %@%@ decimal_of_float %@%@ Js.float_of_number"
|
2022-07-21 16:52:35 +03:00
|
|
|
| Dcalc.Ast.TLit TDuration -> Format.fprintf fmt "duration_of_jsoo"
|
|
|
|
| Dcalc.Ast.TLit TDate -> Format.fprintf fmt "date_of_jsoo"
|
|
|
|
| Dcalc.Ast.TEnum (_, ename) ->
|
|
|
|
Format.fprintf fmt "%a_of_jsoo" format_enum_name ename
|
|
|
|
| Dcalc.Ast.TTuple (_, Some sname) ->
|
|
|
|
Format.fprintf fmt "%a_of_jsoo" format_struct_name sname
|
|
|
|
| Dcalc.Ast.TArray t ->
|
2022-07-23 19:00:04 +03:00
|
|
|
Format.fprintf fmt "Array.map (fun x -> %a x) %@%@ Js.to_array"
|
|
|
|
format_typ_of_jsoo t
|
2022-07-21 16:52:35 +03:00
|
|
|
| _ -> Format.fprintf fmt ""
|
2022-07-18 20:19:56 +03:00
|
|
|
|
2022-07-20 13:00:33 +03:00
|
|
|
let format_var_camel_case (fmt : Format.formatter) (v : 'm var) : unit =
|
2022-07-18 20:19:56 +03:00
|
|
|
let lowercase_name =
|
2022-07-19 18:59:45 +03:00
|
|
|
Bindlib.name_of v |> to_ascii |> to_lowercase
|
|
|
|
|> Re.Pcre.substitute ~rex:(Re.Pcre.regexp "\\.") ~subst:(fun _ ->
|
|
|
|
"_dot_")
|
|
|
|
|> to_ascii |> avoid_keywords |> to_camel_case
|
2022-07-18 20:19:56 +03:00
|
|
|
in
|
|
|
|
if
|
|
|
|
List.mem lowercase_name ["handle_default"; "handle_default_opt"]
|
|
|
|
|| Dcalc.Print.begins_with_uppercase (Bindlib.name_of v)
|
|
|
|
then Format.fprintf fmt "%s" lowercase_name
|
|
|
|
else if lowercase_name = "_" then Format.fprintf fmt "%s" lowercase_name
|
|
|
|
else Format.fprintf fmt "%s_" lowercase_name
|
|
|
|
|
|
|
|
let format_ctx
|
|
|
|
(type_ordering : Scopelang.Dependency.TVertex.t list)
|
|
|
|
(fmt : Format.formatter)
|
|
|
|
(ctx : D.decl_ctx) : unit =
|
2022-07-20 13:00:33 +03:00
|
|
|
let format_prop_or_meth fmt (struct_field_type : D.typ Marked.pos) =
|
|
|
|
match Marked.unmark struct_field_type with
|
2022-07-18 20:19:56 +03:00
|
|
|
| Dcalc.Ast.TArrow _ -> Format.fprintf fmt "Js.meth"
|
|
|
|
| _ -> Format.fprintf fmt "Js.readonly_prop"
|
|
|
|
in
|
|
|
|
let format_struct_decl fmt (struct_name, struct_fields) =
|
2022-07-20 19:25:41 +03:00
|
|
|
let fmt_struct_name fmt _ = format_struct_name fmt struct_name in
|
|
|
|
let fmt_module_struct_name fmt _ =
|
|
|
|
To_ocaml.format_to_module_name fmt (`Sname struct_name)
|
|
|
|
in
|
2022-07-21 16:52:35 +03:00
|
|
|
let fmt_to_jsoo fmt _ =
|
|
|
|
Format.fprintf fmt "%a"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@\n")
|
|
|
|
(fun fmt (struct_field, struct_field_type) ->
|
|
|
|
match Marked.unmark struct_field_type with
|
|
|
|
| Dcalc.Ast.TArrow (t1, t2) ->
|
|
|
|
Format.fprintf fmt
|
|
|
|
"method %a@ =@ Js.wrap_meth_callback@ (@[<hov 2>fun input ->@\n\
|
|
|
|
%a (%a.%a (%a input))@])"
|
|
|
|
format_struct_field_name_camel_case struct_field
|
|
|
|
format_typ_to_jsoo t2 fmt_struct_name ()
|
|
|
|
format_struct_field_name (None, struct_field)
|
|
|
|
format_typ_of_jsoo t1
|
|
|
|
| _ ->
|
|
|
|
Format.fprintf fmt "val %a@ =@ %a %a.%a"
|
|
|
|
format_struct_field_name_camel_case struct_field
|
|
|
|
format_typ_to_jsoo struct_field_type fmt_struct_name ()
|
|
|
|
format_struct_field_name (None, struct_field)))
|
|
|
|
struct_fields
|
|
|
|
in
|
|
|
|
let fmt_of_jsoo fmt _ =
|
|
|
|
Format.fprintf fmt "%a"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ";@\n")
|
|
|
|
(fun fmt (struct_field, struct_field_type) ->
|
|
|
|
match Marked.unmark struct_field_type with
|
|
|
|
| Dcalc.Ast.TArrow _ ->
|
|
|
|
Format.fprintf fmt
|
2022-07-21 17:18:36 +03:00
|
|
|
"%a = failwith \"The function '%a' translation isn't yet \
|
|
|
|
supported...\""
|
2022-07-21 16:52:35 +03:00
|
|
|
format_struct_field_name (None, struct_field)
|
|
|
|
format_struct_field_name (None, struct_field)
|
|
|
|
| _ ->
|
|
|
|
Format.fprintf fmt "%a@ =@ %a %a##.%a" format_struct_field_name
|
|
|
|
(None, struct_field) format_typ_of_jsoo struct_field_type
|
|
|
|
fmt_struct_name () format_struct_field_name_camel_case
|
|
|
|
struct_field))
|
|
|
|
struct_fields
|
|
|
|
in
|
|
|
|
let fmt_conv_funs fmt _ =
|
|
|
|
Format.fprintf fmt
|
|
|
|
"let %a_to_jsoo (%a : %a.t) : %a Js.t = object%%js@\n\
|
|
|
|
@[<hov 2>%a@]@\n\
|
|
|
|
end@\n\
|
|
|
|
let %a_of_jsoo (%a : %a Js.t) : %a.t = {@[<hov 2>%a@]}"
|
|
|
|
fmt_struct_name () fmt_struct_name () fmt_module_struct_name ()
|
|
|
|
fmt_struct_name () fmt_to_jsoo () fmt_struct_name () fmt_struct_name
|
|
|
|
() fmt_struct_name () fmt_module_struct_name () fmt_of_jsoo ()
|
|
|
|
in
|
|
|
|
|
2022-07-18 20:19:56 +03:00
|
|
|
if List.length struct_fields = 0 then
|
2022-07-20 19:25:41 +03:00
|
|
|
Format.fprintf fmt
|
|
|
|
"class type %a =@ object end@\n\
|
2022-07-21 16:52:35 +03:00
|
|
|
let %a_to_jsoo (_ : %a.t) : %a Js.t = object%%js end@\n\
|
2022-07-20 19:25:41 +03:00
|
|
|
let %a_of_jsoo (_ : %a Js.t) : %a.t = ()" fmt_struct_name ()
|
|
|
|
fmt_struct_name () fmt_module_struct_name () fmt_struct_name ()
|
|
|
|
fmt_struct_name () fmt_struct_name () fmt_module_struct_name ()
|
2022-07-18 20:19:56 +03:00
|
|
|
else
|
|
|
|
Format.fprintf fmt
|
2022-07-21 16:52:35 +03:00
|
|
|
"class type %a =@\n@[<hov 2>object@ @[<hov 2>@ @ %a@]@\nend@]@\n%a@\n"
|
2022-07-20 19:25:41 +03:00
|
|
|
fmt_struct_name ()
|
2022-07-18 20:19:56 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@\n")
|
2022-07-21 17:18:36 +03:00
|
|
|
(fun fmt (struct_field, struct_field_type) ->
|
|
|
|
Format.fprintf fmt "method %a:@ %a %a"
|
|
|
|
format_struct_field_name_camel_case struct_field format_typ
|
|
|
|
struct_field_type format_prop_or_meth struct_field_type))
|
2022-07-21 16:52:35 +03:00
|
|
|
struct_fields fmt_conv_funs ()
|
2022-07-18 20:19:56 +03:00
|
|
|
in
|
2022-07-21 16:52:35 +03:00
|
|
|
let format_enum_decl
|
|
|
|
fmt
|
|
|
|
(enum_name, (enum_cons : (D.EnumConstructor.t * D.typ Marked.pos) list))
|
|
|
|
=
|
|
|
|
let fmt_enum_name fmt _ = format_enum_name fmt enum_name in
|
|
|
|
let fmt_module_enum_name fmt _ =
|
|
|
|
To_ocaml.format_to_module_name fmt (`Ename enum_name)
|
|
|
|
in
|
|
|
|
let fmt_to_jsoo fmt _ =
|
|
|
|
Format.fprintf fmt "%a"
|
2022-07-18 20:19:56 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@\n")
|
2022-07-21 16:52:35 +03:00
|
|
|
(fun fmt (cname, typ) ->
|
|
|
|
match Marked.unmark typ with
|
2022-07-21 17:18:36 +03:00
|
|
|
| Dcalc.Ast.TTuple (_, None) ->
|
|
|
|
Cli.error_print
|
|
|
|
"Tuples aren't supported yet in the conversion to JS"
|
|
|
|
| _ ->
|
2022-07-21 16:52:35 +03:00
|
|
|
Format.fprintf fmt
|
|
|
|
"| %a arg -> object%%js@[<hov 2>@\n\
|
|
|
|
val kind = Js.string \"%a\"@\n\
|
2022-07-21 17:18:36 +03:00
|
|
|
val payload = Js.Unsafe.coerce (Js.Unsafe.inject (%a \
|
|
|
|
arg))@]@\n\
|
2022-07-21 16:52:35 +03:00
|
|
|
end"
|
|
|
|
format_enum_cons_name cname format_enum_cons_name cname
|
2022-07-21 17:18:36 +03:00
|
|
|
format_typ_to_jsoo typ))
|
2022-07-18 20:19:56 +03:00
|
|
|
enum_cons
|
2022-07-21 16:52:35 +03:00
|
|
|
in
|
|
|
|
let fmt_of_jsoo fmt _ =
|
|
|
|
Format.fprintf fmt
|
|
|
|
"match %a##.kind |> Js.to_string with@\n\
|
|
|
|
%a@\n\
|
|
|
|
| cons -> failwith (Printf.sprintf \"Unexpected '%%s' kind for the \
|
|
|
|
enumeration '%a.t'\" cons)"
|
|
|
|
fmt_enum_name ()
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@\n")
|
|
|
|
(fun fmt (cname, typ) ->
|
|
|
|
match Marked.unmark typ with
|
2022-07-21 17:18:36 +03:00
|
|
|
| Dcalc.Ast.TTuple (_, None) ->
|
|
|
|
Cli.error_print
|
|
|
|
"Tuples aren't yet supported in the conversion to JS..."
|
|
|
|
| Dcalc.Ast.TLit TUnit ->
|
|
|
|
Format.fprintf fmt "| \"%a\" ->@\n%a.%a ()"
|
|
|
|
format_enum_cons_name cname fmt_module_enum_name ()
|
|
|
|
format_enum_cons_name cname
|
|
|
|
| _ ->
|
2022-07-21 16:52:35 +03:00
|
|
|
Format.fprintf fmt
|
2022-07-23 19:00:04 +03:00
|
|
|
"| \"%a\" ->@\n%a.%a (%a (Js.Unsafe.coerce %a##.payload))"
|
2022-07-21 17:18:36 +03:00
|
|
|
format_enum_cons_name cname fmt_module_enum_name ()
|
2022-07-22 20:02:09 +03:00
|
|
|
format_enum_cons_name cname format_typ_of_jsoo typ
|
2022-07-21 17:18:36 +03:00
|
|
|
fmt_enum_name ()))
|
2022-07-21 16:52:35 +03:00
|
|
|
enum_cons fmt_module_enum_name ()
|
|
|
|
in
|
|
|
|
|
|
|
|
let fmt_conv_funs fmt _ =
|
|
|
|
Format.fprintf fmt
|
|
|
|
"let %a_to_jsoo : %a.t -> %a Js.t = function@\n\
|
|
|
|
@[<hov 2>%a@]@\n\
|
|
|
|
let %a_of_jsoo (%a : %a Js.t) : %a.t = @[<hov 2>%a@]" fmt_enum_name
|
|
|
|
() fmt_module_enum_name () fmt_enum_name () fmt_to_jsoo ()
|
|
|
|
fmt_enum_name () fmt_enum_name () fmt_enum_name ()
|
|
|
|
fmt_module_enum_name () fmt_of_jsoo ()
|
|
|
|
in
|
|
|
|
Format.fprintf fmt
|
|
|
|
"class type %a =@\n\
|
|
|
|
@[<hov 2>object@ @[<hov 2>@ @ method kind : Js.js_string Js.t \
|
|
|
|
Js.readonly_prop@\n\
|
|
|
|
@[<v 2>(** Expects one of:@\n\
|
|
|
|
%a *)@\n\
|
|
|
|
@\n\
|
2022-07-21 17:18:36 +03:00
|
|
|
@]method payload : Js.Unsafe.any Js.t Js.readonly_prop@\n\
|
2022-07-21 16:52:35 +03:00
|
|
|
@]@\n\
|
|
|
|
end@]@\n\
|
|
|
|
%a@\n"
|
|
|
|
format_enum_name enum_name
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@\n")
|
2022-07-21 17:18:36 +03:00
|
|
|
(fun fmt (enum_cons, _) ->
|
2022-07-21 16:52:35 +03:00
|
|
|
Format.fprintf fmt "- \"%a\"" format_enum_cons_name enum_cons))
|
|
|
|
enum_cons fmt_conv_funs ()
|
2022-07-18 20:19:56 +03:00
|
|
|
in
|
|
|
|
let is_in_type_ordering s =
|
|
|
|
List.exists
|
|
|
|
(fun struct_or_enum ->
|
|
|
|
match struct_or_enum with
|
|
|
|
| Scopelang.Dependency.TVertex.Enum _ -> false
|
|
|
|
| Scopelang.Dependency.TVertex.Struct s' -> s = s')
|
|
|
|
type_ordering
|
|
|
|
in
|
|
|
|
let scope_structs =
|
|
|
|
List.map
|
|
|
|
(fun (s, _) -> Scopelang.Dependency.TVertex.Struct s)
|
|
|
|
(Dcalc.Ast.StructMap.bindings
|
|
|
|
(Dcalc.Ast.StructMap.filter
|
|
|
|
(fun s _ -> not (is_in_type_ordering s))
|
|
|
|
ctx.ctx_structs))
|
|
|
|
in
|
|
|
|
List.iter
|
|
|
|
(fun struct_or_enum ->
|
|
|
|
match struct_or_enum with
|
|
|
|
| Scopelang.Dependency.TVertex.Struct s ->
|
|
|
|
Format.fprintf fmt "%a@\n" format_struct_decl (s, find_struct s ctx)
|
|
|
|
| Scopelang.Dependency.TVertex.Enum e ->
|
|
|
|
Format.fprintf fmt "%a@\n" format_enum_decl (e, find_enum e ctx))
|
|
|
|
(type_ordering @ scope_structs)
|
|
|
|
|
2022-07-21 17:18:36 +03:00
|
|
|
let fmt_input_struct_name fmt (scope_def : ('a expr, 'm) D.scope_def) =
|
|
|
|
format_struct_name fmt scope_def.scope_body.scope_body_input_struct
|
|
|
|
|
|
|
|
let fmt_output_struct_name fmt (scope_def : ('a expr, 'm) D.scope_def) =
|
|
|
|
format_struct_name fmt scope_def.scope_body.scope_body_output_struct
|
|
|
|
|
|
|
|
let rec format_scopes_to_fun
|
2022-07-19 18:59:45 +03:00
|
|
|
(ctx : Dcalc.Ast.decl_ctx)
|
|
|
|
(fmt : Format.formatter)
|
2022-07-21 17:18:36 +03:00
|
|
|
(scopes : ('expr, 'm) Dcalc.Ast.scopes) =
|
2022-07-19 18:59:45 +03:00
|
|
|
match scopes with
|
|
|
|
| Dcalc.Ast.Nil -> ()
|
|
|
|
| Dcalc.Ast.ScopeDef scope_def ->
|
|
|
|
let scope_var, scope_next = Bindlib.unbind scope_def.scope_next in
|
2022-07-20 19:25:41 +03:00
|
|
|
let fmt_fun_call fmt _ =
|
|
|
|
Format.fprintf fmt "%a |> %a_of_jsoo |> %a |> %a_to_jsoo"
|
2022-07-21 17:18:36 +03:00
|
|
|
fmt_input_struct_name scope_def fmt_input_struct_name scope_def
|
|
|
|
format_var scope_var fmt_output_struct_name scope_def
|
2022-07-20 19:25:41 +03:00
|
|
|
in
|
2022-07-21 17:18:36 +03:00
|
|
|
Format.fprintf fmt "@\n@\nlet %a (%a : %a Js.t) : %a Js.t =@\n%a@\n%a"
|
|
|
|
format_var scope_var fmt_input_struct_name scope_def
|
|
|
|
fmt_input_struct_name scope_def fmt_output_struct_name scope_def
|
|
|
|
fmt_fun_call () (format_scopes_to_fun ctx) scope_next
|
|
|
|
|
|
|
|
let rec format_scopes_to_callbacks
|
|
|
|
(ctx : Dcalc.Ast.decl_ctx)
|
|
|
|
(fmt : Format.formatter)
|
|
|
|
(scopes : ('expr, 'm) Dcalc.Ast.scopes) : unit =
|
|
|
|
match scopes with
|
|
|
|
| Dcalc.Ast.Nil -> ()
|
|
|
|
| Dcalc.Ast.ScopeDef scope_def ->
|
|
|
|
let scope_var, scope_next = Bindlib.unbind scope_def.scope_next in
|
|
|
|
let fmt_meth_name fmt _ =
|
|
|
|
Format.fprintf fmt "method %a : (%a Js.t -> %a Js.t) Js.callback"
|
|
|
|
format_var_camel_case scope_var fmt_input_struct_name scope_def
|
|
|
|
fmt_output_struct_name scope_def
|
|
|
|
in
|
|
|
|
Format.fprintf fmt "@\n@\n@[<hov 2> %a =@\n Js.wrap_callback@ %a@]%a"
|
|
|
|
fmt_meth_name () format_var scope_var
|
|
|
|
(format_scopes_to_callbacks ctx)
|
|
|
|
scope_next
|
2022-07-19 18:59:45 +03:00
|
|
|
|
2022-07-18 20:19:56 +03:00
|
|
|
let format_program
|
|
|
|
(fmt : Format.formatter)
|
2022-07-29 14:39:33 +03:00
|
|
|
(module_name : string option)
|
2022-07-20 13:00:33 +03:00
|
|
|
(prgm : 'm Lcalc.Ast.program)
|
2022-07-18 20:19:56 +03:00
|
|
|
(type_ordering : Scopelang.Dependency.TVertex.t list) =
|
2022-07-21 17:18:36 +03:00
|
|
|
let fmt_lib_name fmt _ =
|
|
|
|
Format.fprintf fmt "%sLib"
|
2022-07-29 14:39:33 +03:00
|
|
|
(Option.fold ~none:""
|
|
|
|
~some:(fun name ->
|
|
|
|
List.nth (String.split_on_char ' ' name) 1
|
|
|
|
|> String.split_on_char '_'
|
|
|
|
|> List.map String.capitalize_ascii
|
|
|
|
|> String.concat "")
|
|
|
|
module_name)
|
2022-07-21 17:18:36 +03:00
|
|
|
in
|
|
|
|
|
2022-07-20 17:51:28 +03:00
|
|
|
Cli.call_unstyled (fun _ ->
|
|
|
|
Format.fprintf fmt
|
|
|
|
"(** This file has been generated by the Catala compiler, do not \
|
|
|
|
edit! *)@\n\
|
|
|
|
@\n\
|
|
|
|
open Runtime_ocaml.Runtime@\n\
|
|
|
|
open Runtime_jsoo.Runtime@\n\
|
|
|
|
open Js_of_ocaml@\n\
|
|
|
|
%s@\n\
|
|
|
|
@\n\
|
|
|
|
[@@@@@@ocaml.warning \"-4-26-27-32-41-42\"]@\n\
|
|
|
|
@\n\
|
|
|
|
(* Generated API *)\n\n\
|
|
|
|
%a@\n\
|
2022-07-21 17:18:36 +03:00
|
|
|
%a@\n\
|
2022-07-20 17:51:28 +03:00
|
|
|
@\n\n\
|
2022-07-21 17:18:36 +03:00
|
|
|
\ let _ =@ @[<hov 2> Js.export \"%a\"@\n\
|
2022-07-20 17:51:28 +03:00
|
|
|
(object%%js@ @[\n\
|
2022-07-21 17:18:36 +03:00
|
|
|
%a@]@\n\
|
|
|
|
end)@]@?"
|
2022-07-29 14:39:33 +03:00
|
|
|
(Option.fold ~none:"" ~some:(fun name -> name) module_name)
|
|
|
|
(format_ctx type_ordering) prgm.decl_ctx
|
2022-07-21 17:18:36 +03:00
|
|
|
(format_scopes_to_fun prgm.decl_ctx)
|
|
|
|
prgm.scopes fmt_lib_name ()
|
|
|
|
(format_scopes_to_callbacks prgm.decl_ctx)
|
2022-07-20 17:51:28 +03:00
|
|
|
prgm.scopes)
|
2022-07-18 20:19:56 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
let apply
|
2022-07-29 14:39:33 +03:00
|
|
|
~(source_file : Pos.input_file)
|
2022-07-26 18:27:42 +03:00
|
|
|
~(output_file : string option)
|
2022-07-29 14:39:33 +03:00
|
|
|
~scope
|
2022-07-20 13:00:33 +03:00
|
|
|
(prgm : 'm Lcalc.Ast.program)
|
2022-07-18 20:19:56 +03:00
|
|
|
(type_ordering : Scopelang.Dependency.TVertex.t list) =
|
2022-07-29 14:39:33 +03:00
|
|
|
ignore scope;
|
|
|
|
let output_file, with_formatter =
|
|
|
|
File.get_formatter_of_out_channel ~source_file ~output_file ~ext:extension
|
|
|
|
()
|
2022-07-19 19:28:24 +03:00
|
|
|
in
|
2022-07-29 14:39:33 +03:00
|
|
|
with_formatter (fun fmt ->
|
2022-07-27 17:09:37 +03:00
|
|
|
Cli.trace_flag := true;
|
2022-07-29 14:39:33 +03:00
|
|
|
Cli.debug_print "Writing OCaml code to %s..."
|
|
|
|
(Option.value ~default:"stdout" output_file);
|
2022-07-27 17:09:37 +03:00
|
|
|
To_ocaml.format_program fmt prgm type_ordering;
|
|
|
|
File.ocamlformat_file_opt output_file);
|
2022-07-29 14:39:33 +03:00
|
|
|
|
|
|
|
let filename_without_ext =
|
|
|
|
match output_file with
|
|
|
|
| Some "-" -> output_file
|
|
|
|
| Some f -> Some (Filename.basename f |> Filename.remove_extension)
|
|
|
|
| None -> None
|
2022-07-27 17:09:37 +03:00
|
|
|
in
|
2022-07-29 14:39:33 +03:00
|
|
|
let jsoo_output_file, with_formatter =
|
|
|
|
File.get_formatter_of_out_channel ~source_file
|
|
|
|
~output_file:
|
|
|
|
(Option.map
|
|
|
|
(fun name ->
|
|
|
|
if "-" = name then "-"
|
|
|
|
else Filename.remove_extension name ^ "_api_web.ml")
|
|
|
|
output_file)
|
|
|
|
~ext:"_api_web.ml" ()
|
|
|
|
in
|
|
|
|
let module_name =
|
2022-07-27 17:09:37 +03:00
|
|
|
Option.map
|
2022-07-29 14:39:33 +03:00
|
|
|
(fun name -> Printf.sprintf "open %s" (String.capitalize_ascii name))
|
|
|
|
filename_without_ext
|
2022-07-27 17:09:37 +03:00
|
|
|
in
|
2022-07-29 14:39:33 +03:00
|
|
|
Cli.log_print "module_name: %s\n"
|
|
|
|
@@ Option.fold ~none:"none" ~some:(fun s -> s) module_name;
|
|
|
|
with_formatter (fun fmt ->
|
2022-07-27 17:09:37 +03:00
|
|
|
Cli.debug_print "Writing JSOO API code to %s..."
|
2022-07-29 14:39:33 +03:00
|
|
|
(Option.value ~default:"stdout" jsoo_output_file);
|
2022-07-27 17:09:37 +03:00
|
|
|
To_jsoo.format_program fmt module_name prgm type_ordering;
|
2022-07-29 14:39:33 +03:00
|
|
|
File.ocamlformat_file_opt jsoo_output_file)
|
2022-07-18 20:19:56 +03:00
|
|
|
|
2022-07-20 15:20:21 +03:00
|
|
|
let () = Driver.Plugin.register_lcalc ~name ~extension apply
|