CodePlexProject Hosting for Open Source Software

This is a library designed to understand basic mathematical strings and parse them into an object which can be evaluated at run time.

It is designed to allow for variables to be evaluated by a delegate.

IComponent[] rawComponents;

IComponent parsed = null;

if (Parser.GetComponents(txt, out rawComponents))

{

parsed = Parser.GetComponentTree(rawComponents);

}

The first first command (Parser.GetComponets) attempts to parse the string and if successful returns true.

The rawComponents array contains the different sections of the string, however at this point it has not applied any order of operation to it.

The second command Parser.GetComponentTree applies the order of operation to form a single component which can then be used to evaluate the string:

```
````object newVal = parsed.Evaluate(getVariable);`

The getVariable is a delegate to a function with a single string input returning the value of that variable.

By default, Parser.GetComponentTree pre-evaluates and constant parts of the ComponentTree. This can be stopped by calling it like:

`parsed = Parser.GetComponentTree(rawComponents, false);`

When evaluating the Component, functions are looked up in the Parser object. This is done using a
**case-insensitive** match in a dictionary.

You can register most of the function in System.Math by calling:

```
````Parser.RegisterMathFunctions();`

This will register any public static function in System.Math which takes only double parameters. As Round takes a double and an int, it is wrapped as a Round(double, double). It also registers and If function.

If you wish to register a custom function,

It currently makes assumption that:

- . is the decimal point
- , separate terms in functions
- ,
**cannot currently be used in constants**

+ | Addition Unary Plus |
a+b +a (should support multiple) |

- | Subtraction Unary Minus |
a-b -a (should support multiple) |

/ | Division | a/b |

* | Multiplication | a*b |

^ | Power | a^b |

% | Modulo Percentage Value (divide by 100) |
a%b a% |

‰ | Per Mille Value (divide by 1000) | a‰ |

= == |
Equals | a=b a==b |

!= <> |
Does Not Equal | a!=b a<>b |

< | less than | a<b |

<= | less than or equal to | a<=b |

> | greater than | a>b |

>= | greater than or equal to | a>=b |

! | Not | !a |

| | Exclusive Or | a|b |

|| | Or | a||b |

&& | And | a&&b |

& | Concatenate | a&b |

- Want to be able to assign a value to a variable inside the expression
- This would be a switch in the library in which case = would be assignment and == would Equals operator
**NOT CURRENTLY IMPLEMENTED**

Any numerical constant can be entered.

Commas cannot be used in constants.

In addition some keywords are recognised as constants:

- true
- false
- pi (Math.PI)
- e (Math.E)

- 5
- 5%
- 1.23
- -12.4

0 1 2 3 4 5 6 7 8 9 .

+ – / * ^ % ‰ = ! < > | &

Must start and end with a “

Follows the Excel rules – i.e. “” for a “ in constant

Must start and end with a #

Formats supported: yyyy-MM-dd, dd-MMM-yy, dd-MMM-yyyy

( ) [ ]

Must be paired or will result in parser error

Variable and function names can consist of a-z, A-Z, _, 0-9 but cannot start with a number.

Space, Tab, Return characters will be ignored

Will cause an unable to parse error

Last edited Jun 3, 2012 at 11:08 PM by jdunkerley, version 12