Giter Site home page Giter Site logo

expressionevaluator's Introduction

ExpressionEvaluator

C# Dynamic Expression Evaluator

This is a simple expression evaluation library targeting .NET

Usage

Usage of this library is fairly simple.

Step 1: Create and Initialize an Evaluation Context

    
    Context context = new Context();
    
    context.DeclareVariable("var1", typeof(int));
    context.DeclareVariable("var2", typeof(string));
    
    context.ExportType(typeof(Math));
    content.ExportType(typeof(DateTime));

Step 2: Compile an expression for evaluation

    CompiledExpression compiledExpr = new ExpressionCompiler(context).Compile("var1+=3");

Step 3: Set variables values to be used during evaluation at runtime

    ExpressionEvaluator evaluator = new ExpressionEvaluator(compiledExpr);
    evaluator.SetVariable("var1", 5);

Step 4: Evaluate the expression

    Func<int> func = evaluator.Evaluate<int>();
    func();

Examples

Simple binary expressions

"a".     //return value of variable 'a'
"a+1"    //return value of variable 'a' + 1 - add
"a-1"    //return value of variable 'a' - 1 - subtract
"a*2"    //return value of variable 'a' * 2 - multiply  
"a/2"    //return value of variable 'a' / 2 - divide
"a % 2"  //return value of variable 'a' % 2 - modulo
"a << 1" //return value of variable 'a' << 1 - left-shift
"a >> 1" //return value of variable 'a' >> 1 - right-shift
"a & 7"  //return value of variable 'a' & 1 - bitwise-and
"a | 2"  //return value of variable 'a' | 2 - bitwise-or
"a ^ a"  //return value of variable 'a' & 'a' - bitwise-xor

Simple assignment expressions - overwriting variable value

"a++"              //Assign value of variable 'a' with post increment expression
"a--"              //Assign value of variable 'a' with post decrement expression 
"--a"              //Assign value of variable 'a' with pre decrement expression 
"++a"              //Assign value of variable 'a' with pre increment expression 
"a+=1"             //Assign value of variable 'a' with add expression 
"a-=1"             //Assign value of variable 'a' with substract expression 
"a*=2"             //Assign value of variable 'a' with multiply expression 
"a/=2"             //Assign value of variable 'a' with divide expression
"a%=2"             //Assign value of variable 'a' with modulo expression 
"a<<=1"            //Assign value of variable 'a' with left-shift expression 
"a>>=1"            //Assign value of variable 'a' with right-shift expression 
"a&=7"             //Assign value of variable 'a' with bitwise-and expression  
"a|=7"             //Assign value of variable 'a' with bitwise-or expression
"a^=7"             //Assign value of variable 'a' with bitwise-xor expression
"a=(a+7)".         //Assign value of variable 'a' with a simple expression
"a=(a+(Int16)7)"   //Assign value of variable 'a' with a complex expression 
"a=Int32.MaxValue" //Assign a constant value to variable 'a'. Is also an example of Field or property access on exported type. Requires Int32 to be added to exported type

Simple logical expressions

"a < 7"           //Compare < between variable 'a' and int constant
"a <= 7"          //Compare <= between variable 'a' and int constant
"a == 6"          //Compare for equality between variable 'a' and int constant
"a != 6"          //Compare for not equal between variable 'a' and int constant
"a > 7"           //Compare > between variable 'a' and int constant
"a >= 7"          //Compare >= between variable 'a' and int constant
"a && 7"          //Logical and between variable 'a' and int constant
"a || 0"          //Logical or between variable 'a' and int constant
"true && true".   //Logical and between boolean constants
"true || false".  //Logical or between boolean constants
"a >= 7000000000" //Compare variable 'a' (int) with long constant

Object creation expression

"new DateTime(50000000000)"   //Create a new instance of DateTime based on Ticks (long). Requires DateTime to be added to ExportedTypes in Context
"a=new DateTime(50000000000)" //Create a new instance of DateTime and assign to variable 'a'. Requires DateTime to be added to ExportedTypes in Context

Method invocation expression

"a.AddTicks((Int64)100)".  //Invokes method of variable (or on Exported Type)
"a=a.AddTicks((Int64)100)" //Invokes method of variable and assign value to variable 'a'
"Int32.Parse(\"100\")".    //Invokes method on exported type. Requires Int32 to be added to ExportedTypes in Context

Other complex example expression

"String.Concat(obj[\"channel\"][\"item\"][0][\"title\"].ToString().Substring(0,8), suffix)" //Operates on variable 'obj' of type JObject and 'suffix' of type String
"Int32.Parse(((Int32)Math.Sqrt(a * a * 1.0)).ToString()) == 5" //Operates on variable 'a' of type int

Reference - Test Cases

Functional Tests

expressionevaluator's People

Contributors

sriksun avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.