-
Notifications
You must be signed in to change notification settings - Fork 37
Add support for hexadecimal, octal and binary values #40
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/** | ||
* Hoa | ||
* | ||
* | ||
* @license | ||
* | ||
* New BSD License | ||
* | ||
* Copyright © 2007-2016, Hoa community. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* * Neither the name of the Hoa nor the names of its contributors may be | ||
* used to endorse or promote products derived from this software without | ||
* specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
namespace Hoa\Math\Exception; | ||
|
||
/** | ||
* Class \Hoa\Math\Exception\OutOfBound. | ||
* | ||
* Extending the \Hoa\Math\Exception class. | ||
* | ||
* @copyright Copyright © 2007-2016 Hoa community | ||
* @license New BSD License | ||
*/ | ||
class OutOfBounds extends Exception | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,7 +65,7 @@ public function case_visitor_exhaustively() | |
new Regex\Visitor\Isotropic( | ||
new LUT\Sampler\Random() | ||
), | ||
9 | ||
7 | ||
), | ||
$compiler = Compiler\Llk\Llk::load( | ||
new File\Read('hoa://Library/Math/Arithmetic.pp') | ||
|
@@ -75,7 +75,7 @@ public function case_visitor_exhaustively() | |
->executeOnFailure(function () use (&$expression) { | ||
echo 'Failed expression: ', $expression, '.', "\n"; | ||
}) | ||
->when(function () use (&$sampler, &$compiler, &$visitor) { | ||
->when(function () use (&$sampler, &$compiler, &$visitor, &$expression) { | ||
foreach ($sampler as $i=> $expression) { | ||
try { | ||
$x = (float) $visitor->visit( | ||
|
@@ -95,7 +95,7 @@ public function case_visitor_exhaustively() | |
|
||
$this | ||
->float($x) | ||
->isNearlyEqualTo($y); | ||
->isNearlyEqualTo($y, 1.0e-10); | ||
} | ||
}); | ||
} | ||
|
@@ -223,4 +223,66 @@ public function case_change_default_context() | |
->call('getVariable')->withArguments($variableName)->once | ||
->call('getConstant')->withArguments('PI')->once; | ||
} | ||
|
||
public function case_visitor_hexadecimal_number() | ||
{ | ||
$this | ||
->given( | ||
$compiler = Compiler\Llk\Llk::load(new File\Read('hoa://Library/Math/Arithmetic.pp')), | ||
$visitor = new CUT(), | ||
$hexadecimalNumber = '0x2a' | ||
) | ||
->then | ||
->object($compiler->parse($hexadecimalNumber)) | ||
->isInstanceOf('Hoa\Compiler\Llk\TreeNode') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can check the ID and the value of the token (resp. |
||
->float($visitor->visit($compiler->parse($hexadecimalNumber))) | ||
->isEqualTo(hexdec('2a')); | ||
} | ||
|
||
public function case_visitor_out_of_bounds_hexadecimal_number() | ||
{ | ||
$this | ||
->given( | ||
$compiler = Compiler\Llk\Llk::load(new File\Read('hoa://Library/Math/Arithmetic.pp')), | ||
$visitor = new CUT(), | ||
$hexadecimalNumber = '0x9000000000000000' | ||
) | ||
->then | ||
->object($compiler->parse($hexadecimalNumber)) | ||
->isInstanceOf('Hoa\Compiler\Llk\TreeNode') | ||
->exception(function () use ($hexadecimalNumber, $compiler, $visitor) { | ||
$visitor->visit($compiler->parse($hexadecimalNumber)); | ||
}) | ||
->isInstanceOf('Hoa\Math\Exception\OutOfBounds'); | ||
} | ||
|
||
public function case_visitor_octal_number() | ||
{ | ||
$this | ||
->given( | ||
$compiler = Compiler\Llk\Llk::load(new File\Read('hoa://Library/Math/Arithmetic.pp')), | ||
$visitor = new CUT(), | ||
$octalNumber = '052' | ||
) | ||
->then | ||
->object($compiler->parse($octalNumber)) | ||
->isInstanceOf('Hoa\Compiler\Llk\TreeNode') | ||
->float($visitor->visit($compiler->parse($octalNumber))) | ||
->isEqualTo(octdec('52')); | ||
} | ||
|
||
public function case_visitor_binary_number() | ||
{ | ||
$this | ||
->given( | ||
$compiler = Compiler\Llk\Llk::load(new File\Read('hoa://Library/Math/Arithmetic.pp')), | ||
$visitor = new CUT(), | ||
$binary = '0b101010' | ||
) | ||
->then | ||
->object($compiler->parse($binary)) | ||
->isInstanceOf('Hoa\Compiler\Llk\TreeNode') | ||
->float($visitor->visit($compiler->parse($binary))) | ||
->isEqualTo(bindec('101010')); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
namespace Hoa\Math\Visitor; | ||
|
||
use Hoa\Math; | ||
use Hoa\Math\Exception\OutOfBounds; | ||
use Hoa\Visitor; | ||
|
||
/** | ||
|
@@ -245,18 +246,39 @@ public function visit( | |
|
||
case 'token': | ||
$value = $element->getValueValue(); | ||
$token = $element->getValueToken(); | ||
$out = null; | ||
|
||
if ('constant' === $element->getValueToken()) { | ||
if ('constant' === $token) { | ||
if (defined($value)) { | ||
$out = constant($value); | ||
} else { | ||
$out = $this->getConstant($value); | ||
} | ||
} elseif ('id' === $element->getValueToken()) { | ||
} elseif ('id' === $token) { | ||
return $value; | ||
} else { | ||
$out = (float) $value; | ||
$number = preg_replace('/^0[xXbB]/', '', $value); | ||
|
||
switch ($token) { | ||
case 'binary': | ||
$number = bindec($number); | ||
break; | ||
|
||
case 'hexadecimal': | ||
$number = hexdec($number); | ||
break; | ||
|
||
case 'octal': | ||
$number = octdec($number); | ||
break; | ||
} | ||
|
||
if ('hexadecimal' === $token && $number > PHP_INT_MAX) { | ||
throw new OutOfBounds('Number ' . $value . ' is out of bound for the ' . php_uname('m') . ' platform.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, this is not totally correct. We can have a double/real written in the hexa or octal notation. Do we restrict binary, hexadecimal and octal notation to integers only? If yes, this is still not correct in case of an integer overflow: You must check it is greater or equal to |
||
} | ||
|
||
$out = (float) $number; | ||
} | ||
|
||
$acc = function () use ($out, $acc) { | ||
|
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 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried with
8
which produce a reasonable test suite (~200 seconds AFAIR) but ended up getting the #42 bug :/