<commit_before>ExpressionEvaluator is a library to help developers evaluate C# and VB .NET expressions. The expressions you need to evaluate are compiled through the .NET Framework's own CodeDOM so nearly all language features are supported. The library exposes late-bound host objects to the expressions that add a scripting-like capability.

ExpressionEvaluator is written in C#.

# History and Errata

ExpressionEvaluator was written to help parse Visual Basic expressions in SQL Server Reporting Services reports. In particular, the objective was to produce a library that would produce the same output as Microsoft's own report server. To this effect the library automatically adds imports and references to some assemblies and types. The relevant section of the RDL Specification is reproduced below:

> ## Built-in References
 
> Expressions may reference types from the following assemblies: `Microsoft.VisualBasic.dll`, `System.dll`, `mscorlib.dll` and `Microsoft.SqlServer.Types.dll`.

> For convenience, the following namespaces are imported: `Microsoft.VisualBasic`, `System` and `Microsoft.SqlServer.Types`.

> In addition, the following types are imported: `System.Convert` and `System.Math`.

In the interests of producing a generic evaluator `Microsoft.SqlServer.Types` is not imported or referenced. Further, the library is also capable of evaluating any expression that has a `CodeDomProvider` available for it - not just VB .NET<sup>1</sup>.

ExpressionEvaluator's API allows for general purpose expression evaluation, but suited towards bulk evaluation<sup>2</sup>. To use ExpressionEvaluator efficiently it's required that all expressions are added first, and then compiled before being evaluated.

Currently expressions are compiled and executed within the context of the calling assembly; sandboxing expression execution is planned for a later release.

<sup>1</sup> In fact, the default language for the library is C#.

<sup>2</sup> This design was chosen because all possible expressions are known up-front in a Reporting Services report.

# Example

    class MyObject
    {
        public string Phrase = "Hello";
    }

    class Program
    {
        static IEnumerable<string> Expressions =
            new List<string>
            {
                "2 * 5",
                "Log10(50)",
                "HostObject.Phrase",
                "\"World\""
            };

        static void Main(string[] args)
        {
            using (var evaluator = new ExpressionEvaluator("VisualBasic"))
            {
                // Configure out host object for the expressions to reference.
                var hostObject = new MyObject();
                evaluator.HostObjects.Add("HostObject", hostObject);

                // Add each expression that needs to be compiled.
                foreach (var expression in Expressions)
                    evaluator.Expressions.Add(expression);

                // Compile each expression. This will also "associate" any host objects.
                evaluator.Compile();

                // Evaluate the expressions.
                foreach (var expression in Expressions)
                    Console.WriteLine(evaluator.Evaluate(expression).ToString());

                // Even though expressions are compiled, they can still be dynamic.
                hostObject.Phrase = "!";
                Console.WriteLine(evaluator.Evaluate("HostObject.Phrase"));

                // If you have simple requirements, you can also perform ad-hoc evaluation.
                Console.WriteLine(ExpressionEvaluator.EvaluateAdHoc("\"But it might be slower\""));
            }
        }
    }

Output:

    10
    1.69897000433602
    Hello
    World
    !
    But it might be slower<commit_msg>Updating README.md - Fixes #8.<commit_after>ExpressionEvaluator is a library to help developers evaluate C# and VB .NET expressions. The expressions you need to evaluate are compiled through the .NET Framework's own CodeDOM so nearly all language features are supported. The library can expose remotable objects to the expressions for a scripting-like capability. All expression evaluation is sandboxed.

ExpressionEvaluator is written in C#.

# History and Errata

ExpressionEvaluator was written to help parse Visual Basic expressions in SQL Server Reporting Services reports. In particular, the objective was to produce a library that would produce the same output as Microsoft's own report server. To this effect the library automatically adds imports and references to some assemblies and types. The relevant section of the RDL Specification is reproduced below:

> ## Built-in References
 
> Expressions may reference types from the following assemblies: `Microsoft.VisualBasic.dll`, `System.dll`, `mscorlib.dll` and `Microsoft.SqlServer.Types.dll`.

> For convenience, the following namespaces are imported: `Microsoft.VisualBasic`, `System` and `Microsoft.SqlServer.Types`.

> In addition, the following types are imported: `System.Convert` and `System.Math`.

In the interests of producing a generic evaluator `Microsoft.SqlServer.Types` is not imported or referenced. Further, the library is also capable of evaluating any expression that has a `CodeDomProvider` available for it - not just VB .NET.

ExpressionEvaluator's API allows for general purpose expression evaluation, but suited towards bulk evaluation<sup>1</sup>. To use ExpressionEvaluator efficiently it's required that all expressions are added first, and then compiled before being evaluated.

<sup>1</sup> This design was chosen because all possible expressions are known up-front in a Reporting Services report.

# Example

```csharp
static void Main(string[] args)
{
    var expressions = new List<string>
                            {
                                "3 * 5",
                                "Log10(50)",
                                "Parameters!Greeting + \" World!\""
                            };

    // An ExpressionMeta contains the expressions and extensions to be compiled.
    var meta = new ExpressionMeta("VisualBasic");

    // Add the expressions to be compiled.
    foreach(var expression in expressions)
        meta.AddExpression(expression);

    // Add the extensions to be compiled.
    var extension = new Dictionary<string, string> {{"Greeting", "Hello"}};
    meta.AddExtensionIgnoreAssembly(new Extension("Parameters", extension));

    // Compile the expressions
    using(var evaluator = meta.Compile())
    {
        // Evaluate the expression
        foreach(var expression in expressions)
            Console.WriteLine("{0}", evaluator.Evaluate(expression));
    }
}
```

Output:

```
15
1.69897000433602
Hello World!
```
{'fraction_non_alphanumeric': 0.07056541846934208, 'fraction_numerical': 0.008056094286140535, 'mean_word_length': 3.0023880597014925, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5743634', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1515, 'n_words': 700}