expressions 0.1.1

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

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.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.1"

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.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

Analysis

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

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
0 / 100
Health:
Code health derived from static analysis. [more]
99 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
50
Learn more about scoring.

Platforms

Detected platforms: Flutter, web, other

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

Suggestions

  • The description is too short.

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

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 4 hints.

    Run dartfmt to format lib/expressions.dart.

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

    Similar analysis of the following files failed:

    • lib/src/expressions.dart (hint)
    • lib/src/parser.dart (hint)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <2.0.0
quiver >=0.24.0 <0.30.0 0.29.0+1
typedparser ^0.1.1 0.1.2
Transitive dependencies
matcher 0.12.3
meta 1.1.5
path 1.6.1
petitparser 1.7.6
stack_trace 1.9.2
Dev dependencies
test ^0.12.0