Expressions module

Provides syntax and lexical analyzers, and an expression calculator optimized for repeated calculations.

Packages

The module contains the following packages:

  • Calculator - expression calculator
  • CSV - CSV tokenizer
  • IO - input/output utility classes to support lexical analysis
  • Mustache - Mustache templating engine
  • Tokenizers - lexical analyzers to break incoming character streams into tokens
  • Variants - dynamic objects that can hold any values and operators for them

Use

Install the dotnet package as

dotnet add package PipServices3.Expressions

The example below shows how to use expression calculator to dynamically calculate user-defined expressions.

using PipServices3.Commons.Config;
using PipServices3.Commons.Refer;
using PipServices3.Commons.Run;
using PipServices3.Expressions.Calculator;
using PipServices3.Expressions.Calculator.Variables;
using PipServices3.Expressions.Variants;

ExpressionCalculator calculator = new ExpressionCalculator();

calculator.Expression = "A + b / (3 - Max(-123, 1)*2)";

VariableCollection vars = new VariableCollection();
vars.Add(new Variable("A", new Variant(1)));
vars.Add(new Variable("B", new Variant(3)));

Variant result = calculator.EvaluateUsingVariablesAsync(vars).Result;
Console.WriteLine("The result of the expression is " + result.ToString());
...