Skip to content
Ahmed Elmalt edited this page Jul 13, 2019 · 5 revisions

XPress is a simple yet powerful expression language for .net string literals. Make string truth expressions in compile time using binary, unary, relational, and conditional operators. XPress compiles expressions into an optimized Func<XpressRuntimeContext, bool> where it can be evaluated in runtime using variable values. XPress supports numerical, text, boolean, null, and variable operands.

Table of Contents

The Basics

Create XpressCompiler object

// Direct instantiation using constructor
XpressCompiler _compiler = new XpressCompiler();
        
// Using Singleton Default instance method
XpressCompiler _compiler = XpressCompiler.Default;

Create a string expression, then compile it into compiled .net lambda using Compile() method

var expression = "x gt y";
var compilationResult = _compiler.Compile(code);

The XpressCompilationResult object contains Code lambda of type Func<XpressRuntimeContext, bool>. The Func expect input of XpressRuntimeContext a map of <string,string> to pass variables values to Func at execution

// Create XpressRuntimeContext object with variable names and values
XpressRuntimeContext runtimeCtx = new XpressRuntimeContext() { { "x", "10" }, { "y", "9" } };
        
// Call compiled Func with runtimecontext
var result = compilationResult.Code(runtimeCtx);

The Return result object is a boolean value of expression evaluated

Assert.True(result);

The XpressCompilationResult object also offers boolean Flag Compiled will be set to false in case compiler failed to compile the expression. A Log property on XpressCompilationResult object will offer compilation logging trail

Assert.True(compilationResult.Compiled);
Assert.False(compilationResult.Log.HasErrors);

Variables

  • variables literals can be used anywhere within an expression
  • The variable name follows same C# variable literals naming constraints
    • Must start with a letter, or _
    • May contain Unicode letter characters, decimal digit characters, Unicode connecting characters, Unicode combining characters, or Unicode formatting characters.
var e = "y eq x+2*2-4/2";

Operands

Boolean Operands true, false

// simple operand will evaluate to true
var experssion = "true";
        
// simple operand will evaluate to false
var experssion = "false";

Numerical Operands: supports int32 numbers.

var e = "9 gt 10";

Text Operands: supports .net Strings in a single quotes

var e = " 'Ahmed' eq 'ahmed'";

null creates a null object can be used in comparisons with strings and variables.

var e = " 'Ahmed' ne null";

Expressions

Binary Expressions

Mathematical Operators +, -, *, /, % is supported on Numerical operands.

var experssion = "x eq (1+2)";

+ operator can be applied to string operands as a concatenation function.

var e = "x eq ('ahmed '+'elmalt')";

- operator can be applied to string operands as a subtraction function.

var e = "'hmed' eq ('ahmed' - 'a')";

Binary operators can't be applied to mixed types of operands.

Unary Expressions

not Unary Logical Not can be applied only to Boolean Operands, Boolean Variable, or Boolean Expressions

// boolean operands
var e1 = "not false";
        
// boolean variables, the value of x must be set to a boolean value at runtime or runtime error
var e2 = "not x";
        
// boolean expression
var e3 = "not ('a' eq 'b' and 9 gt 10)";

Relational Expressions

  • eq Equal
  • ne Not Equal
  • gt Greater Than
  • lt Less Than
  • ge Greater Than or Equal
  • le Less Than or Equal
var e = "1 eq 4";

Conditional Expressions

  • or Logical OR
  • and Logical AND
var e = "false or x lt 2";

Operator Precedence

  • Parentheses: ()
  • Unary Logial Operators: not
  • Binary Multiplicative Operators: *, / , %
  • Binary Additive Operators:+, -
  • Equality Operators: eq, ne
  • Logical Operators: gt, lt, ge. le
  • Conditional Operators: and, or

XPress is very powerful and can be used to compose sophisticated boolean expressions as strings.

var e = "(x ne null and x+1 gt 10) or (y ne null and (y*(5+1)-2) lt 5)";