tmteam / NFun

Expressions Evaluator for .NET
MIT License
57 stars 4 forks source link
automation calculations embedded eval evaluator execute executescript expression-evaluator expression-parser interoperability math mathematics parser reflection script script-language scripting type-inference

NFun. Expressions Evaluator for .NET

To install NFun, run the following command in the Package Manager Console:

PM> Install-Package NFun 

What is the NFun?

This is an expression evaluator or a mini-script language for .net. It supports working with mathematical expressions as well as with collections, strings, hi-order functions and structures. NFun is quite similar to NCalc but with a rich type system and linq support. See the 'How to / specifications' section for details

Nfun can perform simple evaluations

  double d = Funny.Calc<double>(" 2 * 10 + 1 ") // 21  
  bool b   = Funny.Calc<bool>("false and (2 > 1)") // false

  // 'age' and 'name' are properties from input 'User' model 
  string userAlias = Funny.Calc<User,string> (
                       "if(age < 18) name else 'Mr. {name}' ", 
                        inputUser)  

as well as complex, with multiple composite inputs and outputs

  // Evaluate many values and set them into 'Person' object's properties 
  // inputs and outputs 'age', 'cars' and 'birthYear' are properties of 'Person' object 
  var personModel = new Person(birthYear: 2000);

  Funny.CalcContext(@"   
     age = 2022 - birthYear 
     cars = [
        { name = 'lada',   cost = 1200, power = 102 },
        { name = 'camaro', cost = 5000, power = 275 }
     ]
     ", personModel);

  Assert.Equal(22,   personModel.Age);
  Assert.Equal(2,    personModel.Cars.Count());
  Assert.Equal(1200, personModel.Cars[0].Cost);

Low-level hardcore API is also supported

  var runtime = Funny.Hardcore.Build("y = 2x+1");

  runtime["x"].Value = 42; //Write input data
  runtime.Run(); //Run script
  var result = runtime["y"].Value //collect results

  Console.WriteLine("Script contains these variables:"
  foreach(var variable in runtime.Variables)
     Console.WriteLine(
        "{variable.Name}:{variable.Type} {variable.IsOutput?"[OUTPUT]":"[INPUT]"}");

Key features

How to

API - guide and examples

Syntax - guide and examples

Built in functions


Boring specification is better than no specification

Boring specification: Basics

Boring specification: Operators

Boring specification: Arrays

Boring specification: Texts (Strings)

Boring specification: Structs

Boring specification: Rules (Anonymous functions)

Boring specification: Types

     ';,                                                                                ;;      
   'lO0l                                                                               'dKkc    
  c0Xk;                                                                                  cOXk:  
'dXKc                 ';;;,'                                        ',;;;'                'dXKl 
dNKc                     ',;;;;,'                              ',;;;;,'                     oXXl
XNo                          '',;;;,'                      ',;;;,''                         'kW0
WK:                               ,:::,                  ,:::,                               lNX
W0;                            ',;;;;,                    ,;;;;,'                            cXN
W0;                        ',;;;,'                            ',;;;,'                        lXN
NXl                    ,;;;;,'                                    ',;;;;,                    dWK
OWO,                  ','        ',;;;,                  ,;;;,'        ','                  :KNd
:0Nk,                        ',;;;,'                        ',;;;,'                        :0Nx,
 ,xX0c                  ',;;;,,'                                ',,;;;,'                 'oKKo' 
   cOKO:               ,;,'                                          ',;,               l0Kx;   
     :dc                                                                               'lo;     

                                     ,;;,              ';;,                                     
                                    ;:;;:,            ,:;;:;                                    
                                  ';:,  ,:;          ;:,  ,:;'                                   
                                 ';:'    ,:;'      ';:,    '::'                                  
                                 ',       ','      ','      ','