Skip to content

Commit 98224d6

Browse files
committed
add bigint pow operator
1 parent 94d209b commit 98224d6

29 files changed

+544
-493
lines changed

jscomp/core/js_exp_make.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,8 @@ let bigint_div ?comment (e1: t) (e2: t) = bin ?comment Div e1 e2
12701270

12711271
let bigint_mod ?comment (e1: t) (e2: t) = bin ?comment Mod e1 e2
12721272

1273+
let bigint_pow ?comment (e1: t) (e2: t) = bin ?comment Pow e1 e2
1274+
12731275
let bigint_comp (cmp : Lam_compat.comparison) ?comment (e0: t) (e1: t) =
12741276
match (cmp, e0.expression_desc, e1.expression_desc) with
12751277
| Ceq, Number (Bigint {i = i0; _}), Number (Bigint {i = i1; _}) -> bool (i0 = i1)

jscomp/core/js_exp_make.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ val bigint_div : ?comment: string -> t -> t -> t
286286

287287
val bigint_mod : ?comment: string -> t -> t -> t
288288

289+
val bigint_pow : ?comment: string -> t -> t -> t
290+
289291
val bigint_comp : Lam_compat.comparison -> ?comment:string -> t -> t -> t
290292

291293
val js_comp : Lam_compat.comparison -> ?comment:string -> t -> t -> t

jscomp/core/js_op.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type binop =
4848
| Mul
4949
| Div
5050
| Mod
51+
| Pow
5152
| InstanceOf
5253

5354
(**

jscomp/core/js_op_util.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ let op_prec (op : Js_op.binop) =
4040
| Band -> (7, 7, 7)
4141
| Lsl | Lsr | Asr -> (10, 10, 11)
4242
| Plus | Minus -> (11, 11, 12)
43-
| Mul | Div | Mod -> (12, 12, 13)
43+
| Mul | Div | Mod | Pow -> (12, 12, 13)
4444

4545
let op_int_prec (op : Js_op.int_op) =
4646
match op with
@@ -64,6 +64,7 @@ let op_str (op : Js_op.binop) =
6464
| Mul -> "*"
6565
| Div -> "/"
6666
| Mod -> "%"
67+
| Pow -> "**"
6768
| Eq -> "="
6869
| Or -> "||"
6970
| And -> "&&"

jscomp/core/lam_analysis.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
7777
| Pintoffloat | Pfloatofint | Pnegfloat
7878
(* | Pabsfloat *)
7979
| Paddfloat | Psubfloat | Pmulfloat | Pdivfloat | Pfloatcomp _ | Pbigintcomp _ | Pjscomp _
80-
| Pnegbigint | Paddbigint | Psubbigint | Pmulbigint
80+
| Pnegbigint | Paddbigint | Psubbigint | Pmulbigint | Ppowbigint
8181
(* String operations *)
8282
| Pstringlength | Pstringrefu | Pstringrefs | Pbyteslength | Pbytesrefu
8383
| Pbytesrefs | Pmakearray | Parraylength | Parrayrefu | Parrayrefs

jscomp/core/lam_compile_primitive.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
221221
| _ -> assert false)
222222
| Pmodint64 -> Js_long.mod_ args
223223
| Pmodbigint -> (match args with [ e1; e2 ] -> E.bigint_mod e1 e2 | _ -> assert false)
224+
| Ppowbigint -> (match args with [ e1; e2 ] -> E.bigint_pow e1 e2 | _ -> assert false)
224225
| Plslint -> (
225226
match args with [ e1; e2 ] -> E.int32_lsl e1 e2 | _ -> assert false)
226227
| Plslint64 -> Js_long.lsl_ args

jscomp/core/lam_convert.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
260260
| Pmulbigint -> prim ~primitive:Pmulbigint ~args loc
261261
| Pdivbigint _is_safe (*FIXME*) -> prim ~primitive:Pdivbigint ~args loc
262262
| Pmodbigint _is_safe (*FIXME*) -> prim ~primitive:Pmodbigint ~args loc
263+
| Ppowbigint -> prim ~primitive:Ppowbigint ~args loc
263264
| Pbigintcomp x -> prim ~primitive:(Pbigintcomp x) ~args loc
264265
| Pintcomp x -> prim ~primitive:(Pintcomp x) ~args loc
265266
| Poffsetint x -> prim ~primitive:(Poffsetint x) ~args loc

