waughj/math-parser

简单的Lisp-like数学解析器。

v0.1.0 2019-12-02 21:44 UTC

This package is auto-updated.

Last update: 2024-09-29 05:28:22 UTC


README

简单的Lisp-like数学解析器。

示例

use WaughJ\MathParser\MathParser;

$math = new MathParser();
echo( $math->parse( '(+ 2 2)' ) );

打印 "4"。

要使用不同的解析器

use WaughJ\MathParser\MathParser;

$math = new MathParser( new LisphpParser( [ ',' ] ) );
echo( $math->parse( '(*,2,5)' ) );

打印 "10"。

要创建一个新自定义函数

use WaughJ\MathParser\MathParser;
use WaughJ\MathParser\MathParserExceptionInvalidFunctionCall;

$math = new MathParser();
$math->addFunction
(
    'double',
    function( array $args )
    {
        $arg_count = count( $args );
        if ( $arg_count < 1 )
        {
            throw new MathParserExceptionInvalidFunctionCall
            (
                "Call to function “double” given no arguments. Needs at least 1."
            );
        }
        return array_shift( $args ) * 2;
    }
);
echo( $math->parse( '(double 222)' ) );

打印 "444"。

变更日志

0.1.0

  • 初始稳定版本。