Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Add support for hexadecimal, octal and binary values #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions Arithmetic.pp
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,20 @@
//


%skip space [\x20\x09]+
%token bracket_ \(
%token _bracket \)
%token comma ,
%token number (0|[1-9]\d*)(\.\d+)?([eE][\+\-]?\d+)?
%token plus \+
%token minus \-|−
%token times \*|×
%token div /|÷
%token constant [A-Z_]+[A-Z0-9_]+
%token id \w+
%skip space [\x20\x09]+
%token bracket_ \(
%token _bracket \)
%token comma ,
%token hexadecimal 0[xX][0-9a-fA-F]{1,16}
%token octal 0[0-7]{1,21}
%token binary 0[bB][01]{1,64}
%token number (0|[1-9]\d*)(\.\d+)?([eE][\+\-]?\d+)?
%token plus \+
%token minus \-|−
%token times \*|×
%token div /|÷
%token constant [A-Z_]+[A-Z0-9_]+
%token id \w+

expression:
primary() ( ::plus:: #addition expression() )?
Expand All @@ -73,7 +76,10 @@
| function()

number:
<number>
<number>
| <hexadecimal>
| <octal>
| <binary>

constant:
<constant>
Expand Down
49 changes: 49 additions & 0 deletions Exception/OutOfBounds.php
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
{
}
24 changes: 15 additions & 9 deletions Test/Unit/Arithmetic.pp
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@
//


%skip space [\x20\x09]+
%token bracket_ \(
%token _bracket \)
%token comma ,
%token number ([1-9]\d*)(\.\d+)?
%token plus \+
%token minus \-
%token times \*
%token div /
%skip space [\x20\x09]+
%token bracket_ \(
%token _bracket \)
%token comma ,
%token hexadecimal 0[xX][1-9a-fA-F][0-9a-fA-F]{0,15}
%token octal 0[1-7][0-7]{0,20}
%token binary 0[bB]1[01]{0,63}
%token number ([1-9]\d*)(\.\d+)?
%token plus \+
%token minus \-
%token times \*
%token div /

expression:
primary() ( ::plus:: #addition expression() )?
Expand All @@ -70,3 +73,6 @@

number:
<number>
| <hexadecimal>
| <octal>
| <binary>
68 changes: 65 additions & 3 deletions Test/Unit/Visitor/Arithmetic.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function case_visitor_exhaustively()
new Regex\Visitor\Isotropic(
new LUT\Sampler\Random()
),
9
7
Copy link
Member Author

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 :)

Copy link
Member Author

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 :/

),
$compiler = Compiler\Llk\Llk::load(
new File\Read('hoa://Library/Math/Arithmetic.pp')
Expand All @@ -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(
Expand All @@ -95,7 +95,7 @@ public function case_visitor_exhaustively()

$this
->float($x)
->isNearlyEqualTo($y);
->isNearlyEqualTo($y, 1.0e-10);
}
});
}
Expand Down Expand Up @@ -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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check the ID and the value of the token (resp. token and hexadecimal).
Same for subsequent test cases.

->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'));
}
}
28 changes: 25 additions & 3 deletions Visitor/Arithmetic.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
namespace Hoa\Math\Visitor;

use Hoa\Math;
use Hoa\Math\Exception\OutOfBounds;
use Hoa\Visitor;

/**
Expand Down Expand Up @@ -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.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

Choose a reason for hiding this comment

The 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 0.

}

$out = (float) $number;
}

$acc = function () use ($out, $acc) {
Expand Down