From 0c180e12f9d7b2eb571d6f877a0866a7e283f3dc Mon Sep 17 00:00:00 2001 From: Emile Rolley Date: Tue, 19 Jul 2022 19:17:02 +0200 Subject: [PATCH] refactor(runtimes): add runtime_ocaml and runtime_jsoo in the catala package --- build_system/dune | 2 +- compiler/dcalc/ast.ml | 1 + compiler/dcalc/ast.mli | 1 + compiler/dcalc/dune | 2 +- compiler/dcalc/interpreter.ml | 1 + compiler/dune | 8 +++-- compiler/index.mld | 4 +-- compiler/lcalc/ast.ml | 1 + compiler/lcalc/ast.mli | 1 + compiler/lcalc/dune | 2 +- compiler/scalc/dune | 2 +- compiler/scalc/to_python.ml | 1 + compiler/surface/desugaring.ml | 2 ++ compiler/verification/dune | 2 +- runtimes/dune | 4 +++ runtimes/jsoo/dune | 5 ++++ runtimes/jsoo/runtime.ml | 50 ++++++++++++++++++++++++++++++++ runtimes/jsoo/runtime.mli | 53 ++++++++++++++++++++++++++++++++++ runtimes/ocaml/dune | 4 +-- runtimes/ocaml/runtime.mli | 4 +++ 20 files changed, 139 insertions(+), 11 deletions(-) create mode 100644 runtimes/dune create mode 100644 runtimes/jsoo/dune create mode 100644 runtimes/jsoo/runtime.ml create mode 100644 runtimes/jsoo/runtime.mli diff --git a/build_system/dune b/build_system/dune index fafeb9ce..515077bd 100644 --- a/build_system/dune +++ b/build_system/dune @@ -7,7 +7,7 @@ (library (name clerk_driver) (public_name clerk.driver) - (libraries catala.runtime catala.utils ninja_utils cmdliner re ANSITerminal) + (libraries catala.runtime_ocaml catala.utils ninja_utils cmdliner re ANSITerminal) (modules clerk_driver)) (executable diff --git a/compiler/dcalc/ast.ml b/compiler/dcalc/ast.ml index e08cff97..0d9ce50d 100644 --- a/compiler/dcalc/ast.ml +++ b/compiler/dcalc/ast.ml @@ -18,6 +18,7 @@ [@@@ocaml.warning "-7-34"] open Utils +module Runtime = Runtime_ocaml.Runtime module ScopeName : Uid.Id with type info = Uid.MarkedString.info = Uid.Make (Uid.MarkedString) () diff --git a/compiler/dcalc/ast.mli b/compiler/dcalc/ast.mli index 73ab8ca8..267ed1ad 100644 --- a/compiler/dcalc/ast.mli +++ b/compiler/dcalc/ast.mli @@ -18,6 +18,7 @@ (** Abstract syntax tree of the default calculus intermediate representation *) open Utils +module Runtime = Runtime_ocaml.Runtime module ScopeName : Uid.Id with type info = Uid.MarkedString.info module StructName : Uid.Id with type info = Uid.MarkedString.info module StructFieldName : Uid.Id with type info = Uid.MarkedString.info diff --git a/compiler/dcalc/dune b/compiler/dcalc/dune index e450c570..17c24913 100644 --- a/compiler/dcalc/dune +++ b/compiler/dcalc/dune @@ -1,7 +1,7 @@ (library (name dcalc) (public_name catala.dcalc) - (libraries bindlib unionFind utils re camomile runtime) + (libraries bindlib unionFind utils re camomile catala.runtime_ocaml) (preprocess (pps visitors.ppx))) diff --git a/compiler/dcalc/interpreter.ml b/compiler/dcalc/interpreter.ml index 1dfeaac8..bdaac0dd 100644 --- a/compiler/dcalc/interpreter.ml +++ b/compiler/dcalc/interpreter.ml @@ -18,6 +18,7 @@ open Utils module A = Ast +module Runtime = Runtime_ocaml.Runtime (** {1 Helpers} *) diff --git a/compiler/dune b/compiler/dune index 351a0420..ee0b9d50 100644 --- a/compiler/dune +++ b/compiler/dune @@ -10,7 +10,7 @@ dcalc lcalc scalc - runtime + catala.runtime_ocaml verification) (modules plugin driver)) @@ -22,7 +22,11 @@ (modules catala_web_interpreter) (preprocess (pps js_of_ocaml-ppx)) - (libraries catala.driver js_of_ocaml)) + (libraries + catala.driver + js_of_ocaml + catala.runtime_ocaml + catala.runtime_jsoo)) (executable (name catala) diff --git a/compiler/index.mld b/compiler/index.mld index 8c62462b..c84b5a89 100644 --- a/compiler/index.mld +++ b/compiler/index.mld @@ -106,9 +106,9 @@ Two more modules contain additional features for the compiler: {li {{: utils.html} Compiler utilities}} } -The Catala runtime documentation is available here: +The Catala runtimes documentation is available here: -{!modules: Runtime} +{!modules: Runtime_ocaml.Runtime Runtime_jsoo.Runtime} Last, it is possible to customize the backend to the compiler using a plugin mechanism. The API is defined inside the following module: diff --git a/compiler/lcalc/ast.ml b/compiler/lcalc/ast.ml index 42673a98..379a70f0 100644 --- a/compiler/lcalc/ast.ml +++ b/compiler/lcalc/ast.ml @@ -15,6 +15,7 @@ the License. *) open Utils +module Runtime = Runtime_ocaml.Runtime module D = Dcalc.Ast type lit = diff --git a/compiler/lcalc/ast.mli b/compiler/lcalc/ast.mli index a795ff70..d3442db8 100644 --- a/compiler/lcalc/ast.mli +++ b/compiler/lcalc/ast.mli @@ -15,6 +15,7 @@ the License. *) open Utils +module Runtime = Runtime_ocaml.Runtime (** Abstract syntax tree for the lambda calculus *) diff --git a/compiler/lcalc/dune b/compiler/lcalc/dune index 7fa1bcb3..ab4ad13b 100644 --- a/compiler/lcalc/dune +++ b/compiler/lcalc/dune @@ -1,7 +1,7 @@ (library (name lcalc) (public_name catala.lcalc) - (libraries bindlib dcalc scopelang runtime) + (libraries bindlib dcalc scopelang catala.runtime_ocaml) (preprocess (pps visitors.ppx))) diff --git a/compiler/scalc/dune b/compiler/scalc/dune index 75f842e1..2f0a9449 100644 --- a/compiler/scalc/dune +++ b/compiler/scalc/dune @@ -1,7 +1,7 @@ (library (name scalc) (public_name catala.scalc) - (libraries bindlib lcalc runtime)) + (libraries bindlib lcalc catala.runtime_ocaml)) (documentation (package catala) diff --git a/compiler/scalc/to_python.ml b/compiler/scalc/to_python.ml index 0dc7e996..2c5ed555 100644 --- a/compiler/scalc/to_python.ml +++ b/compiler/scalc/to_python.ml @@ -18,6 +18,7 @@ open Utils open Ast open Lcalc.Backends +module Runtime = Runtime_ocaml.Runtime module D = Dcalc.Ast module L = Lcalc.Ast diff --git a/compiler/surface/desugaring.ml b/compiler/surface/desugaring.ml index 601fe717..efdb1d09 100644 --- a/compiler/surface/desugaring.ml +++ b/compiler/surface/desugaring.ml @@ -16,6 +16,8 @@ the License. *) open Utils +module Runtime = Runtime_ocaml.Runtime + (** Translation from {!module: Surface.Ast} to {!module: Desugaring.Ast}. - Removes syntactic sugars diff --git a/compiler/verification/dune b/compiler/verification/dune index 0584a6c2..0b4e10cd 100644 --- a/compiler/verification/dune +++ b/compiler/verification/dune @@ -5,7 +5,7 @@ bindlib utils dcalc - runtime + catala.runtime_ocaml calendar (select z3backend.ml diff --git a/runtimes/dune b/runtimes/dune new file mode 100644 index 00000000..cf7745a1 --- /dev/null +++ b/runtimes/dune @@ -0,0 +1,4 @@ +(dirs jsoo ocaml) + +(documentation + (package catala)) diff --git a/runtimes/jsoo/dune b/runtimes/jsoo/dune new file mode 100644 index 00000000..8fa72b53 --- /dev/null +++ b/runtimes/jsoo/dune @@ -0,0 +1,5 @@ +(library + (name runtime_jsoo) + (public_name catala.runtime_jsoo) + (libraries catala.runtime_ocaml js_of_ocaml) + (modules runtime)) diff --git a/runtimes/jsoo/runtime.ml b/runtimes/jsoo/runtime.ml new file mode 100644 index 00000000..64a63c51 --- /dev/null +++ b/runtimes/jsoo/runtime.ml @@ -0,0 +1,50 @@ +(* This file is part of the Catala compiler, a specification language for tax + and social benefits computation rules. Copyright (C) 2020 Inria, contributor: + Emile Rolley . + + 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. *) + +open Js_of_ocaml + +class type source_position = + object + method fileName : Js.js_string Js.t Js.prop + method startLine : int Js.prop + method endLine : int Js.prop + method startColumn : int Js.prop + method endColumn : int Js.prop + method lawHeadings : Js.js_string Js.t Js.js_array Js.t Js.prop + end + +class type raw_event = + object + method eventType : Js.js_string Js.t Js.prop + method information : Js.js_string Js.t Js.js_array Js.t Js.prop + method sourcePosition : source_position Js.t Js.optdef Js.prop + method loggedValueJson : Js.js_string Js.t Js.prop + end + +class type event = + object + method data : Js.js_string Js.t Js.prop + end + +class type duration = + object + method years : int Js.prop + method months : int Js.prop + method days : int Js.prop + end + +(* let duration_of_jsoo d = *) +(* Runtime.duration_of_numbers d##.years d##.months d##.days *) diff --git a/runtimes/jsoo/runtime.mli b/runtimes/jsoo/runtime.mli new file mode 100644 index 00000000..7bc88e8e --- /dev/null +++ b/runtimes/jsoo/runtime.mli @@ -0,0 +1,53 @@ +(* This file is part of the Catala compiler, a specification language for tax + and social benefits computation rules. Copyright (C) 2020 Inria, contributor: + Emile Rolley . + + 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. *) + +(** Runtime used by generated web API via + {{:http://ocsigen.org/js_of_ocaml/latest/manual/overview} [js_of_ocaml]}. *) + +open Js_of_ocaml + +class type source_position = + object + method fileName : Js.js_string Js.t Js.prop + method startLine : int Js.prop + method endLine : int Js.prop + method startColumn : int Js.prop + method endColumn : int Js.prop + method lawHeadings : Js.js_string Js.t Js.js_array Js.t Js.prop + end + +class type raw_event = + object + method eventType : Js.js_string Js.t Js.prop + method information : Js.js_string Js.t Js.js_array Js.t Js.prop + method sourcePosition : source_position Js.t Js.optdef Js.prop + method loggedValueJson : Js.js_string Js.t Js.prop + end + +class type event = + object + method data : Js.js_string Js.t Js.prop + end + +class type duration = + object + method years : int Js.prop + method months : int Js.prop + method days : int Js.prop + end + +(* let duration_of_jsoo d = *) +(* Runtime.duration_of_numbers d##.years d##.months d##.days *) diff --git a/runtimes/ocaml/dune b/runtimes/ocaml/dune index 066e97cd..12536bc0 100644 --- a/runtimes/ocaml/dune +++ b/runtimes/ocaml/dune @@ -1,6 +1,6 @@ (library - (name runtime) - (public_name catala.runtime) + (name runtime_ocaml) + (public_name catala.runtime_ocaml) (preprocess (pps ppx_yojson_conv)) (libraries calendar zarith zarith_stubs_js) diff --git a/runtimes/ocaml/runtime.mli b/runtimes/ocaml/runtime.mli index e6e85a74..e6957483 100644 --- a/runtimes/ocaml/runtime.mli +++ b/runtimes/ocaml/runtime.mli @@ -248,7 +248,11 @@ val date_of_numbers : int -> int -> int -> date (**{2 Durations} *) val duration_of_numbers : int -> int -> int -> duration +(** Usage : [duration_of_numbers year mounth day]. *) + val duration_to_years_months_days : duration -> int * int * int +(**{2 Times} *) + val duration_to_string : duration -> string (**{1 Defaults} *)