expressions 0.2.5+2 copy "expressions: ^0.2.5+2" to clipboard
expressions: ^0.2.5+2 copied to clipboard

A library to parse and evaluate simple dart and javascript like expressions.

❤️ sponsor

expressions #

Build Status

A library to parse and evaluate simple expressions.

This library can handle simple expressions, but no operations, blocks of code, control flow statements and so on. It supports a syntax that is common to most programming languages (so no special things like string interpolation, cascade notation, named parameters).

It is partly inspired by jsep.

Usage #

Example 1: evaluate expression with default evaluator

// Parse expression:
Expression expression = Expression.parse("cos(x)*cos(x)+sin(x)*sin(x)==1");

// Create context containing all the variables and functions used in the expression
var context = {
  "x": pi / 5,
  "cos": cos,
  "sin": sin
};

// Evaluate expression
final evaluator = const ExpressionEvaluator();
var r = evaluator.eval(expression, context);


print(r); // = true
copied to clipboard

Example 2: evaluate expression with custom evaluator

// Parse expression:
Expression expression = Expression.parse("'Hello '+person.name");

// Create context containing all the variables and functions used in the expression
var context = {
  "person": new Person("Jane")
};

// The default evaluator can not handle member expressions like `person.name`.
// When you want to use these kind of expressions, you'll need to create a
// custom evaluator that implements the `evalMemberExpression` to get property
// values of an object (e.g. with `dart:mirrors` or some other strategy).
final evaluator = const MyEvaluator();
var r = evaluator.eval(expression, context);


print(r); // = 'Hello Jane'
copied to clipboard

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Creating and maintaining this package takes a lot of time. If you like the result, please consider to ❤️ sponsor. With your support, I will be able to further improve and support this project. Also, check out my other dart packages at pub.dev.

90
likes
125
points
1.39k
downloads

Publisher

verified publisherappsup.be

Weekly Downloads

2024.06.21 - 2025.01.03

A library to parse and evaluate simple dart and javascript like expressions.

Homepage

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

meta, petitparser, quiver, rxdart

More

Packages that depend on expressions