expressions 0.1.2

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

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]
74
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
91
Overall:
Weighted score of the above. [more]
85
Learn more about scoring.

We analyzed this package on Apr 25, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14

Platforms

Detected platforms: Flutter, web, other

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

Health issues and suggestions

Document public APIs. (-0.12 points)

64 out of 66 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/src/expressions.dart. (-0.50 points)

Analysis of lib/src/expressions.dart reported 1 hint:

line 117 col 61: Use = to separate a named parameter from its default value.

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/parser.dart.

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

Maintenance suggestions

The package description is too short. (-9 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <3.0.0
petitparser ^2.0.0 2.2.1
quiver >=0.24.0 <3.0.0 2.0.3
Transitive dependencies
matcher 0.12.5
meta 1.1.7
path 1.6.2
stack_trace 1.9.3
Dev dependencies
test ^1.0.0