Skip to content

Commit

Permalink
Merge pull request #1620 from DanielXMoore/mod-bigint
Browse files Browse the repository at this point in the history
`%%` operator types support `bigint` in addition to `number`
  • Loading branch information
edemaine authored Nov 24, 2024
2 parents a55086c + 6e4e5f7 commit 00a06fe
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
6 changes: 4 additions & 2 deletions source/parser/helper.civet
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ declareHelper := {
state.prelude.push ["", [ // [indent, statement]
preludeVar
moduloRef
ts ": (a: number, b: number) => number"
" = (a, b) => (a % b + b) % b"
" = "
ts "("
"(a", ts(": number"), ", b", ts(": number"), ") => (a % b + b) % b",
ts ") as ((a: number, b: number) => number) & ((a: bigint, b: bigint) => bigint)"
], ";\n"]
Falsy(FalsyRef): void
state.prelude.push ["", // [indent, statement]
Expand Down
4 changes: 2 additions & 2 deletions test/assignment.civet
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ describe "assignment", ->
a %/= b
a ÷= b
---
var modulo: (a: number, b: number) => number = (a, b) => (a % b + b) % b;
var modulo = ((a: number, b: number) => (a % b + b) % b) as ((a: number, b: number) => number) & ((a: bigint, b: bigint) => bigint);
var div: (a: number, b: number) => number = (a, b) => Math.floor(a / b);
a = modulo(a, b)
a = div(a, b)
Expand Down Expand Up @@ -701,7 +701,7 @@ describe "assignment", ->
---
index = (@index + 1) %% 8
---
var modulo: (a: number, b: number) => number = (a, b) => (a % b + b) % b;
var modulo = ((a: number, b: number) => (a % b + b) % b) as ((a: number, b: number) => number) & ((a: bigint, b: bigint) => bigint);
index = modulo((this.index + 1), 8)
"""

Expand Down
15 changes: 12 additions & 3 deletions test/binary-op.civet
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ describe "binary operations", ->
---
a %% b
---
var modulo: (a: number, b: number) => number = (a, b) => (a % b + b) % b;
var modulo = ((a: number, b: number) => (a % b + b) % b) as ((a: number, b: number) => number) & ((a: bigint, b: bigint) => bigint);
modulo(a, b)
"""

testCase.js """
positive modulo JS
---
a %% b
---
var modulo = (a, b) => (a % b + b) % b;
modulo(a, b)
"""

Expand All @@ -39,7 +48,7 @@ describe "binary operations", ->
---
a %% b %% c
---
var modulo: (a: number, b: number) => number = (a, b) => (a % b + b) % b;
var modulo = ((a: number, b: number) => (a % b + b) % b) as ((a: number, b: number) => number) & ((a: bigint, b: bigint) => bigint);
modulo(modulo(a, b), c)
"""

Expand All @@ -48,7 +57,7 @@ describe "binary operations", ->
---
x + a %% b
---
var modulo: (a: number, b: number) => number = (a, b) => (a % b + b) % b;
var modulo = ((a: number, b: number) => (a % b + b) % b) as ((a: number, b: number) => number) & ((a: bigint, b: bigint) => bigint);
x + modulo(a, b)
"""

Expand Down
2 changes: 1 addition & 1 deletion test/function-block-shorthand.civet
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe "&. function block shorthand", ->
---
'abc'[&%]
---
var modulo: (a: number, b: number) => number = (a, b) => (a % b + b) % b;
var modulo = ((a: number, b: number) => (a % b + b) % b) as ((a: number, b: number) => number) & ((a: bigint, b: bigint) => bigint);
$ => 'abc'[modulo($, 'abc'.length)]
"""

Expand Down
6 changes: 3 additions & 3 deletions test/property-access.civet
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ describe "property access", ->
x[i%%]
x[i %% ]
---
var modulo: (a: number, b: number) => number = (a, b) => (a % b + b) % b;
var modulo = ((a: number, b: number) => (a % b + b) % b) as ((a: number, b: number) => number) & ((a: bigint, b: bigint) => bigint);
x[modulo(i, x.length)]
x[modulo(i , x.length)]
x[modulo(i, x.length)]
Expand All @@ -533,7 +533,7 @@ describe "property access", ->
x.y[i+j%]
(x+y)[i+j%]
---
var modulo: (a: number, b: number) => number = (a, b) => (a % b + b) % b;
var modulo = ((a: number, b: number) => (a % b + b) % b) as ((a: number, b: number) => number) & ((a: bigint, b: bigint) => bigint);
let ref;(ref = x())[modulo(i+j, ref.length)]
let ref1;(ref1 = x.y)[modulo(i+j, ref1.length)]
let ref2;(ref2 = (x+y))[modulo(i+j, ref2.length)]
Expand All @@ -544,6 +544,6 @@ describe "property access", ->
---
x()[i+j%] = rhs
---
var modulo: (a: number, b: number) => number = (a, b) => (a % b + b) % b;
var modulo = ((a: number, b: number) => (a % b + b) % b) as ((a: number, b: number) => number) & ((a: bigint, b: bigint) => bigint);
let ref;(ref = x())[modulo(i+j, ref.length)] = rhs
"""

0 comments on commit 00a06fe

Please sign in to comment.