jscomp/core/lam_primitive.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type t =
8686
| Pmulbigint
8787
| Pdivbigint
8888
| Pmodbigint
89+
| Ppowbigint
8990
| Pintcomp of Lam_compat.comparison
9091
| Pfloatcomp of Lam_compat.comparison
9192
| Pjscomp of Lam_compat.comparison
@@ -214,6 +215,7 @@ let eq_primitive_approx (lhs : t) (rhs : t) =
214215
| Pmulbigint -> rhs = Pmulbigint
215216
| Pdivbigint -> rhs = Pdivbigint
216217
| Pmodbigint -> rhs = Pmodbigint
218+
| Ppowbigint -> rhs = Ppowbigint
217219
| Pjs_apply -> rhs = Pjs_apply
218220
| Pjs_runtime_apply -> rhs = Pjs_runtime_apply
219221
| Pstringlength -> rhs = Pstringlength

jscomp/core/lam_primitive.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type t =
7676
| Pmulbigint
7777
| Pdivbigint
7878
| Pmodbigint
79+
| Ppowbigint
7980
| Pintcomp of Lam_compat.comparison
8081
| Pfloatcomp of Lam_compat.comparison
8182
| Pjscomp of Lam_compat.comparison

jscomp/core/lam_print.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,13 @@ let primitive ppf (prim : Lam_primitive.t) =
134134
| Pfloatcomp Cle -> fprintf ppf "<=."
135135
| Pfloatcomp Cgt -> fprintf ppf ">."
136136
| Pfloatcomp Cge -> fprintf ppf ">=."
137-
| Pnegbigint -> fprintf ppf "~n"
138-
| Paddbigint -> fprintf ppf "+n"
139-
| Psubbigint -> fprintf ppf "-n"
140-
| Pmulbigint -> fprintf ppf "*n"
141-
| Pdivbigint -> fprintf ppf "/n"
137+
| Pnegbigint -> fprintf ppf "~,"
138+
| Paddbigint -> fprintf ppf "+,"
139+
| Psubbigint -> fprintf ppf "-,"
140+
| Pmulbigint -> fprintf ppf "*,"
141+
| Pdivbigint -> fprintf ppf "/,"
142142
| Pmodbigint -> fprintf ppf "modn"
143+
| Ppowbigint -> fprintf ppf "**,"
143144
| Pbigintcomp Ceq -> fprintf ppf "==,"
144145
| Pbigintcomp Cneq -> fprintf ppf "!=,"
145146
| Pbigintcomp Clt -> fprintf ppf "<,"

jscomp/ml/lambda.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ type primitive =
230230
| Paddfloat | Psubfloat | Pmulfloat | Pdivfloat
231231
| Pfloatcomp of comparison
232232
(* Bigint operations *)
233-
| Pnegbigint | Paddbigint | Psubbigint
233+
| Pnegbigint | Paddbigint | Psubbigint | Ppowbigint
234234
| Pmulbigint | Pdivbigint of is_safe | Pmodbigint of is_safe
235235
| Pbigintcomp of comparison
236236
(* String operations *)

jscomp/ml/lambda.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ type primitive =
196196
| Paddfloat | Psubfloat | Pmulfloat | Pdivfloat
197197
| Pfloatcomp of comparison
198198
(* Bigint operations *)
199-
| Pnegbigint | Paddbigint | Psubbigint
199+
| Pnegbigint | Paddbigint | Psubbigint | Ppowbigint
200200
| Pmulbigint | Pdivbigint of is_safe | Pmodbigint of is_safe
201201
| Pbigintcomp of comparison
202202
(* String operations *)

jscomp/ml/printlambda.ml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,11 @@ let primitive ppf = function
177177
| Pfloatcomp(Cle) -> fprintf ppf "<=."
178178
| Pfloatcomp(Cgt) -> fprintf ppf ">."
179179
| Pfloatcomp(Cge) -> fprintf ppf ">=."
180-
| Pnegbigint -> fprintf ppf "~n"
181-
| Paddbigint -> fprintf ppf "+n"
182-
| Psubbigint -> fprintf ppf "-n"
183-
| Pmulbigint -> fprintf ppf "*n"
180+
| Pnegbigint -> fprintf ppf "~,"
181+
| Paddbigint -> fprintf ppf "+,"
182+
| Psubbigint -> fprintf ppf "-,"
183+
| Pmulbigint -> fprintf ppf "*,"
184+
| Ppowbigint -> fprintf ppf "**,"
184185
| Pdivbigint Safe -> fprintf ppf "/n"
185186
| Pdivbigint Unsafe -> fprintf ppf "/nu"
186187
| Pmodbigint Safe -> fprintf ppf "mod"
@@ -298,6 +299,7 @@ let name_of_primitive = function
298299
| Pmulbigint -> "Pmulbigint"
299300
| Pdivbigint _ -> "Pdivbigint"
300301
| Pmodbigint _ -> "Pmodbigint"
302+
| Ppowbigint -> "Ppowbigint"
301303
| Pbigintcomp _ -> "Pbigintcomp"
302304
| Pstringlength -> "Pstringlength"
303305
| Pstringrefu -> "Pstringrefu"

