PHP code example of freezewarp / php-shunting-yard
1. Go to this page and download the library: Download freezewarp/php-shunting-yard library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
freezewarp / php-shunting-yard example snippets
use RR\Shunt\Parser;
$equation = '3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3';
$result = Parser::parse($equation);
echo $result; //3.0001220703125
use RR\Shunt\Parser;
use RR\Shunt\Context;
$ctx = new Context();
$ctx->def('abs'); // wrapper for PHP "abs" function
$ctx->def('foo', 5); // constant "foo" with value "5"
$ctx->def('bar', function($a, $b) { return $a * $b; }); // define function
$equation = '3 + bar(4, 2) / (abs(-1) - foo) ^ 2 ^ 3';
$result = Parser::parse($equation, $ctx);
echo $result; //3.0001220703125
use RR\Shunt\Parser;
use RR\Shunt\Context;
$ctx = new Context();
$ctx->def('foo', 5); // constant "foo" with value "5"
$equation = '(foo > 3) & (foo < 6)';
$result = Parser::parse($equation, $ctx);
echo $result; //true
use RR\Shunt\Parser;
use RR\Shunt\Context;
$counter = 1;
$ctx = new Context();
$ctx->def('data', function() { global $counter; return $counter++; }); // define function
$ctx->def('bar', function($a) { return 2*$a; }); // define function
$equation = 'bar(data())';
$parser = new Parser(new Scanner($equation));
$result = $parser->reduce($this->ctx); // first result
echo $result; // 2
$result = $parser->reduce($this->ctx); // second result
echo $result; // 4
new class($array) extends Context
{
public function cs($name) {
return your_array_getter($this->constants, $name);
}
}
json
{
"reezewarp/php-shunting-yard": "dev-master"
}
}