Exemple #1
0
<?php

require 'vendor/autoload.php';
use Cubiche\Core\Visitor\Tests\Fixtures\Calculator;
use Cubiche\Core\Visitor\Tests\Fixtures\Mult;
use Cubiche\Core\Visitor\Tests\Fixtures\SmartExpressionToStringConverter;
use Cubiche\Core\Visitor\Tests\Fixtures\Sum;
use Cubiche\Core\Visitor\Tests\Fixtures\Value;
$expression1 = new Sum(new Value(5), new Mult(new Value(3), new Value(2)));
$expression2 = new Mult(new Value(4), $expression1);
$calculator = new Calculator();
$converter = new SmartExpressionToStringConverter();
echo $expression1->accept($converter) . ' = ' . $expression1->accept($calculator) . PHP_EOL;
echo $expression2->accept($converter) . ' = ' . $expression2->accept($calculator) . PHP_EOL;
Exemple #2
0
 /**
  * @param Mult $mult
  *
  * @return int|float
  */
 public function visitMult(Mult $mult)
 {
     return $mult->firstOperand()->accept($this) * $mult->secondOperand()->accept($this);
 }
Exemple #3
0
<?php

require 'vendor/autoload.php';
use Cubiche\Core\Visitor\Tests\Fixtures\Calculator;
use Cubiche\Core\Visitor\Tests\Fixtures\Evaluator;
use Cubiche\Core\Visitor\Tests\Fixtures\Mult;
use Cubiche\Core\Visitor\Tests\Fixtures\SmartExpressionToStringConverter;
use Cubiche\Core\Visitor\Tests\Fixtures\Sum;
use Cubiche\Core\Visitor\Tests\Fixtures\Value;
use Cubiche\Core\Visitor\Tests\Fixtures\Variable;
$expression = new Mult(new Variable('x'), new Sum(new Value(5), new Mult(new Variable('y'), new Value(2))));
$variables = array('x' => 10, 'y' => -7);
$evaluator = new Evaluator(new Calculator(), $variables);
$converter = new SmartExpressionToStringConverter();
echo $expression->accept($converter) . ' = ' . $expression->accept($evaluator) . PHP_EOL;