Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

yallie/Sprache.Calc

Repository files navigation

Appveyor build statusTests

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.

Instaco.de

Usage example

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()());

Installation

To use expression evaluator in your projects, installSprache.Calc NuGet packageby 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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp