Commit d3ee8b7f authored by Anton Smirnov's avatar Anton Smirnov
Browse files

Explain new things

parent ed12c98e
Loading
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -17,4 +17,5 @@ Evaluates the expression with given variables (if any).
``Calculator::evaluate()``
==========================

``Calculator::evaluate($expression, ...$vars)`` is a shorthand for ``Calculator::parse($expression)->calc(...$vars)``
``Calculator::evaluate($expression, $config, ...$vars)`` is a shorthand for
``Calculator::parse($expression, $config)->calc(...$vars)``
+54 −25
Original line number Diff line number Diff line
Configuration
#############

``Arokettu\ArithmeticParser\Config`` and ``Arokettu\ArithmeticParser\ConfigBuilder``
classes are used to configure the calculator and the parser (in future).
.. highlight:: php

Config is an immutable object and ConfigBuilder is a user-friendly way to set up options.

ConfigBuilder
=============

``build()``
-----------
``Arokettu\ArithmeticParser\Config`` class is used to configure the calculator and the parser (in future).

A method to create a Config object with configured parameters.
Config is an immutable object and ConfigBuilder is a user-friendly way to set up options.

``ConfigBuilder::default()``
----------------------------
``Config::default()``
=====================

The default preset used when no config is specified.

``ConfigBuilder::defaultConfig()``
----------------------------------

A prebuilt instance of the Config object for the default preset.
A shortcut for ``ConfigBuilder::default()->build()`` with a cached instance.

Functions
=========

The only configurable thing for now is a set of functions.
The function must be a callable that accepts a single float argument.

Default functions:
@@ -57,9 +43,52 @@ Default functions:

You can:

* Replace functions with your own list:
  ``$builder->setFunctions(['myfunc2' => fn ($a) => a ** 2]);``
* Add new functions:
  ``$builder->addFunctions(['myfunc3' => fn ($a) => a ** 3]);``
* Remove functions:
  ``$builder->removeFunctions('acos', 'asin');``
* Replace functions with your own list::

    <?php
    $config->setFunctions(myfunc2: fn ($a) => a ** 2);
* Add new functions::

    <?php
    $config->addFunctions(myfunc3: fn ($a) => a ** 3);
* Remove functions::

    <?php
    $config->removeFunctions('acos', 'asin');

Operators
=========

Operators can be unary and binary.
Operator symbol can be any string without digits.
Be wary when using latin character based operators, they are case-sensitive and may shadow variables and functions.

Default operators:

* ``+``, ``-`` in both unary and binary form. They are built-in and are not configurable.
* ``*``, ``/``.

You can:

* Replace operators with your own list::

    <?php
    $config->setOperators(
        new BinaryOperator('×', fn ($a, $b) => $a * $b, BinaryOperator::PRIORITY_MUL),
        new BinaryOperator('÷', fn ($a, $b) => $a / $b, BinaryOperator::PRIORITY_MUL),
    );

* Add new operators::

    <?php
    // assume you have factorial defined
    $config->addOperators(
        new BinaryOperator('^', pow(...), BinaryOperator::PRIORITY_POW, BinaryAssoc::RIGHT),
        new UnaryOperator('!', factorial(...), UnaryPos::POSTFIX),
    );

* Remove operators::

    <?php
    // you cannot divide by zero if you cannot divide
    $config->removeOperators('/');
+3 −2
Original line number Diff line number Diff line
@@ -6,12 +6,13 @@ Supported Elements

Supported expressions can include:

* Unary ``+`` and ``-``.
* Binary ``+``, ``-``, ``*``, ``/``.
* Unary operators ``+`` and ``-`` with any custom operators added.
* Binary ``+``, ``-``, ``*``, ``/`` with any custom operators added. ``*`` and ``/`` can be disabled.
* Numbers, obviously.
  All numbers are cast to float internally.
* Functions.
  Function names are case insensitive alphanumeric strings that do not start with a number.
  They may have an optional ``@`` prefix.
  Functions accept a single parameter.
  Custom functions can be created with a Config object.
* Variables.