- Notifications
You must be signed in to change notification settings - Fork10
Easy to use extensible calculator for .NET. Demonstrates Sprache toolkit grammar inheritance.
License
yallie/Sprache.Calc
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This library provides easy to use extensible expression evaluator based onLinqyCalculator sample.The evaluator supports arithmetic operations, custom functions and parameters. It takes stringrepresentation of an expression and converts it to a structured LINQ expression instancewhich can easily be compiled to an executable delegate. In contrast with interpreted expressionevaluators such as NCalc, compiled expressions perform just as fast as native C# methods.
varcalc=newSprache.Calc.XtensibleCalculator();// using expressionsvarexpr=calc.ParseExpression("Sin(y/x)", x=>2, y=>System.Math.PI);varfunc=expr.Compile();Console.WriteLine("Result = {0}",func());// custom functionscalc.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 functionscalc.RegisterFunction("Add","a + b","a","b");expr=calc.ParseExpression("Add(353, 181)");Console.WriteLine("Result = {0}",expr.Compile()());
To use expression evaluator in your projects, installSprache.Calc NuGet packageby running the following command in the Package Manager Console:
PM> Install-Package Sprache.CalcSprache.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:
- Sprache.Calc: building yet another expression evaluator
- Наследование грамматик в Sprache на примере калькулятора выражений.
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
About
Easy to use extensible calculator for .NET. Demonstrates Sprache toolkit grammar inheritance.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.