Esempio n. 1
0
 /**
  * Simple arithmetic solver
  * Using shunting-yard algorithm
  * @param  string $workerId
  * @param  string $taskId
  * @param  string $input
  * @return string
  * @author Andraz <*****@*****.**>
  */
 public function run($workerId, $taskId, $input)
 {
     file_put_contents('result.log', $input, FILE_APPEND);
     $result = \RR\Shunt\Parser::parse($input);
     file_put_contents('result.log', $result, FILE_APPEND);
     $this->sendResult($workerId, $taskId, $result);
     return $result;
 }
 public function testParserWithStringConstants()
 {
     $context = new Context();
     $const = 'string constant';
     $context->def('const', $const, 'string');
     $actual = Parser::parse('const', $context);
     $this->assertEquals($const, $actual);
 }
 /**
  * @param $equation
  * @param array $constants
  *                         @param $expected
  *
  * @dataProvider equationAndConstantsProvider
  */
 public function testParserWithConstants($equation, array $constants, $expected)
 {
     $context = new Context();
     foreach ($constants as $key => $val) {
         $context->def($key, $val);
     }
     $actual = Parser::parse($equation, $context);
     $this->assertEquals($expected, $actual);
 }
 public function testWrapPHPFunction()
 {
     $context = new Context();
     $context->def('abs');
     $equation = 'abs(100)';
     $actual = Parser::parse($equation, $context);
     $expected = 100;
     $this->assertEquals($expected, $actual);
     $equation = 'abs(-100)';
     $actual = Parser::parse($equation, $context);
     $expected = 100;
     $this->assertEquals($expected, $actual);
 }
function benchmark($term)
{
    print "{$term}\n";
    $a = microtime(true);
    for ($i = 0; $i < 10000; ++$i) {
        $r = eval("return {$term};");
    }
    print "native = {$r} : " . (microtime(true) - $a) . " (eval)\n";
    for ($i = 0; $i < 10000; ++$i) {
        $r = Parser::parse($term);
    }
    print "parser = {$r} : " . (microtime(true) - $a) . "\n\n";
}
Esempio n. 6
0
function benchmark($term)
{
    print "{$term}\n";
    $iterations = 10000;
    // native calculations
    $a = microtime(true);
    for ($i = 0; $i < $iterations; ++$i) {
        $r = eval("return {$term};");
    }
    $totalExecutionTime = microtime(true) - $a;
    print "native  : " . round($totalExecutionTime, 6) . "s  " . round($totalExecutionTime / $iterations, 10) . "s  per operation \n";
    // calculations using parser
    $a = microtime(true);
    for ($i = 0; $i < $iterations; ++$i) {
        $r = Parser::parse($term);
    }
    $totalExecutionTime = microtime(true) - $a;
    print "parser  : " . round($totalExecutionTime, 6) . "s  " . round($totalExecutionTime / $iterations, 10) . "s  per operation\n";
    // time taken by scanner
    $a = microtime(true);
    for ($i = 0; $i < $iterations; ++$i) {
        $s = new Scanner($term);
    }
    $totalExecutionTime = microtime(true) - $a;
    print "scanner : " . round($totalExecutionTime, 6) . "s  " . round($totalExecutionTime / $iterations, 10) . "s  per operation\n";
    // calculations using parser
    $c = new Context();
    $p = new Parser(new Scanner($term));
    // time taken to evaluate parser stack
    $a = microtime(true);
    for ($i = 0; $i < $iterations; ++$i) {
        $r = $p->reduce($c);
    }
    $totalExecutionTime = microtime(true) - $a;
    print "reduce  : " . round($totalExecutionTime, 6) . "s  " . round($totalExecutionTime / $iterations, 10) . "s  per operation\n\n";
}
<?php

// Composer autoloading
if (file_exists('vendor/autoload.php')) {
    $loader = (include 'vendor/autoload.php');
}
use RR\Shunt\Parser;
use RR\Shunt\Context;
// einfach
$trm = '3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3';
print Parser::parse($trm);
// 3.0001220703125
print "\n";
// mit eigenen konstanten und funktionen
$ctx = new Context();
$ctx->def('abs');
// wrapper
$ctx->def('foo', 5);
$ctx->def('bar', function ($a, $b) {
    return $a * $b;
});
$trm = '3 + bar(4, 2) / (abs(-1) - foo) ^ 2 ^ 3';
print Parser::parse($trm, $ctx);
// 3.0001220703125
 /**
  * @expectedException RR\Shunt\Exception\ParseError
  */
 public function testParserExceptionSurplusClosingBracket()
 {
     $context = new Context();
     $context->def('pi');
     $equation = 'pi())';
     $actual = Parser::parse($equation, $context);
 }
Esempio n. 9
0
// Composer autoloading
if (file_exists('vendor/autoload.php')) {
    $loader = (include 'vendor/autoload.php');
}
use RR\Shunt\Parser;
use RR\Shunt\Context;
// einfach
$trm = '3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3';
print Parser::parse($trm) . "\n";
// 3.0001220703125
// mit eigenen konstanten und funktionen
$ctx = new Context();
$ctx->def('abs');
// wrapper
$ctx->def('foo', 5);
$ctx->def('bar', function ($a, $b) {
    return $a * $b;
});
$trm = '3 + bar(4, 2) / (abs(-1) - foo) ^ 2 ^ 3';
print Parser::parse($trm, $ctx) . "\n";
// 3.0001220703125
// mit string konstanten
$ctx = new Context();
$ctx->def('groupA', 'A', 'string');
// string constant
$ctx->def('isgroupA', function ($g) {
    return $g == 'A' ? 1 : 0;
});
$trm = 'isgroupA(groupA)';
print Parser::parse($trm, $ctx) . "\n";
// 1
 /**
  * @expectedException \RR\Shunt\Exception\RuntimeError
  */
 public function testParenthesisThrowsError()
 {
     $equation = '()';
     Parser::parse($equation);
 }
 public function testParenthesisThrowsError()
 {
     $this->expectOutputString(null);
     $equation = '()';
     return Parser::parse($equation);
 }
Esempio n. 12
0
 /**
  * @expectedException \RR\Shunt\Exception\RuntimeError
  */
 public function testModulusFromZero()
 {
     $equation = '100%0';
     Parser::parse($equation);
 }