Skip to content

Commit

Permalink
Fold modulo operator on constant values and raise zero remainder erro…
Browse files Browse the repository at this point in the history
…r quickly
  • Loading branch information
itchyny committed Jan 24, 2021
1 parent 80052e5 commit 3442d21
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ static block constant_fold(block a, block b, int op) {
case '-': res = jv_number(na - nb); break;
case '*': res = jv_number(na * nb); break;
case '/': res = jv_number(na / nb); break;
case '%': res = jv_number((intmax_t)nb == 0 ? INFINITY : (intmax_t)na % (intmax_t)nb); break;
case EQ: res = (cmp == 0 ? jv_true() : jv_false()); break;
case NEQ: res = (cmp != 0 ? jv_true() : jv_false()); break;
case '<': res = (cmp < 0 ? jv_true() : jv_false()); break;
Expand Down
1 change: 1 addition & 0 deletions src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ static block constant_fold(block a, block b, int op) {
case '-': res = jv_number(na - nb); break;
case '*': res = jv_number(na * nb); break;
case '/': res = jv_number(na / nb); break;
case '%': res = jv_number((intmax_t)nb == 0 ? INFINITY : (intmax_t)na % (intmax_t)nb); break;
case EQ: res = (cmp == 0 ? jv_true() : jv_false()); break;
case NEQ: res = (cmp != 0 ? jv_true() : jv_false()); break;
case '<': res = (cmp < 0 ? jv_true() : jv_false()); break;
Expand Down
4 changes: 4 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,10 @@ try (1%.) catch .
1/0
jq: error: Division by zero? at <top-level>, line 1:

%%FAIL
1%0
jq: error: Remainder by zero? at <top-level>, line 1:

# Basic numbers tests: integers, powers of two
[range(-52;52;1)] as $powers | [$powers[]|pow(2;.)|log2] == $powers
null
Expand Down

0 comments on commit 3442d21

Please sign in to comment.