Skip to content

Commit d82b4bc

Browse files
committed
clean up
1 parent 9652115 commit d82b4bc

11 files changed

+23
-20
lines changed

jscomp/core/js_analyzer.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ let rec eq_expression ({ expression_desc = x0 } : J.expression)
158158
| Undefined x -> y0 = Undefined x
159159
| Number (Int { i }) -> (
160160
match y0 with Number (Int { i = j }) -> i = j | _ -> false)
161-
| Number (Bigint (s0, i )) -> (
162-
match y0 with Number (Bigint (s1, j)) -> s0 = s1 && i = j | _ -> false)
161+
| Number (Bigint {positive = p0; value = v0}) -> (
162+
match y0 with Number (Bigint {positive = p1; value = v1}) -> p0 = p1 && v0 = v1 | _ -> false)
163163
| Number (Float _) -> false
164164
(* begin match y0 with
165165
| Number (Float j) ->

jscomp/core/js_dump.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ and expression_desc cxt ~(level : int) f x : cxt =
664664
Int32.to_string i
665665
(* check , js convention with ocaml lexical convention *)
666666
| Uint i -> Format.asprintf "%lu" i
667-
| Bigint (sign, i) -> Format.asprintf "%sn" (Bigint_utils.to_string sign i)
667+
| Bigint {positive; value} -> Format.asprintf "%sn" (Bigint_utils.to_string positive value)
668668
in
669669
let need_paren =
670670
if s.[0] = '-' then level > 13

jscomp/core/js_exp_make.ml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ let obj_int_tag_literal : t =
312312

313313
let int ?comment ?c i : t = { expression_desc = Number (Int { i; c }); comment }
314314

315-
let bigint ?comment sign i : t = { expression_desc = Number (Bigint (sign, i)); comment}
315+
let bigint ?comment sign i : t = { expression_desc = Number (Bigint {positive=sign; value=i}); comment}
316316

317-
let zero_bigint_literal : t = {expression_desc = Number (Bigint (true, "0")); comment = None}
317+
let zero_bigint_literal : t = {expression_desc = Number (Bigint {positive=true; value="0"}); comment = None}
318318

319319
let small_int i : t =
320320
match i with
@@ -808,9 +808,7 @@ let tag_type = function
808808
| Int i -> small_int i
809809
| Float f -> float f
810810
| Bigint i ->
811-
let open Bigint_utils in
812-
let sign, i = i |> remove_leading_sign in
813-
let i = remove_leading_zeros i in
811+
let sign, i = Bigint_utils.parse_bigint i in
814812
bigint sign i
815813
| Bool b -> bool b
816814
| Null -> nil

jscomp/core/js_op.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,13 @@ type 'a access = Getter | Setter
126126
(* literal char *)
127127
type float_lit = { f : string } [@@unboxed]
128128

129+
type bigint_lit = { positive: bool; value: string }
130+
129131
type number =
130132
| Float of float_lit
131133
| Int of { i : int32; c : int option }
132134
| Uint of int32
133-
| Bigint of bool * string
135+
| Bigint of bigint_lit
134136

135137
(* becareful when constant folding +/-,
136138
since we treat it as js nativeint, bitwise operators:

jscomp/core/lam.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ let prim ~primitive:(prim : Lam_primitive.t) ~args loc : t =
506506
(* FIXME: could raise? *)
507507
Lift.bool
508508
(Lam_compat.cmp_float cmp (float_of_string a) (float_of_string b))
509-
| Pbigintcomp cmp, Const_bigint (_, a), Const_bigint (_, b) -> default ()
509+
| Pbigintcomp cmp, Const_bigint _, Const_bigint _ -> default ()
510510
| Pintcomp ((Ceq | Cneq) as op), Const_pointer a, Const_pointer b ->
511511
Lift.bool
512512
(match op with

jscomp/frontend/external_ffi_types.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,7 @@ let inline_int64_primitive (i : int64) : string list =
301301
[""; to_string (Ffi_inline_const (Const_int64 i))]
302302

303303
let inline_bigint_primitive (i : string) : string list =
304-
let sign, i = i |> Bigint_utils.remove_leading_sign in
305-
let i = Bigint_utils.remove_leading_zeros i in
304+
let sign, i = Bigint_utils.parse_bigint i in
306305
[""; to_string (Ffi_inline_const (Const_bigint (sign, i)))]
307306

308307
let inline_float_primitive (i : string) : string list =

jscomp/ml/bigint_utils.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let remove_leading_sign str : bool * string =
88
if len = 0 then (false, str)
99
else
1010
if is_neg str || is_pos str then (not (is_neg str), String.sub str 1 (len -1))
11-
else (not (is_neg str), str)
11+
else (true, str)
1212

1313
(*
1414
Removes leading zeros from the string only if the first non-zero character
@@ -48,7 +48,11 @@ let remove_leading_zeros str =
4848
let processed_str = aux str in
4949
if starts_with_minus then "-" ^ processed_str else processed_str
5050

51-
let is_numeric s =
51+
let parse_bigint s =
52+
let sign, i = remove_leading_sign s in
53+
(sign, remove_leading_zeros i)
54+
55+
let is_valid s =
5256
let len = String.length s in
5357
if len = 0 then false
5458
else

jscomp/ml/bigint_utils.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ val is_pos: string -> bool
33
val to_string: bool -> string -> string
44
val remove_leading_sign : string -> bool * string
55
val remove_leading_zeros : string -> string
6-
val is_numeric : string -> bool
6+
val parse_bigint: string -> bool * string
7+
val is_valid : string -> bool
78
val compare : bool * string -> bool * string -> int

jscomp/ml/parmatch.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ let build_other ext env : Typedtree.pattern = match env with
11071107
| ({pat_desc=(Tpat_constant (Const_bigint _))} as p,_) :: _ ->
11081108
build_other_constant
11091109
(function Tpat_constant(Const_bigint (sign, i)) -> String.length (Bigint_utils.to_string sign i) | _ -> assert false)
1110-
(function i -> Tpat_constant(Const_bigint (true, (String.make i '*'))))
1110+
(function i -> Tpat_constant(Const_bigint (true, (string_of_int i))))
11111111
0 succ p env
11121112
| ({pat_desc=(Tpat_constant (Const_string _))} as p,_) :: _ ->
11131113
build_other_constant

jscomp/ml/typecore.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,7 @@ let constant : Parsetree.constant -> (Asttypes.constant, error) result =
260260
with Failure _ -> Error (Literal_overflow "int64")
261261
end
262262
| Pconst_integer (i,Some 'n') ->
263-
let sign, i = i |> Bigint_utils.remove_leading_sign in
264-
let i = Bigint_utils.remove_leading_zeros i in
263+
let sign, i = Bigint_utils.parse_bigint i in
265264
Ok (Const_bigint (sign, i))
266265
| Pconst_integer (i,Some c) -> Error (Unknown_literal (i, c))
267266
| Pconst_char c -> Ok (Const_char c)

jscomp/syntax/src/res_core.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,10 +813,10 @@ let parseConstant p =
813813
match p.Parser.token with
814814
| Int {i; suffix} ->
815815
(* Only decimal literal is allowed for bigint *)
816-
if suffix = Some 'n' && not (Bigint_utils.is_numeric i) then
816+
if suffix = Some 'n' && not (Bigint_utils.is_valid i) then
817817
Parser.err p
818818
(Diagnostics.message
819-
"Invalid decimal literal. Only decimal literal is allowed for \
819+
"Invalid bigint literal. Only decimal literal is allowed for \
820820
bigint.");
821821
let intTxt = if isNegative then "-" ^ i else i in
822822
Parsetree.Pconst_integer (intTxt, suffix)

0 commit comments

Comments
 (0)