diff --git a/src/parser.c b/src/parser.c index e7c6a4ea00..59f375f78d 100644 --- a/src/parser.c +++ b/src/parser.c @@ -498,6 +498,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; diff --git a/src/parser.y b/src/parser.y index 7599d9f2f4..4ad00a6886 100644 --- a/src/parser.y +++ b/src/parser.y @@ -230,6 +230,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; diff --git a/tests/jq.test b/tests/jq.test index 466d185099..d948c27b09 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -1655,6 +1655,10 @@ try (1%.) catch . 1/0 jq: error: Division by zero? at , line 1: +%%FAIL +1%0 +jq: error: Remainder by zero? at , line 1: + # Basic numbers tests: integers, powers of two [range(-52;52;1)] as $powers | [$powers[]|pow(2;.)|log2|round] == $powers null diff --git a/tests/shtest b/tests/shtest index 6cc428ca2a..e36cb72cee 100755 --- a/tests/shtest +++ b/tests/shtest @@ -30,47 +30,67 @@ $VALGRIND $Q $JQ -Rne '[inputs] == ["a\u0000b", "c\u0000d", "e"]' $d/input # String constant folding (addition only) nref=$($VALGRIND $Q $JQ -n --debug-dump-disasm '"foo"' | wc -l) +n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '"foo" + "bar"' | wc -l) +if [ $n -ne $nref ]; then + echo "Constant expression folding for strings didn't work" + exit 1 +fi -# Numeric constant folding (not all ops yet) +# Numeric constant folding (binary operators) n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '1+1' | wc -l) if [ $n -ne $nref ]; then - echo "Constant expression folding for strings didn't work" - exit 1 + echo "Constant expression folding for numbers didn't work" + exit 1 fi n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '1-1' | wc -l) if [ $n -ne $nref ]; then - echo "Constant expression folding for strings didn't work" - exit 1 + echo "Constant expression folding for numbers didn't work" + exit 1 fi n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '2*3' | wc -l) if [ $n -ne $nref ]; then - echo "Constant expression folding for strings didn't work" - exit 1 + echo "Constant expression folding for numbers didn't work" + exit 1 fi n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9/3' | wc -l) if [ $n -ne $nref ]; then - echo "Constant expression folding for strings didn't work" - exit 1 + echo "Constant expression folding for numbers didn't work" + exit 1 +fi +n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9%3' | wc -l) +if [ $n -ne $nref ]; then + echo "Constant expression folding for numbers didn't work" + exit 1 fi n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9==3' | wc -l) if [ $n -ne $nref ]; then - echo "Constant expression folding for strings didn't work" - exit 1 + echo "Constant expression folding for numbers didn't work" + exit 1 fi n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9!=3' | wc -l) if [ $n -ne $nref ]; then - echo "Constant expression folding for strings didn't work" - exit 1 + echo "Constant expression folding for numbers didn't work" + exit 1 +fi +n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9<3' | wc -l) +if [ $n -ne $nref ]; then + echo "Constant expression folding for numbers didn't work" + exit 1 +fi +n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9>3' | wc -l) +if [ $n -ne $nref ]; then + echo "Constant expression folding for numbers didn't work" + exit 1 fi n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9<=3' | wc -l) if [ $n -ne $nref ]; then - echo "Constant expression folding for strings didn't work" - exit 1 + echo "Constant expression folding for numbers didn't work" + exit 1 fi n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9>=3' | wc -l) if [ $n -ne $nref ]; then - echo "Constant expression folding for strings didn't work" - exit 1 + echo "Constant expression folding for numbers didn't work" + exit 1 fi ## Test JSON sequence support