jscomp/ml/translcore.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ let primitives_table =
370370
("%subbigint", Psubbigint);
371371
("%mulbigint", Pmulbigint);
372372
("%divbigint", Pdivbigint Safe);
373+
("%powbigint", Ppowbigint);
373374
("%modbigint", Pmodbigint Safe);
374375
("%eqbigint", Pbigintcomp Ceq);
375376
("%noteqbigint", Pbigintcomp Cneq);

jscomp/others/js_bigint.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ external \"-": (bigint, bigint) => bigint = "%subbigint"
6868
external \"*": (bigint, bigint) => bigint = "%mulbigint"
6969
external \"/": (bigint, bigint) => bigint = "%divbigint"
7070
external mod: (bigint, bigint) => bigint = "%modbigint"
71+
external \"**": (bigint, bigint) => bigint = "%powbigint"
7172

7273
@send
7374
/**

jscomp/stdlib-406/pervasives.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ external \"-,": (bigint, bigint) => bigint = "%subbigint"
188188
external \"*,": (bigint, bigint) => bigint = "%mulbigint"
189189
external \"/,": (bigint, bigint) => bigint = "%divbigint"
190190
external modn: (bigint, bigint) => bigint = "%modbigint"
191+
external \"**,": (bigint, bigint) => bigint = "%powbigint"
191192

192193
/* String and byte sequence operations -- more in modules String and Bytes */
193194

jscomp/stdlib-406/pervasives.resi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,10 @@ external \"/,": (bigint, bigint) => bigint = "%divbigint"
564564
Left-associative operator at precedence level 7/11. */
565565
external modn: (bigint, bigint) => bigint = "%modbigint"
566566

567+
/** Bigint Exponentiation.
568+
Left-associative operator at precedence level 7/9. */
569+
external \"**,": (bigint, bigint) => bigint = "%powbigint"
570+
567571
@val
568572
@scope("Number")
569573
/** A special floating-point value denoting the result of an

jscomp/stdlib-406/pervasivesU.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ external \"-,": (bigint, bigint) => bigint = "%subbigint"
189189
external \"*,": (bigint, bigint) => bigint = "%mulbigint"
190190
external \"/,": (bigint, bigint) => bigint = "%divbigint"
191191
external modn: (bigint, bigint) => bigint = "%modbigint"
192+
external \"**,": (bigint, bigint) => bigint = "%powbigint"
192193

193194
/* String and byte sequence operations -- more in modules String and Bytes */
194195

jscomp/stdlib-406/pervasivesU.resi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,10 @@ external \"/,": (bigint, bigint) => bigint = "%divbigint"
567567
Left-associative operator at precedence level 7/11. */
568568
external modn: (bigint, bigint) => bigint = "%modbigint"
569569

