yallie / Sprache.Calc

Easy to use extensible calculator for .NET. Demonstrates Sprache toolkit grammar inheritance.
http://www.codeproject.com/Articles/795056/Sprache-Calc
MIT License
45 stars 12 forks source link
c-sharp calc calculator compiler expression-evaluator grammar sprache

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: