2017 © Pedro Peláez
 

library php-expressions

An extensible mathematical expression parser and evaluator.

image

xylemical/php-expressions

An extensible mathematical expression parser and evaluator.

  • Saturday, March 24, 2018
  • by rhys-mcguckin
  • Repository
  • 2 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

PHP Expressions.

Simple mathematical expression parser and calculator., (*1)

Install

The recommended way to install this library is through composer., (*2)

composer require xylemical/php-expressions

Usage

Most basic use of the parsing and evaluation classes:, (*3)

<?php

use Xylemical\Expressions\Math\BcMath;
use Xylemical\Expressions\Context;
use Xylemical\Expressions\ExpressionFactory;
use Xylemical\Expressions\Evaluator;
use Xylemical\Expressions\Lexer;
use Xylemical\Expressions\Parser;

$math = new BcMath();
$factory = new ExpressionFactory($math);
$lexer = new Lexer($factory);
$parser = new Parser($lexer);
$evaluator = new Evaluator();
$context = new Context();

$tokens = $parser->parse('1 + 1');
$result = $evaluator->evaluate($tokens, $context);

Variables.

Extending the expression factory to incorporate variable substitution involves adding a Value operator that will parse the variable, and use the values from the Context, (*4)

use Xylemical\Expressions\Token;
use Xylemical\Expressions\Value;

$factory->addOperator(new Value('\$[a-zA-Z_][a-zA-Z0-9_]*', function(array $operands, Context $context, Token $token) {
    return $context->getVariable(substr($token->getValue(), 1));
}));

$context->setVariable('example', 10);

$tokens = $parser->parse('2 * $example');
$result = $evaluator->evaluate($tokens, $context);

Functions

Extending the expression factory to incorporate more functions involves adding a Procedure operator that will parse the function name, and perform the expression substitution., (*5)

use Xylemical\Expressions\Token;
use Xylemical\Expressions\Procedure;

$factory->addOperator(new Procedure('ABS', 1, function(array $operands, Context $context, Token $token) {
    $value = $token->getValue();
    if (substr($value, 0, 1) === '-') {
        return substr($value, 1);
    }
    return $value;
}));

$tokens = $parser->parse('abs(-1.2)');
$result = $evaluator->evaluate($tokens, $context);

License

MIT, see LICENSE., (*6)

The Versions

24/03 2018

dev-master

9999999-dev https://github.com/xylemical/php-expressions

An extensible mathematical expression parser and evaluator.

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

math parser shunting-yard algorithm math calculator expression evaluator

24/03 2018

v1.0.2

1.0.2.0 https://github.com/xylemical/php-expressions

An extensible mathematical expression parser and evaluator.

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

math parser shunting-yard algorithm math calculator expression evaluator

24/03 2018

v1.0.1

1.0.1.0 https://github.com/xylemical/php-expressions

An extensible mathematical expression parser and evaluator.

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

math parser shunting-yard algorithm math calculator expression evaluator

23/03 2018

v1.0.0

1.0.0.0 https://github.com/xylemical/php-expressions

An extensible mathematical expression parser and evaluator.

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

math parser shunting-yard algorithm math calculator expression evaluator