Skip to content

Function

Muqsit Rayyan edited this page Sep 29, 2022 · 23 revisions

A function is identified by its unique name. Every function may have any number of non-negative inputs and a numeric output. The inputs and outputs may be either a float type, an int type or a number type (which is int|float).

Function Definition

A function may be registered for a given parser statically (before expressions are parsed). This can be done by invoking FunctionRegistry::register().

$registry = $parser->getFunctionRegistry();
$registry->register("intp1", static fn(int $x) : int => $x + 1);
$registry->register("num2", static fn(float $x) : float => $x * 2);
$registry->register("dist2d", static fn(float $x1, float $y1, float $x2, float $y2) : float => sqrt($x1 * $y1 + $x2 * $y2));

$parser->parse("int(2)")->evaluate(); // 3
$parser->parse("num2(num2(2))")->evaluate(); // 8
$parser->parse("dist2d(2, 3, 4, 5)")->evaluate(); // 5.0990195135927845

Functions accept any number of numeric inputs (including a varying number of inputs (variadic)), and return a numeric output. Below are some examples of valid function signatures:

  • fn() : int
  • fn() : float
  • fn() : int|float
  • fn(int $x, int|float $y) : int
  • fn(int ...$values) : float
  • fn(int $x, int $y, float ...$values) : float

A function may be registered as deterministic so that it is expected to return the same result for the same set of inputs (the meaning of deterministic here is the same as it's meaning in the context of SQL). To register a deterministic function, the deterministic parameter must be set to true at the time of registration.

$registry->register("sum", static fn(int|float $x, int|float $y) : int|float => $x + $y, true /* register this function as deterministic */);

Function calls to deterministic functions having deterministic inputs are computed during the parsing stage rather than the evaluation stage. Some of the example instances of this optimization are listed below:

  • The expression max(sum(2, 3), sum(4, 5)) is pre-computed as 9. The functions max() and sum() are invoked only during the parsing stage and no function call occurs during evaluation.
  • The expression max(sum(x, 3), sum(4, 5)) is pre-computed as max(sum(x, 3), 9). The function call sum(4, 5) is resolved during the parsing stage, while the other function calls are resolved during evaluation.
  • No optimization transformation occurs on the expression max(sum(x, y), sum(z, w)).

Function Call Syntax

A function call is made by mentioning the function name followed by an open and a close curve bracket containing the function arguments. Function arguments must be separated by a comma, such as fn(), fn(x), fn(x, y), etc. If no argument is supplied following a comma, the function's default value for the parameter is used.

For example, for the given function fn(int $x = 1, int $y = 2) : int => $x + $y:

  • fn() resolves to 3.
  • fn(2) resolves to 4.
  • fn(2, 3) resolves to 5.
  • fn(2, ) resolves to 4.
  • fn(,) resolves to 3.
  • fn(, 1) resolves to 2.

For the given function fn(int $x = 1, int $y) : int => $x + $y:

  • fn() throws a ParseException as the parameter $y does not have a default value.
  • fn(1) throws a ParseException as the parameter $y does not have a default value.
  • fn(1,) throws a ParseException as the parameter $y does not have a default value.
  • fn(, 1) resolves to 2.

List of available functions

Below is a list of functions provided by arithmexp out-of-the-box. These functions are directly ported from PHP's list of math functions and behave the same way.

1. Quick Start

1.1. Installation

2. Documentation Notes

2.1. Constant
2.2. Function
2.3. Macro
2.4. Operator
2.5. Variable
2.6. Expression Optimization
2.7. Implementation Internals

Clone this wiki locally