Skip to content

Easy to use extensible calculator for .NET. Demonstrates Sprache toolkit grammar inheritance.

License

Notifications You must be signed in to change notification settings

yallie/Sprache.Calc

Repository files navigation

Sprache.Calc

Appveyor build status Tests

This library provides easy to use extensible expression evaluator based on LinqyCalculator sample. The evaluator supports arithmetic operations, custom functions and parameters. It takes string representation of an expression and converts it to a structured LINQ expression instance which can easily be compiled to an executable delegate. In contrast with interpreted expression evaluators such as NCalc, compiled expressions perform just as fast as native C# methods.

Instaco.de

Usage example

var calc = new Sprache.Calc.XtensibleCalculator();

// using expressions
var expr = calc.ParseExpression("Sin(y/x)", x => 2, y => System.Math.PI);
var func = expr.Compile();
Console.WriteLine("Result = {0}", func());

// custom functions
calc.RegisterFunction("Mul", (a, b, c) => a * b * c);
expr = calc.ParseExpression("2 ^ Mul(PI, a, b)", a => 2, b => 10);
Console.WriteLine("Result = {0}", expr.Compile()());

// end-user's functions
calc.RegisterFunction("Add", "a + b", "a", "b");
expr = calc.ParseExpression("Add(353, 181)");
Console.WriteLine("Result = {0}", expr.Compile()());

Installation

To use expression evaluator in your projects, install Sprache.Calc NuGet package by running the following command in the Package Manager Console:

PM> Install-Package Sprache.Calc

Grammar inheritance technique

Sprache.Calc library serves as a demonstration of grammar inheritance technique with Sprache toolkit. An article describing Sprache.Calc implementation details is currently available in English and Russian:

TL;DR:

  • Declare parsers as virtual properties instead of static fields
  • Decompose the grammar into small and reusable rules
  • Write unit tests for every single atomic parser
  • Use "protected internal" access modifier to enable unit testing
  • Unit tests must be organized in the same hierarchy as parser classes