Giter VIP home page Giter VIP logo

php-expressions's Introduction

PHP Expressions.

Simple mathematical expression parser and calculator.

Install

The recommended way to install this library is through composer.

composer require xylemical/php-expressions

Usage

Most basic use of the parsing and evaluation classes:

<?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

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.

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.

php-expressions's People

Contributors

btry avatar perturbatio avatar rhys-mcguckin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.