Giter VIP home page Giter VIP logo

sprache.calc's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.