Skip to content

Commit

Permalink
Add Belt.Int & Belt.Float
Browse files Browse the repository at this point in the history
  • Loading branch information
bloodyowl committed Mar 24, 2019
1 parent d6e7f8b commit f40c765
Show file tree
Hide file tree
Showing 13 changed files with 481 additions and 1 deletion.
16 changes: 16 additions & 0 deletions jscomp/others/belt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,22 @@ module Option = Belt_Option

module Result = Belt_Result

(** {!Belt.Int}
Utilities for Int.
*)

module Int = Belt_Int


(** {!Belt.Float}
Utilities for Float.
*)

module Float = Belt_Float


(** {!Belt.Debug}
Utilities for set up debugging
Expand Down
34 changes: 34 additions & 0 deletions jscomp/others/belt_Float.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(***********************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. All rights reserved. This file is distributed *)
(* under the terms of the GNU Library General Public License, with *)
(* the special exception on linking described in file ../LICENSE. *)
(* *)
(***********************************************************************)
(* Adapted significantly by Authors of BuckleScript *)


(** {!Belt.Float}
Utililites for Float
*)

external toInt: float -> int = "%intoffloat"

external fromInt: int -> float = "%identity"

external fromString: string -> float = "parseFloat" [@@bs.val]

external toString: float -> string = "String" [@@bs.val]

let (+) = (+.)

let (-) = (-.)

let (/) = (/.)

let ( * ) = ( *. )
34 changes: 34 additions & 0 deletions jscomp/others/belt_Float.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(***********************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. All rights reserved. This file is distributed *)
(* under the terms of the GNU Library General Public License, with *)
(* the special exception on linking described in file ../LICENSE. *)
(* *)
(***********************************************************************)
(* Adapted significantly by Authors of BuckleScript *)


(** {!Belt.Float}
Utililites for Float
*)

external toInt: float -> int = "%intoffloat"

external fromInt: int -> float = "%identity"

external fromString: string -> float = "parseFloat" [@@bs.val]

external toString: float -> string = "String" [@@bs.val]

val (+): float -> float -> float

val (-): float -> float -> float

val (/): float -> float -> float

val ( * ): float -> float -> float
34 changes: 34 additions & 0 deletions jscomp/others/belt_Int.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(***********************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. All rights reserved. This file is distributed *)
(* under the terms of the GNU Library General Public License, with *)
(* the special exception on linking described in file ../LICENSE. *)
(* *)
(***********************************************************************)
(* Adapted significantly by Authors of BuckleScript *)


(** {!Belt.Int}
Utililites for Int
*)

external toFloat: int -> float = "%identity"

external fromFloat: float -> int = "%intoffloat"

external fromString: string -> (_ [@bs.as 10]) -> int = "parseInt" [@@bs.val]

external toString: int -> string = "String" [@@bs.val]

let (+) = (+)

let (-) = (-)

let (/) = (/)

let ( * ) = ( * )
34 changes: 34 additions & 0 deletions jscomp/others/belt_Int.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(***********************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. All rights reserved. This file is distributed *)
(* under the terms of the GNU Library General Public License, with *)
(* the special exception on linking described in file ../LICENSE. *)
(* *)
(***********************************************************************)
(* Adapted significantly by Authors of BuckleScript *)


(** {!Belt.Int}
Utililites for Int
*)

external toFloat: int -> float = "%identity"

external fromFloat: float -> int = "%intoffloat"

external fromString: string -> (_ [@bs.as 10]) -> int = "parseInt" [@@bs.val]

external toString: int -> string = "String" [@@bs.val]

val (+): int -> int -> int

val (-): int -> int -> int

val (/): int -> int -> int

val ( * ): int -> int -> int
6 changes: 5 additions & 1 deletion jscomp/others/release.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ build js_pkg : phony others/js_array.cmi others/js_array.cmj others/js_array2.cm
build others/belt_Array.cmj : cc others/belt_Array.ml | others/belt.cmi others/belt_Array.cmi others/js_math.cmj js_pkg runtime
build others/belt_Array.cmi : cc others/belt_Array.mli | runtime
build others/belt_Debug.cmi others/belt_Debug.cmj : cc others/belt_Debug.ml | runtime
build others/belt_Float.cmj : cc others/belt_Float.ml | others/belt.cmi others/belt_Float.cmi js_pkg runtime
build others/belt_Float.cmi : cc others/belt_Float.mli | runtime
build others/belt_HashMap.cmj : cc others/belt_HashMap.ml | others/belt.cmi others/belt_Array.cmj others/belt_HashMap.cmi others/belt_HashMapInt.cmj others/belt_HashMapString.cmj others/belt_Id.cmj others/belt_internalBuckets.cmj others/belt_internalBucketsType.cmj js_pkg runtime
build others/belt_HashMap.cmi : cc others/belt_HashMap.mli | others/belt.cmi others/belt_HashMapInt.cmi others/belt_HashMapString.cmi others/belt_Id.cmi js_pkg runtime
build others/belt_HashMapInt.cmj : cc others/belt_HashMapInt.ml | others/belt.cmi others/belt_Array.cmj others/belt_HashMapInt.cmi others/belt_internalBuckets.cmj others/belt_internalBucketsType.cmj js_pkg runtime
Expand All @@ -68,6 +70,8 @@ build others/belt_HashSetString.cmj : cc others/belt_HashSetString.ml | others/b
build others/belt_HashSetString.cmi : cc others/belt_HashSetString.mli | runtime
build others/belt_Id.cmj : cc others/belt_Id.ml | others/belt.cmi others/belt_Id.cmi js_pkg runtime
build others/belt_Id.cmi : cc others/belt_Id.mli | runtime
build others/belt_Int.cmj : cc others/belt_Int.ml | others/belt.cmi others/belt_Int.cmi js_pkg runtime
build others/belt_Int.cmi : cc others/belt_Int.mli | runtime
build others/belt_List.cmj : cc others/belt_List.ml | others/belt.cmi others/belt_Array.cmj others/belt_List.cmi others/belt_SortArray.cmj js_pkg runtime
build others/belt_List.cmi : cc others/belt_List.mli | runtime
build others/belt_Map.cmj : cc others/belt_Map.ml | others/belt.cmi others/belt_Array.cmj others/belt_Id.cmj others/belt_Map.cmi others/belt_MapDict.cmj others/belt_MapInt.cmj others/belt_MapString.cmj js_pkg runtime
Expand Down Expand Up @@ -138,4 +142,4 @@ build others/node_module.cmi others/node_module.cmj : cc others/node_module.ml |
build others/node_path.cmi others/node_path.cmj : cc others/node_path.ml | runtime
build others/node_process.cmj : cc others/node_process.ml | others/js_dict.cmj others/node.cmi others/node_process.cmi js_pkg runtime
build others/node_process.cmi : cc others/node_process.mli | others/js_dict.cmi others/node.cmi js_pkg runtime
build others : phony others/belt_Array.cmi others/belt_Array.cmj others/belt_Debug.cmi others/belt_Debug.cmj others/belt_HashMap.cmi others/belt_HashMap.cmj others/belt_HashMapInt.cmi others/belt_HashMapInt.cmj others/belt_HashMapString.cmi others/belt_HashMapString.cmj others/belt_HashSet.cmi others/belt_HashSet.cmj others/belt_HashSetInt.cmi others/belt_HashSetInt.cmj others/belt_HashSetString.cmi others/belt_HashSetString.cmj others/belt_Id.cmi others/belt_Id.cmj others/belt_List.cmi others/belt_List.cmj others/belt_Map.cmi others/belt_Map.cmj others/belt_MapDict.cmi others/belt_MapDict.cmj others/belt_MapInt.cmi others/belt_MapInt.cmj others/belt_MapString.cmi others/belt_MapString.cmj others/belt_MutableMap.cmi others/belt_MutableMap.cmj others/belt_MutableMapInt.cmi others/belt_MutableMapInt.cmj others/belt_MutableMapString.cmi others/belt_MutableMapString.cmj others/belt_MutableQueue.cmi others/belt_MutableQueue.cmj others/belt_MutableSet.cmi others/belt_MutableSet.cmj others/belt_MutableSetInt.cmi others/belt_MutableSetInt.cmj others/belt_MutableSetString.cmi others/belt_MutableSetString.cmj others/belt_MutableStack.cmi others/belt_MutableStack.cmj others/belt_Option.cmi others/belt_Option.cmj others/belt_Range.cmi others/belt_Range.cmj others/belt_Result.cmi others/belt_Result.cmj others/belt_Set.cmi others/belt_Set.cmj others/belt_SetDict.cmi others/belt_SetDict.cmj others/belt_SetInt.cmi others/belt_SetInt.cmj others/belt_SetString.cmi others/belt_SetString.cmj others/belt_SortArray.cmi others/belt_SortArray.cmj others/belt_SortArrayInt.cmi others/belt_SortArrayInt.cmj others/belt_SortArrayString.cmi others/belt_SortArrayString.cmj others/belt_internalAVLset.cmi others/belt_internalAVLset.cmj others/belt_internalAVLtree.cmi others/belt_internalAVLtree.cmj others/belt_internalBuckets.cmi others/belt_internalBuckets.cmj others/belt_internalBucketsType.cmi others/belt_internalBucketsType.cmj others/belt_internalMapInt.cmi others/belt_internalMapInt.cmj others/belt_internalMapString.cmi others/belt_internalMapString.cmj others/belt_internalSetBuckets.cmi others/belt_internalSetBuckets.cmj others/belt_internalSetInt.cmi others/belt_internalSetInt.cmj others/belt_internalSetString.cmi others/belt_internalSetString.cmj others/dom.cmi others/dom_storage.cmi others/dom_storage2.cmi others/node_buffer.cmi others/node_buffer.cmj others/node_child_process.cmi others/node_child_process.cmj others/node_fs.cmi others/node_fs.cmj others/node_module.cmi others/node_module.cmj others/node_path.cmi others/node_path.cmj others/node_process.cmi others/node_process.cmj
build others : phony others/belt_Array.cmi others/belt_Array.cmj others/belt_Debug.cmi others/belt_Debug.cmj others/belt_Float.cmi others/belt_Float.cmj others/belt_HashMap.cmi others/belt_HashMap.cmj others/belt_HashMapInt.cmi others/belt_HashMapInt.cmj others/belt_HashMapString.cmi others/belt_HashMapString.cmj others/belt_HashSet.cmi others/belt_HashSet.cmj others/belt_HashSetInt.cmi others/belt_HashSetInt.cmj others/belt_HashSetString.cmi others/belt_HashSetString.cmj others/belt_Id.cmi others/belt_Id.cmj others/belt_Int.cmi others/belt_Int.cmj others/belt_List.cmi others/belt_List.cmj others/belt_Map.cmi others/belt_Map.cmj others/belt_MapDict.cmi others/belt_MapDict.cmj others/belt_MapInt.cmi others/belt_MapInt.cmj others/belt_MapString.cmi others/belt_MapString.cmj others/belt_MutableMap.cmi others/belt_MutableMap.cmj others/belt_MutableMapInt.cmi others/belt_MutableMapInt.cmj others/belt_MutableMapString.cmi others/belt_MutableMapString.cmj others/belt_MutableQueue.cmi others/belt_MutableQueue.cmj others/belt_MutableSet.cmi others/belt_MutableSet.cmj others/belt_MutableSetInt.cmi others/belt_MutableSetInt.cmj others/belt_MutableSetString.cmi others/belt_MutableSetString.cmj others/belt_MutableStack.cmi others/belt_MutableStack.cmj others/belt_Option.cmi others/belt_Option.cmj others/belt_Range.cmi others/belt_Range.cmj others/belt_Result.cmi others/belt_Result.cmj others/belt_Set.cmi others/belt_Set.cmj others/belt_SetDict.cmi others/belt_SetDict.cmj others/belt_SetInt.cmi others/belt_SetInt.cmj others/belt_SetString.cmi others/belt_SetString.cmj others/belt_SortArray.cmi others/belt_SortArray.cmj others/belt_SortArrayInt.cmi others/belt_SortArrayInt.cmj others/belt_SortArrayString.cmi others/belt_SortArrayString.cmj others/belt_internalAVLset.cmi others/belt_internalAVLset.cmj others/belt_internalAVLtree.cmi others/belt_internalAVLtree.cmj others/belt_internalBuckets.cmi others/belt_internalBuckets.cmj others/belt_internalBucketsType.cmi others/belt_internalBucketsType.cmj others/belt_internalMapInt.cmi others/belt_internalMapInt.cmj others/belt_internalMapString.cmi others/belt_internalMapString.cmj others/belt_internalSetBuckets.cmi others/belt_internalSetBuckets.cmj others/belt_internalSetInt.cmi others/belt_internalSetInt.cmj others/belt_internalSetString.cmi others/belt_internalSetString.cmj others/dom.cmi others/dom_storage.cmi others/dom_storage2.cmi others/node_buffer.cmi others/node_buffer.cmj others/node_child_process.cmi others/node_child_process.cmj others/node_fs.cmi others/node_fs.cmj others/node_module.cmi others/node_module.cmj others/node_path.cmi others/node_path.cmj others/node_process.cmi others/node_process.cmj
92 changes: 92 additions & 0 deletions jscomp/test/bs_float_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'use strict';

var Mt = require("./mt.js");
var Block = require("../../lib/js/block.js");

var suites = /* record */[/* contents : [] */0];

var test_id = /* record */[/* contents */0];

function eq(loc, x, y) {
return Mt.eq_suites(test_id, suites, loc, x, y);
}

function b(loc, x) {
return Mt.bool_suites(test_id, suites, loc, x);
}

function $$throw(loc, x) {
return Mt.throw_suites(test_id, suites, loc, x);
}

function neq(loc, x, y) {
test_id[0] = test_id[0] + 1 | 0;
suites[0] = /* :: */[
/* tuple */[
loc + (" id " + String(test_id[0])),
(function (param) {
return /* Neq */Block.__(1, [
x,
y
]);
})
],
suites[0]
];
return /* () */0;
}

eq("File \"bs_float_test.ml\", line 14, characters 5-12", 1, 1.0);

eq("File \"bs_float_test.ml\", line 15, characters 5-12", -1, -1.0);

eq("File \"bs_float_test.ml\", line 18, characters 5-12", 1, 1);

eq("File \"bs_float_test.ml\", line 19, characters 5-12", 1, 1);

eq("File \"bs_float_test.ml\", line 20, characters 5-12", 1, 1);

eq("File \"bs_float_test.ml\", line 21, characters 5-12", -1, -1);

eq("File \"bs_float_test.ml\", line 22, characters 5-12", -1, -1);

eq("File \"bs_float_test.ml\", line 23, characters 5-12", -1, -1);

eq("File \"bs_float_test.ml\", line 26, characters 5-12", parseFloat("1"), 1.0);

eq("File \"bs_float_test.ml\", line 27, characters 5-12", parseFloat("-1"), -1.0);

eq("File \"bs_float_test.ml\", line 28, characters 5-12", parseFloat("1.7"), 1.7);

eq("File \"bs_float_test.ml\", line 29, characters 5-12", parseFloat("-1.0"), -1.0);

eq("File \"bs_float_test.ml\", line 30, characters 5-12", parseFloat("-1.5"), -1.5);

eq("File \"bs_float_test.ml\", line 31, characters 5-12", parseFloat("-1.7"), -1.0);

eq("File \"bs_float_test.ml\", line 34, characters 5-12", String(1.0), "1");

eq("File \"bs_float_test.ml\", line 35, characters 5-12", String(-1.0), "-1");

eq("File \"bs_float_test.ml\", line 36, characters 5-12", String(-1.5), "-1.5");

eq("File \"bs_float_test.ml\", line 40, characters 5-12", 2.0 + 3.0, 5.0);

eq("File \"bs_float_test.ml\", line 41, characters 5-12", 2.0 - 3.0, -1.0);

eq("File \"bs_float_test.ml\", line 42, characters 5-12", 2.0 * 3.0, 6.0);

eq("File \"bs_float_test.ml\", line 43, characters 5-12", 3.0 / 2.0, 1.5);

Mt.from_pair_suites("File \"bs_float_test.ml\", line 45, characters 23-30", suites[0]);

var F = 0;

exports.suites = suites;
exports.test_id = test_id;
exports.eq = eq;
exports.b = b;
exports.$$throw = $$throw;
exports.neq = neq;
exports.F = F;
/* Not a pure module */
45 changes: 45 additions & 0 deletions jscomp/test/bs_float_test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
let suites : Mt.pair_suites ref = ref []
let test_id = ref 0
let eq loc x y = Mt.eq_suites loc x y ~test_id ~suites
let b loc x = Mt.bool_suites loc x ~test_id ~suites
let throw loc x = Mt.throw_suites ~test_id ~suites loc x
let neq loc x y =
incr test_id ;
suites :=
(loc ^" id " ^ (string_of_int !test_id), (fun _ -> Mt.Neq(x,y))) :: !suites

module F = Belt.Float

let () =
eq __LOC__ (F.fromInt 1) 1.0;
eq __LOC__ (F.fromInt (-1)) (-1.0)

let () =
eq __LOC__ (F.toInt 1.0) 1;
eq __LOC__ (F.toInt 1.3) 1;
eq __LOC__ (F.toInt 1.7) 1;
eq __LOC__ (F.toInt (-1.0)) (-1);
eq __LOC__ (F.toInt (-1.5)) (-1);
eq __LOC__ (F.toInt (-1.7)) (-1)

let () =
eq __LOC__ (F.fromString "1") 1.0;
eq __LOC__ (F.fromString "-1") (-1.0);
eq __LOC__ (F.fromString "1.7") 1.7;
eq __LOC__ (F.fromString "-1.0") (-1.0);
eq __LOC__ (F.fromString "-1.5") (-1.5);
eq __LOC__ (F.fromString "-1.7") (-1.0)

let () =
eq __LOC__ (F.toString 1.0) "1";
eq __LOC__ (F.toString (-1.0)) "-1";
eq __LOC__ (F.toString (-1.5)) "-1.5"

let () =
let open F in
eq __LOC__ (2.0 + 3.0) 5.0;
eq __LOC__ (2.0 - 3.0) (-1.0);
eq __LOC__ (2.0 * 3.0) 6.0;
eq __LOC__ (3.0 / 2.0) 1.5;

;; Mt.from_pair_suites __LOC__ !suites
Loading

0 comments on commit f40c765

Please sign in to comment.