expressions 0.1.2

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 73

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

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'

Features and bugs

Please file feature requests and bugs at the issue tracker.

Changelog

0.1.2

  • Support Dart 2 in pubspec.yaml
  • Use the petitparser version 2 package with fully typed parsers instead of the typedparser package

0.1.1

  • Dart 2 strong mode fixes
  • Evaluate right expression in a binary expression only when necessary

0.1.0

  • Initial version

example/expressions_example.dart

import 'package:expressions/expressions.dart';
import 'dart:math';

main() {
  example_1();
  example_2();
}

// Example 1: evaluate expression with default evaluator
example_1() {
  // 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

}

// Example 2: evaluate expression with custom evaluator
example_2() {
  // 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'

}

class Person {
  final String name;

  Person(this.name);

  Map<String,dynamic> toJson() => {"name": name};
}

class MyEvaluator extends ExpressionEvaluator {
  const MyEvaluator();

  @override
  evalMemberExpression(MemberExpression expression, Map<String, dynamic> context) {
    var object = eval(expression.object, context).toJson();
    return object[expression.property.name];
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  expressions: ^0.1.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:expressions/expressions.dart';
  
Version Uploaded Documentation Archive
0.1.2 Aug 14, 2018 Go to the documentation of expressions 0.1.2 Download expressions 0.1.2 archive
0.1.1 Apr 15, 2018 Go to the documentation of expressions 0.1.1 Download expressions 0.1.1 archive
0.1.0 Mar 29, 2018 Go to the documentation of expressions 0.1.0 Download expressions 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
55
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
73
Learn more about scoring.

We analyzed this package on Oct 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.4

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:expressions/expressions.dart.

Health suggestions

Format lib/expressions.dart.

Run dartfmt to format lib/expressions.dart.

Format lib/src/evaluator.dart.

Run dartfmt to format lib/src/evaluator.dart.

Format lib/src/expressions.dart.

Run dartfmt to format lib/src/expressions.dart.

Format lib/src/parser.dart.

Run dartfmt to format lib/src/parser.dart.

Maintenance suggestions

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <3.0.0
petitparser ^2.0.0 2.0.2
quiver >=0.24.0 <3.0.0 2.0.0+1
Transitive dependencies
matcher 0.12.4
meta 1.1.6
path 1.6.2
stack_trace 1.9.3
Dev dependencies
test ^1.0.0