New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal] Bigint shorthand (123n) for GMP objects #5930
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--TEST-- | ||
Bigint addition tests | ||
--SKIPIF-- | ||
<?php if (!extension_loaded('gmp')) { die('skip requires gmp'); } ?> | ||
--FILE-- | ||
<?php | ||
echo 1_2_3n+1n, "\n"; | ||
var_dump(0700n + 0004n); | ||
echo 2n ** 100, "\n"; | ||
function sum_cube(GMP $a, GMP $b): GMP { | ||
return $a ** 3 + $b ** 3; | ||
} | ||
echo sum_cube(3n, 9000000000000n), "\n"; | ||
|
||
?> | ||
--EXPECTF-- | ||
124 | ||
object(GMP)#1 (1) { | ||
["num"]=> | ||
string(3) "452" | ||
} | ||
1267650600228229401496703205376 | ||
729000000000000000000000000000000000027 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--TEST-- | ||
Bigint AST representation in assert() | ||
--SKIPIF-- | ||
<?php if (!extension_loaded('gmp')) { die('skip requires gmp'); } ?> | ||
--FILE-- | ||
<?php | ||
// The result is a GMP arbitrary precision number object, not an int | ||
try { | ||
assert(is_int(1+0n+01_23n)); | ||
} catch (AssertionError $e) { | ||
echo "Caught " . $e->getMessage() . "\n"; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Caught assert(is_int(1 + 0n + 01_23n)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--TEST-- | ||
Bigint parsing tests | ||
--SKIPIF-- | ||
<?php if (!extension_loaded('tokenizer')) { die('skip requires tokenizer'); } ?> | ||
--FILE-- | ||
<?php | ||
foreach (token_get_all('<?php return 1111111111111111111111111111111111111111111111111239n+012_345n+1n;?>') as $token) { | ||
if (is_string($token)) { | ||
printf("%s\n", $token); | ||
continue; | ||
} | ||
printf("%s: '%s'\n", token_name($token[0]), $token[1]); | ||
} | ||
?> | ||
--EXPECT-- | ||
T_OPEN_TAG: '<?php ' | ||
T_RETURN: 'return' | ||
T_WHITESPACE: ' ' | ||
T_BIGINT: '1111111111111111111111111111111111111111111111111239n' | ||
+ | ||
T_BIGINT: '012_345n' | ||
+ | ||
T_BIGINT: '1n' | ||
; | ||
T_CLOSE_TAG: '?>' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should throw instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does throw (this is C, not C++), and is based on the adjacent ordinary LNUM parsing.
zend_throw_exception(zend_ce_parse_error, "Invalid numeric literal", 0);
For token_get_all, the choice to not throw without the TOKEN_PARSE flag is deliberate - tools such as php-parser couldn't work if token_get_all threw instead of returning the T_ERROR token.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean to not allow octals in bigints at all - reject any input starting with
0
except0n
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the token_get_all() implementation in ext/tokenizer/tokenizer.c clears this exception - I was wondering how it worked for ordinary numbers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes no sense, the GMP extension accepts Hex, Octal and Binary numbers just fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But usecases vs. possible mistakes? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd find it more consistent to allow any integer token to have a bigint equivalent by adding the suffix
n
to it.Possibly use cases would be cryptography with known hex values, arbitrary-precision math based on octal/hex/binary reference material, etc - e.g. https://en.wikipedia.org/wiki/Mersenne_prime