3nr1c / php-shunting-yard
php-shunting-yard
0.1.1
2015-09-13 08:35 UTC
Requires
- php: >=5.3.3
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-09-28 17:54:43 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
作者
原始源代码取自 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 提供的测试用例和重构,用于 composer/packagist。
通过 Composer 安装
在您的 composer.json 文件中定义以下要求
{ "require": { "andig/php-shunting-yard": "dev-master" } }
类加载
require_once "vendor/autoload.php";
手动安装
类加载
require_once "RR/Shunt/Context.php"; require_once "RR/Shunt/Parser.php"; require_once "RR/Shunt/Scanner.php"; require_once "RR/Shunt/Token.php"; require_once "RR/Shunt/Exception/ParseError.php"; require_once "RR/Shunt/Exception/RuntimeError.php"; require_once "RR/Shunt/Exception/SyntaxError.php";
MIT 许可证
特此授予任何获得本软件及其相关文档文件(“软件”)副本的任何人(“任何人”)免费使用软件的权利,不受限制地处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向软件提供副本的个人这样做,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定目的适用性和非侵权性保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任承担责任,无论是基于合同、侵权或其他原因,由此软件或其使用或其他方式而产生。