Add support for hexadecimal, octal and binary values#40
Add support for hexadecimal, octal and binary values#40jubianchi wants to merge 1 commit intohoaproject:masterfrom jubianchi:more-numbers
Conversation
|
Of course, tests are green: The only drawbacks of those notation are they are not compatible with PHP itself so we cannot directly add them to unit tests :/ We could support native syntax (as @Metalaka pointed out in #39 - http://php.net/manual/en/language.types.integer.php) but we would have to forbid constants/ids starting with digits so that we can use |
|
this seems weird, but just thinking out loud: what about allowing constants starting by any digit other than |
|
@CircleCode why not :)
Must also apply to ids then ;) |
|
maybe the question is "why did we want to allow constants starting by digits?" → the answer could help to chose one of the possible solutions amongst
|
|
When talking with @Hywan about supporting constants/ids starting with digits, I talked about "side effects": this one is a side-effects. It prevents us from implementing something interesting with a generic notation. My opinion is: let's close #39 and change constant names in #38 to something like |
|
I share @jubianchi 's opinion. |
|
See my comment in #39. |
|
@jubianchi @CircleCode 👍 |
|
👍 |
| new LUT\Sampler\Random() | ||
| ), | ||
| 9 | ||
| 7 |
There was a problem hiding this comment.
I changed this value because with 9 the test suite generated more than a million expressions and I could not reach the end of the test... way tooooooo long :)
There was a problem hiding this comment.
I tried with 8 which produce a reasonable test suite (~200 seconds AFAIR) but ended up getting the #42 bug :/
|
Visitor/Arithmetic.php
Outdated
| } elseif ('id' === $token) { | ||
| return $value; | ||
| } elseif ('binary' === $token || 'hex' === $token || 'octal' === $token) { | ||
| $out = (float) eval('return ' . $value . ';'); |
There was a problem hiding this comment.
the only way to make them work is by trimming litterals before passing them to *dec : bindec(str_replace('0b', '', $binary));
There was a problem hiding this comment.
So do it. That's by far faster than using an eval. Also, eval is not caught by the opcode cache.
|
thanks for the review. Updates will be done today |
|
❤️ |
|
@hoaproject/hoackers need review :) |
Users can now use three new notations: * `0x539` for hexadecimal values * `0b101010` for binary values * `052` for octal values We can also mix them in expressions: ``` - ( 0b101010 * ( 0x539 / -052 ) ) / 100 ``` The result here will be `13.37`
| ) | ||
| ->then | ||
| ->object($compiler->parse($hexadecimalNumber)) | ||
| ->isInstanceOf('Hoa\Compiler\Llk\TreeNode') |
There was a problem hiding this comment.
You can check the ID and the value of the token (resp. token and hexadecimal).
Same for subsequent test cases.
|
Review done. Need a little bit more corrections :-). |
|
And some test cases for overflows. |
Users can now use three new notations:
\x5390x539for hexadecimal values\b1010100b101010for binary values\052052for octal valuesWe can also mix them in expressions:
- ( \b101010 * ( \x539 / -\052 ) ) / 100- ( 0b101010 * ( 0x539 / -052 ) ) / 100The result here will be
13.37