pmishev / php-shunting-yard
重构重打包自 https://github.com/andig/php-shunting-yard
dev-master
2017-12-26 23:19 UTC
Requires
- php: >=5.3.3
Requires (Dev)
- phpunit/phpunit: ^4.0||^5.0||^6.0
This package is auto-updated.
Last update: 2024-09-10 01:01:29 UTC
README
示例
简单方程解析
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
安装
在您的 composer.json 文件中定义以下需求
{ "require": { "andig/php-shunting-yard": "dev-master" } }
作者
原始源代码来自 https://github.com/droptable/php-shunting-yard,一些修改来自
- https://github.com/andig/php-shunting-yard
- https://github.com/pmishev/php-shunting-yard
- https://github.com/falahati/php-shunting-yard
测试用例和重构由 https://github.com/sergej-kurakin/php-shunting-yard 完成。