This project is read-only.
Project Description
Mathematical Expression Evaluation with symbols and normal bodmas order of precedence. Supports basic arithmetic operations and access to a symbol dictionary


Takes a string expression and return a double value using the standard order of operations usually used in mathematics and many computer programming languages: i.e.
  • exponents and roots
  • multiplication and division
  • addition and subtraction

This is originally based on the C version I wrote on my Amiga in 1987, which ended up in Symon and this version was based on a BBC Basic version which in turn was based on an understanding gained from disassembling the BBC Basic ROM (6502) in 1981, so credit is due to Richard Florance who figured it out first and explained it to me and also to Roger Wilson who built it in the first place.

For the C# version I've finally fixed the evaluation order for operators at the same precedence, originally it was simply recursing back to the start so 10-20+30 used to be evaluated as 10-(20+30) when it should be 10-20+30

The built in way of evaluating symbols (variables) is to use the built in Symbol Dictionary, so call SetSymbol("Name",value) before evaluation and references to Name will be handled as expected.

Dynamic Symbols
There is a virtual method ripe for overloading called GetSymbol - which is called to allow lookup (e.g. in a database or other store) of values dynamically.

You can also use GetSymbol to build a list of referenced symbols in an expression as the method delivers the current and next operator together with the expression tree depth.

    Eval x = new Eval();
    var res = x.Evaluate("2*3+4"); // will return 10

Example with symbols
    Eval x = new Eval();
    x.SetSymbol("ENV.MAIN", 20);
    x.SetSymbol("ENV.SECONDAY", 30);
    var res = x.Evaluate("ENV.MAIN+ENV.SECONDAY"); // will return 50

Last edited Jun 10, 2013 at 8:28 AM by RichardHarrison, version 2