TinyExpr class

The TinyExpr class provides a lightweight expression parser and evaluator.

This class allows you to parse and evaluate mathematical expressions represented as strings. It supports basic arithmetic operations such as addition, subtraction, multiplication, and division, as well as parentheses for grouping.

Example usage:

var expr = TinyExpr('3 + 4 * 2 / (1 - 5)');
var result = expr.evaluate();
print(result); // Output: 1.0

The TinyExpr class can be useful in scenarios where you need to evaluate mathematical expressions dynamically at runtime, such as in calculators, scripting engines, or configuration files.

Features:

  • Supports basic arithmetic operations: +, -, *, /
  • Supports parentheses for grouping: (, )
  • Handles operator precedence and associativity

Limitations:

  • Does not support advanced mathematical functions (e.g., trigonometric functions)
  • Does not support variables or constants

Example:

var expr = TinyExpr('10 + (2 * 3) - 4 / 2');
var result = expr.evaluate();
print(result); // Output: 15.0

Note: This class is designed for simplicity and ease of use, and may not be suitable for complex mathematical expressions or performance-critical applications.

Constructors

TinyExpr(String expression, {bool powFromRight = false})
Creates a new TinyExpr instance with the given expression.

Properties

constants Map<String, double>
User-defined constants. for example: {'pi': 3.14159, 'e': 2.71828}
final
currentToken Token
The current token being processed during evaluation.
no setter
expression String
The input mathematical expression. for example: 3 + 4 * 2 / (1 - 5)
final
functions Map<String, Function>
User-defined functions. for example: {'f': (double x) => x * x} used to store the value of functions in the expression.
final
hashCode int
The hash code for this object.
no setterinherited
powFromRight bool
Determines the order of exponentiation. By default, exponentiation is left-associative (e.g., 2^3^4 is evaluated as (2^3)^4). If powFromRight is set to true, exponentiation is right-associative (e.g., 2^3^4 is evaluated as 2^(3^4)).
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
tokens List<Token>
The list of tokens generated from the input expression. for example: [Token.number(3), Token.operator('+'), Token.number(4), ...] used to store the tokens generated from the input expression during evaluation.
final
variables Map<String, double>
User-defined variables. for example: {'x': 5, 'y': 10} used to store the value of variables in the expression. use addVariables to add a new variables. use updateVariables to update the value of an existing variable.
final

Methods

addVariables(Map<String, double> variables) → void
Add a new variable with the given name and value.
evaluate() double
Evaluate the input expression and return the result. for example: 3 + 4 * 2 / (1 - 5) used to evaluate the input expression and return the result. The addVariables method is used to add new variables. The updateVariables method is used to update the value of an existing variable. The variables map is used to store the value of variables in the expression. The functions map is used to store the value of functions in the expression. The constants map is used to store the value of constants in the expression. The tokens list is used to store the tokens generated from the input expression. The currentToken getter is used to access the current token being processed.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
Returns a string representation of the TinyExpr instance. for example: TinyExpr('3 + 4 * 2 / (1 - 5)') used to return a string representation of the TinyExpr instance.
override
updateVariables(Map<String, double> variables) → void
Update the value of an existing variable with the given name and value.

Operators

operator ==(Object other) bool
The equality operator.
inherited