-
Notifications
You must be signed in to change notification settings - Fork 470
Introducing the bigint #6670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introducing the bigint #6670
Changes from 44 commits
1441f59
db001fa
20410da
2f6217b
0b14c33
4f40575
6020820
8d95ccd
83070a0
128aa20
c1d39b2
8835276
51e2ab2
84cf107
0f21844
6b01997
d386c4a
bc22261
3b34a01
d3032e1
d3090ff
5683ca2
da60074
41ba70b
3e9a471
fee14cc
fc1ea04
ec90078
b8a5efd
79df62c
dd6b0cf
9852ad0
00d741a
379dc31
990e5ac
0d40dd9
79c7013
5748e83
4a43e39
e7e41c4
e8e8050
5e963cd
84eecd7
9652115
d82b4bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
[1;33mWarning number 11[0m | ||
[36m/.../fixtures/bigint_match_literal.res[0m:[2m3:3-4[0m | ||
|
||
1 [2m│[0m let m1 = switch 1n { | ||
2 [2m│[0m | 0001n => 1 | ||
[1;33m3[0m [2m│[0m | [1;33m1n[0m => 1 | ||
4 [2m│[0m | -0001n => -1 | ||
5 [2m│[0m | _ => 0 | ||
|
||
this match case is unused. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
[1;31mWe've found a bug for you![0m | ||
[36m/.../fixtures/variant_coercion_bigint.res[0m:[2m5:10-20[0m | ||
|
||
3 [2m│[0m let x = One(true) | ||
4 [2m│[0m | ||
[1;31m5[0m [2m│[0m let y = ([1;31mx :> bigint[0m) | ||
6 [2m│[0m | ||
|
||
Type x is not a subtype of bigint |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
[1;31mWe've found a bug for you![0m | ||
[36m/.../fixtures/variant_coercion_bigint_as.res[0m:[2m5:10-20[0m | ||
|
||
3 [2m│[0m let x = One | ||
4 [2m│[0m | ||
[1;31m5[0m [2m│[0m let y = ([1;31mx :> bigint[0m) | ||
6 [2m│[0m | ||
|
||
Type x is not a subtype of bigint |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
let m1 = switch 1n { | ||
| 0001n => 1 | ||
| 1n => 1 | ||
| -0001n => -1 | ||
| _ => 0 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type x = | @as(1n) One(bool) | @as(2n) Two | ||
|
||
let x = One(true) | ||
|
||
let y = (x :> bigint) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type x = | @as(1n) One | Two | ||
|
||
let x = One | ||
|
||
let y = (x :> bigint) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -312,6 +312,10 @@ let obj_int_tag_literal : t = | |
|
||
let int ?comment ?c i : t = { expression_desc = Number (Int { i; c }); comment } | ||
|
||
let bigint ?comment sign i : t = { expression_desc = Number (Bigint (sign, i)); comment} | ||
|
||
let zero_bigint_literal : t = {expression_desc = Number (Bigint (true, "0")); comment = None} | ||
|
||
let small_int i : t = | ||
match i with | ||
| 0 -> zero_int_literal | ||
|
@@ -803,11 +807,17 @@ let tag_type = function | |
| Ast_untagged_variants.String s -> str s ~delim:DStarJ | ||
| Int i -> small_int i | ||
| Float f -> float f | ||
| Bigint i -> | ||
let open Bigint_utils in | ||
let sign, i = i |> remove_leading_sign in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style/naming: perhaps: |
||
let i = remove_leading_zeros i in | ||
bigint sign i | ||
| Bool b -> bool b | ||
| Null -> nil | ||
| Undefined -> undefined | ||
| Untagged IntType -> str "number" | ||
| Untagged FloatType -> str "number" | ||
| Untagged BigintType -> str "bigint" | ||
| Untagged BooleanType -> str "boolean" | ||
| Untagged FunctionType -> str "function" | ||
| Untagged StringType -> str "string" | ||
|
@@ -1253,6 +1263,23 @@ let rec int32_band ?comment (e1 : J.expression) (e2 : J.expression) : | |
(* let int32_bin ?comment op e1 e2 : J.expression = *) | ||
(* {expression_desc = Int32_bin(op,e1, e2); comment} *) | ||
|
||
let bigint_op ?comment op (e1: t) (e2: t) = bin ?comment op e1 e2 | ||
|
||
let bigint_comp (cmp : Lam_compat.comparison) ?comment (e0: t) (e1: t) = | ||
bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1 | ||
|
||
let bigint_div ~checked ?comment (e0: t) (e1: t) = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like the option to disable checking divide by zero is really only hidden under some ancient internal-only configuration at some point, it can be removed, perhaps in a later PR |
||
if checked then | ||
runtime_call Js_runtime_modules.bigint "div" [e0; e1] | ||
else | ||
bigint_op ?comment Div e0 e1 | ||
|
||
let bigint_mod ~checked ?comment (e0: t) (e1: t) = | ||
if checked then | ||
runtime_call Js_runtime_modules.bigint "mod_" [e0; e1] | ||
else | ||
bigint_op ?comment Mod e0 e1 | ||
|
||
(* TODO -- alpha conversion | ||
remember to add parens.. | ||
*) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ type binop = | |
| Mul | ||
| Div | ||
| Mod | ||
| Pow | ||
| InstanceOf | ||
|
||
(** | ||
|
@@ -129,6 +130,7 @@ type number = | |
| Float of float_lit | ||
| Int of { i : int32; c : int option } | ||
| Uint of int32 | ||
| Bigint of bool * string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small style suggestion keep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good, it seems not going to be unboxed, still okay? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The difference is going to be tiny in practice. |
||
|
||
(* becareful when constant folding +/-, | ||
since we treat it as js nativeint, bitwise operators: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -506,6 +506,7 @@ let prim ~primitive:(prim : Lam_primitive.t) ~args loc : t = | |
(* FIXME: could raise? *) | ||
Lift.bool | ||
(Lam_compat.cmp_float cmp (float_of_string a) (float_of_string b)) | ||
| Pbigintcomp cmp, Const_bigint (_, a), Const_bigint (_, b) -> default () | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: here |
||
| Pintcomp ((Ceq | Cneq) as op), Const_pointer a, Const_pointer b -> | ||
Lift.bool | ||
(match op with | ||
|
@@ -641,7 +642,7 @@ let rec eval_const_as_bool (v : Lam_constant.t) : bool = | |
| Const_int64 x -> x <> 0L | ||
| Const_js_false | Const_js_null | Const_module_alias | Const_js_undefined _ -> | ||
false | ||
| Const_js_true | Const_string _ | Const_pointer _ | Const_float _ | ||
| Const_js_true | Const_string _ | Const_pointer _ | Const_float _ | Const_bigint _ | ||
| Const_block _ | Const_float_array _ -> | ||
true | ||
| Const_some b -> eval_const_as_bool b | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just todo note. maybe it will be better to disable ansi color for test snapshot, or to checkin both colored and not