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
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^4is evaluated as(2^3)^4). IfpowFromRightis set totrue, exponentiation is right-associative (e.g.,2^3^4is evaluated as2^(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. useaddVariablesto add a new variables. useupdateVariablesto update the value of an existing variable.final
Methods
-
addVariables(
Map< String, double> variables) → void -
Add a new variable with the given
nameandvalue. -
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. TheaddVariablesmethod is used to add new variables. TheupdateVariablesmethod is used to update the value of an existing variable. Thevariablesmap is used to store the value of variables in the expression. Thefunctionsmap is used to store the value of functions in the expression. Theconstantsmap is used to store the value of constants in the expression. Thetokenslist is used to store the tokens generated from the input expression. ThecurrentTokengetter 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
TinyExprinstance. for example:TinyExpr('3 + 4 * 2 / (1 - 5)')used to return a string representation of theTinyExprinstance.override -
updateVariables(
Map< String, double> variables) → void -
Update the value of an existing variable with the given
nameandvalue.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited