aboutsummaryrefslogtreecommitdiff
path: root/scTypes.ml
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@chimrod.com>2016-11-21 17:06:19 +0100
committerSébastien Dailly <sebastien@chimrod.com>2017-01-01 13:30:43 +0100
commit023c11470e32744a43b7e3c7c248f3c47ebdc687 (patch)
tree832e04c2923295d5adf61e58d9a333afb5b26c77 /scTypes.ml
parentef312564ca84a2b49fc291434d8fb2f8501bb618 (diff)
Use gadt for function catalog
Diffstat (limited to 'scTypes.ml')
-rwxr-xr-xscTypes.ml78
1 files changed, 50 insertions, 28 deletions
diff --git a/scTypes.ml b/scTypes.ml
index ddbae12..6ea9f35 100755
--- a/scTypes.ml
+++ b/scTypes.ml
@@ -1,7 +1,5 @@
(** All the types used in the spreadsheet. *)
-module Calendar = CalendarLib.Calendar.Precise
-
let u = UTF8.from_utf8string
exception Error
@@ -11,13 +9,12 @@ type cell = Cell.t
type ident = UTF8.t
type types =
- | Num : Num.num * (UTF8.t option) -> types (** A number *)
- | Str : UTF8.t -> types (** A string *)
- | Date : Num.num -> types (** A date in julian day *)
+ | Num of Num.num * (UTF8.t option) (** A number *)
+ | Str of UTF8.t (** A string *)
+ | Date of Num.num (** A date in julian day *)
- | Undefined : types (** The content is not defined *)
- | Bool : bool -> types (** A boolean *)
- | List : types list -> types (** List with heterogenous datas *)
+ | Undefined (** The content is not defined *)
+ | Bool of bool (** A boolean *)
type refs =
| Cell of cell (** A cell *)
@@ -70,37 +67,55 @@ module Type = struct
end
| Str x -> UTF8.Buffer.add_string buffer x
| Bool b -> UTF8.Printf.bprintf buffer "%B" b
- | List l ->
- UTF8.Printf.bprintf buffer "[%a]"
- (show_list show) l
| Date n ->
- Num.float_of_num n
- |> Calendar.from_jd
- |> CalendarLib.Printer.Precise_Calendar.to_string
- |> u
- |> UTF8.Buffer.add_string buffer
+ let y, m, d = Tools.Date.date_from_julian_day n in
+ UTF8.Printf.bprintf buffer "%d/%d/%d" y m d
end
end
module Refs = struct
+ type 'a range =
+ | Single of 'a
+ | Array1 of 'a list
+ | Array2 of 'a list list
+
let collect = function
- | Cell x -> [Pervasives.fst x]
- | Range (first, snd) ->
- let (x1, y1) = Pervasives.fst first
+ | Cell x -> Single (Pervasives.fst x)
+ | Range (fst, snd) ->
+ let (x1, y1) = Pervasives.fst fst
and (x2, y2) = Pervasives.fst snd in
let min_x = min x1 x2
and max_x = max x1 x2
and min_y = min y1 y2
and max_y = max y1 y2 in
- let elms = ref [] in
- for x = min_x to max_x do
- for y = min_y to max_y do
- elms := (x, y)::!elms
- done
- done;
- List.rev (!elms)
+ if (min_x = max_x) || (min_y = max_y) then (
+ (* There is only a one dimension array *)
+ let elms = ref [] in
+ for x = min_x to max_x do
+ for y = min_y to max_y do
+ elms := (x, y)::!elms
+ done
+ done;
+ Array1 (!elms)
+ ) else (
+ (* This a two-dimension array *)
+ let elmx = ref [] in
+ for x = min_x to max_x do
+ let elmy = ref [] in
+ for y = min_y to max_y do
+ elmy := (x, y)::!elmy
+ done;
+ elmx := !elmy::!elmx
+ done;
+ Array2 (!elmx)
+ )
+
+ let map f = function
+ | Single coord -> Single (f coord)
+ | Array1 values -> Array1 (List.map f values)
+ | Array2 values -> Array2 (List.map (List.map f) values)
let shift (vector_x, vector_y) ref =
let _shift ((x, y), (fixed_x, fixed_y)) =
@@ -125,9 +140,16 @@ module Result = struct
| Result v1, Result v2 -> Type.(=) v1 v2
| _, _ -> t1 = t2
-
let show = begin function
- | Error _ -> u"#Error"
+ | Error x ->
+ (*
+ let buffer = Buffer.create 16 in
+ let b = Format.formatter_of_buffer buffer in
+ Errors.printf b x;
+ Format.pp_print_flush b ();
+ u(Buffer.contents buffer)
+ *)
+ u"#Error"
| Result v ->
let buffer = UTF8.Buffer.create 16 in
Type.show buffer v;