570+
/** Bigint Exponentiation.
571+
Left-associative operator at precedence level 7/9. */
572+
external \"**,": (bigint, bigint) => bigint = "%powbigint"
573+
570574
@val
571575
@scope("Number")
572576
/** A special floating-point value denoting the result of an

jscomp/syntax/src/res_comments_table.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,8 @@ and walkExpression expr t comments =
12881288
Longident.Lident
12891289
( ":=" | "||" | "&&" | "=" | "==" | "<" | ">" | "!=" | "!=="
12901290
| "<=" | ">=" | "|>" | "+" | "+." | "-" | "-." | "++" | "^"
1291-
| "*" | "*." | "/" | "/." | "**" | "|." | "|.u" | "<>" );
1291+
| "*" | "*." | "/" | "/." | "**" | "**," | "|." | "|.u"
1292+
| "<>" );
12921293
};
12931294
},
12941295
[(Nolabel, operand1); (Nolabel, operand2)] ) ->

jscomp/syntax/src/res_core.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2190,7 +2190,8 @@ and parseBinaryExpr ?(context = OrdinaryExpr) ?a p prec =
21902190
let endPos = p.prevEndPos in
21912191
let tokenPrec =
21922192
(* exponentiation operator is right-associative *)
2193-
if token = Exponentiation then tokenPrec else tokenPrec + 1
2193+
if token = Exponentiation || token = ExponentiationComma then tokenPrec
2194+
else tokenPrec + 1
21942195
in
21952196
let b = parseBinaryExpr ~context p tokenPrec in
21962197
let loc = mkLoc a.Parsetree.pexp_loc.loc_start b.pexp_loc.loc_end in

jscomp/syntax/src/res_parsetree_viewer.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ let operatorPrecedence operator =
294294
| "=" | "==" | "<" | ">" | "!=" | "<>" | "!==" | "<=" | ">=" | "|>" -> 4
295295
| "+" | "+." | "+," | "-" | "-." | "-," | "^" -> 5
296296
| "*" | "*." | "/" | "/." -> 6
297-
| "**" -> 7
297+
| "**" | "**," -> 7
298298
| "#" | "##" | "|." | "|.u" -> 8
299299
| _ -> 0
300300

@@ -317,7 +317,7 @@ let isBinaryOperator operator =
317317
match operator with
318318
| ":=" | "||" | "&&" | "=" | "==" | "<" | ">" | "!=" | "!==" | "<=" | ">="
319319
| "|>" | "+" | "+." | "+," | "-" | "-." | "-," | "^" | "*" | "*." | "*," | "/"
320-
| "/." | "/," | "**" | "|." | "|.u" | "<>" ->
320+
| "/." | "/," | "**" | "**," | "|." | "|.u" | "<>" ->
321321
true
322322
| _ -> false
323323

@@ -342,7 +342,7 @@ let isEqualityOperator operator =
342342

343343
let isRhsBinaryOperator operator =
344344
match operator with
345-
| "**" -> true
345+
| "**" | "**," -> true
346346
| _ -> false
347347

348348
let flattenableOperators parentOperator childOperator =

jscomp/syntax/src/res_scanner.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,9 +691,14 @@ let rec scan scanner =
691691
Token.Hash)
692692
| '*' -> (
693693
match peek scanner with
694-
| '*' ->
695-
next2 scanner;
696-
Token.Exponentiation
694+
| '*' -> (
695+
match peek2 scanner with
696+
| ',' ->
697+
next3 scanner;
698+
Token.ExponentiationComma
699+
| _ ->
700+
next2 scanner;
701+
Token.Exponentiation)
697702
| '.' ->
698703
next2 scanner;
699704
Token.AsteriskDot

jscomp/syntax/src/res_token.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type t =
4444
| AsteriskDot
4545
| AsteriskComma
4646
| Exponentiation
47+
| ExponentiationComma
4748
| Minus
4849
| MinusDot
4950
| MinusComma
@@ -111,7 +112,7 @@ let precedence = function
111112
| Asterisk | AsteriskDot | AsteriskComma | Forwardslash | ForwardslashDot
112113
| ForwardslashComma ->
113114
6
114-
| Exponentiation -> 7
115+
| Exponentiation | ExponentiationComma -> 7
115116
| MinusGreater -> 8
116117
| Dot -> 9
117118
| _ -> 0
@@ -174,6 +175,7 @@ let toString = function
174175
| AsteriskDot -> "*."
175176
| AsteriskComma -> "*,"
176177
| Exponentiation -> "**"
178+
| ExponentiationComma -> "**,"
177179
| Assert -> "assert"
178180
| Tilde -> "tilde"
179181
| Question -> "?"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
1n /, 2n
2+
3+
3n *, 4n
4+
5+
2n **, 2n
6+
7+
10n +, 54
8+
9+
289n -, 138n
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
;;1n /, 2n
2+
;;3n *, 4n
3+
;;2n **, 2n
4+
;;10n +, 54
5+
;;289n -, 138n

jscomp/syntax/tests/printer/other/case.res

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ let precedence = x => switch x {
9393
| AsteriskDot
9494
| Forwardslash
9595
| ForwardslashDot => 6
96-
| Exponentiation => 7
96+
| Exponentiation
97+
| ExponentiationComma => 7
9798
| Hash
9899
| HashHash
99100
| MinusGreater => 8

jscomp/syntax/tests/printer/other/expected/case.res.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ let precedence = x =>
8383
| AsteriskDot
8484
| Forwardslash
8585
| ForwardslashDot => 6
86-
| Exponentiation => 7
86+
| Exponentiation
87+
| ExponentiationComma => 7
8788
| Hash
8889
| HashHash
8990
| MinusGreater => 8

0 commit comments

Comments
 (0)