prot1vogas / expression-parser
Simple expression parser
0.3.0
2022-02-11 23:56 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: ~4.8
This package is not auto-updated.
Last update: 2024-05-04 07:33:37 UTC
README
Simple expression parser.
Compatible with PHP 5.4 and up.
Basic usage
$parser = new Parser(new TokenTypesFactory(), new Lexer());
$evaluator = new Evaluator();
$expression = $parser->parse("2 + 2");
echo $evaluator->execute($expression);
Features
- binary operations + - * / % ^
- unary operations - sqrt ! sin cos tan
- brackets
- float variables
- constants pi, e
You can override TokenTypesFactory to add custom operations, variables or brackets. There are some classes of token types:
- BinaryOperation
- UnaryOperation
- Variable
- Brackets
For example, new operation 'func' may be added in method getUnaryOperations
like this:
$operations[] = new UnaryOperation('func', function ($args) {
return $args[0] / 2;
}, 15, UnaryOperation::PREFIX_NOTATION);