From 4d412027d0f08252ed4d3863657484daba92be72 Mon Sep 17 00:00:00 2001 From: Louis Gesbert Date: Thu, 2 May 2024 15:57:19 +0200 Subject: [PATCH] Remove direct dependency towards dates_calc from the compiler The dependency should only go through the `Runtime` module --- compiler/desugared/from_surface.ml | 2 +- compiler/shared_ast/print.ml | 4 ++-- compiler/surface/dune | 1 - runtimes/ocaml/runtime.ml | 20 ++++++-------------- runtimes/ocaml/runtime.mli | 5 ++++- 5 files changed, 13 insertions(+), 19 deletions(-) diff --git a/compiler/desugared/from_surface.ml b/compiler/desugared/from_surface.ml index fef2d146..d9af0fc5 100644 --- a/compiler/desugared/from_surface.ml +++ b/compiler/desugared/from_surface.ml @@ -375,7 +375,7 @@ let rec translate_expr (try Runtime.date_of_numbers date.literal_date_year date.literal_date_month date.literal_date_day - with Dates_calc.Dates.InvalidDate -> + with Failure _ -> Message.error ~pos "There is an error in this date, it does not correspond to a \ correct calendar day") diff --git a/compiler/shared_ast/print.ml b/compiler/shared_ast/print.ml index 513d4910..e391a22a 100644 --- a/compiler/shared_ast/print.ml +++ b/compiler/shared_ast/print.ml @@ -1051,13 +1051,13 @@ module UserFacing = struct and some others not, adding confusion. *) let date (lang : Global.backend_lang) ppf d = - let y, m, d = Dates_calc.Dates.date_to_ymd d in + let y, m, d = Runtime.date_to_years_months_days d in match lang with | En | Pl -> Format.fprintf ppf "%04d-%02d-%02d" y m d | Fr -> Format.fprintf ppf "%02d/%02d/%04d" d m y let duration (lang : Global.backend_lang) ppf dr = - let y, m, d = Dates_calc.Dates.period_to_ymds dr in + let y, m, d = Runtime.duration_to_years_months_days dr in let rec filter0 = function | (0, _) :: (_ :: _ as r) -> filter0 r | x :: r -> x :: List.filter (fun (n, _) -> n <> 0) r diff --git a/compiler/surface/dune b/compiler/surface/dune index ed6a89d9..67e1a3a5 100644 --- a/compiler/surface/dune +++ b/compiler/surface/dune @@ -8,7 +8,6 @@ re zarith zarith_stubs_js - dates_calc shared_ast) (preprocess (pps sedlex.ppx visitors.ppx))) diff --git a/runtimes/ocaml/runtime.ml b/runtimes/ocaml/runtime.ml index d020b18d..e7e90b22 100644 --- a/runtimes/ocaml/runtime.ml +++ b/runtimes/ocaml/runtime.ml @@ -206,11 +206,16 @@ let day_of_month_of_date (d : date) : integer = (* This could fail, but is expected to only be called with known, already validated arguments by the generated code *) let date_of_numbers (year : int) (month : int) (day : int) : date = - Dates_calc.Dates.make_date ~year ~month ~day + try Dates_calc.Dates.make_date ~year ~month ~day + with Dates_calc.Dates.InvalidDate -> + failwith "date_of_numbers: invalid date" let date_to_string (d : date) : string = Format.asprintf "%a" Dates_calc.Dates.format_date d +let date_to_years_months_days (d : date) : int * int * int = + Dates_calc.Dates.date_to_ymd d + let first_day_of_month = Dates_calc.Dates.first_day_of_month let last_day_of_month = Dates_calc.Dates.last_day_of_month @@ -219,19 +224,6 @@ let duration_of_numbers (year : int) (month : int) (day : int) : duration = let duration_to_string (d : duration) : string = Format.asprintf "%a" Dates_calc.Dates.format_period d -(* breaks previous format *) -(* let x, y, z = CalendarLib.Date.Period.ymd d in - * let to_print = - * List.filter (fun (a, _) -> a <> 0) [x, "years"; y, "months"; z, "days"] - * in - * match to_print with - * | [] -> "empty duration" - * | _ -> - * Format.asprintf "%a" - * (Format.pp_print_list - * ~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ") - * (fun fmt (d, l) -> Format.fprintf fmt "%d %s" d l)) - * to_print *) let duration_to_years_months_days (d : duration) : int * int * int = Dates_calc.Dates.period_to_ymds d diff --git a/runtimes/ocaml/runtime.mli b/runtimes/ocaml/runtime.mli index b17c8782..9b9124b5 100644 --- a/runtimes/ocaml/runtime.mli +++ b/runtimes/ocaml/runtime.mli @@ -315,10 +315,12 @@ val year_of_date : date -> integer val date_to_string : date -> string val date_of_numbers : int -> int -> int -> date -(** Usage: [date_of_numbers year month day] *) +(** Usage: [date_of_numbers year month day]. + @raise Failure on invalid inputs *) val first_day_of_month : date -> date val last_day_of_month : date -> date +val date_to_years_months_days : date -> int * int * int (**{2 Durations} *) @@ -326,6 +328,7 @@ 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