Getting Started

This page demonstrates how to quickly get started in creating and evaluating expressions. You'll need to add a reference to Ciloci.Flee.dll and import the Ciloci.Flee namespace to be able to compile the code.

// Define the context of our expression
ExpressionContext context = new ExpressionContext();
// Allow the expression to use all static public methods of System.Math

// Define an int variable
context.Variables["a"] = 100;

// Create a dynamic expression that evaluates to an Object
IDynamicExpression eDynamic = context.CompileDynamic("sqrt(a) + pi");
// Create a generic expression that evaluates to a double
IGenericExpression<double> eGeneric = context.CompileGeneric<double>("sqrt(a) + pi");

// Evaluate the expressions
double result = (double)eDynamic.Evaluate();
result = eGeneric.Evaluate();

// Update the value of our variable
context.Variables["a"] = 144;
// Evaluate again to get the updated result
result = eGeneric.Evaluate();

The ExpressionContext holds all the information required for an expression (besides the expression text). We use its Imports property to make all the static members of the System.Math class available to the expression and the Variables property to dynamically define some variables. With the context defined, we compile the expression text to create a dynamic expression (evaluates to Object) or a generic expression (type-safe and fast) and finally call the Evaluate method on either expression to get its result.

Invalid expressions
Expressions can fail to compile if, for example, the syntax of the text is invalid. An ExpressionCompileExpression will be thrown whenever an expression cannot be compiled.

   ExpressionContext context = new ExpressionContext();
   IDynamicExpression e = context.CompileDynamic("100 +* 200");
catch (ExpressionCompileException ex)
   // Handle expression compile error
   if (ex.Reason == CompileExceptionReason.SyntaxError)
      Console.WriteLine("Check your expression syntax");

Last edited Nov 9, 2008 at 10:05 PM by ECiloci, version 22


0x69 Jun 13, 2012 at 11:04 AM 
For that you need custom functions and a bit of black magic:
public static class CustomFunctions{
public static double isZero(double x) {
return (x == 0.0) ? 1.0 : 0.0;

class Program {

static void Main(string[] args) {
ExpressionContext context = new ExpressionContext();
IDynamicExpression eDynamic;
double r1, r2;

context.Variables.Add("a1", 0.0);
context.Variables.Add("a2", 0.0);
eDynamic = context.CompileDynamic("isZero(a2)*1.0 + (1.0-isZero(a2))*(a1/(a2+isZero(a2)))");

context.Variables["a1"] = 1.0;

// a2 zero
context.Variables["a2"] = 0.0;
r1 = (double)eDynamic.Evaluate();

// a2 not zero
context.Variables["a2"] = 10.0;
r2 = (double)eDynamic.Evaluate();

Console.WriteLine("{0}\n{1}", r1, r2);

bhrnjica May 11, 2008 at 10:03 AM 
Hi, your lib is great, but i have one quest,
I need division with check for arg2: if(a2==0, 1,a1/a2)
how can i achieve this