|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionI've come across several expression evaluators on this site. They utilize several clever ideas to implement them, but each of them had their "gotchas". I needed one that was flexible (handled more than one data type), that didn't compile code (too slow and wasted resources), that worked (one couldn't handle any unary operator), and could be easily adapted for other needs. Thus, I've developed a set of simple classes called About the project filesIncluded in the project zip(s) are the .cs files that implement regular expressions, expression evaluation, and function evaluation. Also included is a console based tester application that will allow you to enter and manually test particular expressions. Simply select the version of the project (VS 2005 or 2003) from the versions above. APIThe API is really simple. You have two main classes:
The first time
Both the Expression stringsExpression strings are easy to construct. Standard operator precedence applies. (See C# documentation) Parenthesis work. The !, -, and ~ unary operators are functional. To call a function in the expression string use the following syntax: To get a list of the built-in functions, look at the Here are some examples of expression strings: (1 + 1) * 17 / 3 $now() >= $today() $pi() == $e() "Today is " + $fmtdate($today(), "dddd, MMMM d, yyyy") @(Variable1) == @(Variable2) !true == false
Sample codeIn the unit test project, you will see a lot of sample expressions. Here are some example codes for usage: Construction and evaluationExpressionEval expr = new ExpressionEval("1+1");
object val = expr.Evaluate();
Creating a custom function handlerstatic void eval_AdditionalFunctionEventHandler(
object sender, AdditionalFunctionEventArgs e)
{
object[] parameters = e.GetParameters();
switch (e.Name)
{
case "brent":
e.ReturnValue = "This Library Rocks!";
break;
case "liljohn":
e.ReturnValue = "WWWWWWWWHHHHHAT? YEAYAH! OKAY!";
break;
case "strcat":
string ret = "";
foreach (object parameter in parameters)
ret += parameter.ToString();
e.ReturnValue = ret;
break;
case "setvar":
(sender as FunctionEval).SetVariable(
"" + parameters[0],
parameters[1]
);
break;
}
}
Using the tester applicationMake sure the tester application is set as the startup project. Hit F5 to build and run. You will see a blank console screen. Type in an expression and hit Enter. Its evaluation will appear below it, otherwise an error will be displayed. Type "clr" to clear the console, and type "exit" to quit the application. Known issuesThere is one issue that I am aware of. If you place two binary operators in a row, no error is returned. Instead, the first in the list is used, and the rest until the right operand (w/ or w/o unary operator) are ignored. Example: (1 * + -1). This will ignore '+' and execute (1 * -1). Note: I've improved the error handling to catch unused tokens and missing binary operators. Updates
|
||||||||||||||||||||||