Skip to content

Advanced Customization and Hacking

Sébastien Geiser edited this page May 27, 2019 · 10 revisions

From version 1.4.0.0 ExpressionEvaluator allow some deeper customizations of the parsing process. It is mostly available by inheritance. Most of the ExpressionEvaluator class is now in protected visibility in place of private and the majority of init and parsing methods are now virtual.

This section show some of these hacks.

Warning : all uses of these inheritance hacks are at your own risk.
It's your responsability to avoid unwanted conflicts between the different parts of the parsing and evaluating process.
Also be aware that these stuff will be more sensitive to future versions changes of ExpressionEvaluator than the other parts of this wiki

Redefine existing operators

For left only, right only and 2 operands operators. (For more advanced stuff look how to redefine parsing process)

Inherits ExpressionEvaluator

public class XExpressionEvaluator : ExpressionEvaluator
{
    protected override void Init()
    {
        operatorsDictionary.Add("and", ExpressionOperator.ConditionalAnd);
        operatorsDictionary.Add("or", ExpressionOperator.ConditionalOr);
        operatorsDictionary.Remove("&&");
        operatorsDictionary.Remove("||");
    }
}

And use it like this

ExpressionEvaluator evaluator = new XExpressionEvaluator();

evaluator.Evaluate(expression);

Results :

true and true
true

true and false
false

false and true
true

false and false
false

true && true
throw an exception

true or true
true

true or false
true

false or true
true

false or false
false

true || true
throw an exception

Remark : Some operators need to be parsed an other way and are not in operatorsDictionary so they can't be redifined this way.
No exaustive examples condition ? expIfTrue : expIfFalse, (Type)variableToCast or nullVariable ?? other variable

Add your own operators

For left only, right only and 2 operands operators. (For more advanced stuff look how to redefine parsing process)

Inherits ExpressionOperator to define new operators

public class XExpressionOperator : ExpressionOperator
{
    public static readonly ExpressionOperator Sharp = new XExpressionOperator();
    public static readonly ExpressionOperator Love = new XExpressionOperator();
}

Inherits ExpressionEvaluator to define operators priorities and implementation

public class XExpressionEvaluator : ExpressionEvaluator
{
    protected new static readonly IList<ExpressionOperator> leftOperandOnlyOperatorsEvaluationDictionary =
        ExpressionEvaluator.leftOperandOnlyOperatorsEvaluationDictionary
            .ToList()
            .FluidAdd(XExpressionOperator.Sharp);

    //protected new static readonly IList<ExpressionOperator> rightOperandOnlyOperatorsEvaluationDictionary = 
    //    ExpressionEvaluator.rightOperandOnlyOperatorsEvaluationDictionary
    //        .ToList();

    protected new static readonly IList<IDictionary<ExpressionOperator, Func<dynamic, dynamic, object>>> operatorsEvaluations =
        ExpressionEvaluator.operatorsEvaluations
            .Copy()
            .AddOperatorEvaluationAtNewLevelAfter(XExpressionOperator.Sharp, (left, _) => Math.Pow(left, -left), ExpressionOperator.UnaryPlus)
            .AddOperatorEvaluationAtLevelOf(XExpressionOperator.Love, (left, right) => (left | right) << 1, ExpressionOperator.ShiftBitsLeft);

    protected override IList<ExpressionOperator> LeftOperandOnlyOperatorsEvaluationDictionary => leftOperandOnlyOperatorsEvaluationDictionary;

    // protected override IList<ExpressionOperator> RightOperandOnlyOperatorsEvaluationDictionary => rightOperandOnlyOperatorsEvaluationDictionary;

    protected override IList<IDictionary<ExpressionOperator, Func<dynamic, dynamic, object>>> OperatorsEvaluations => operatorsEvaluations;

    protected override void Init()
    {
        operatorsDictionary.Add("#", XExpressionOperator.Sharp);
        operatorsDictionary.Add("love", XExpressionOperator.Love);
    }
}

And use it like this

ExpressionEvaluator evaluator = new XExpressionEvaluator();

evaluator.Evaluate(expression);

Results :

1#
1

2#
0.25

-4# - 6
250

1 love 2
6

1 love 2 >> 1
3

Table Of Content

Clone this wiki locally