Search code examples
c#.netlanguage-designirony

Using Irony for custom language design in .Net 2.0


Just as the question says, I'm stuck with .Net 2.0 here in my company and there is no chance of upgrading. Is there a way to make Irony work in .NET 2.0.

I'll briefly try to explain what I'm trying to achieve. We at our company have a payroll system where we let customer define there own formulas for payslip items. These formulas must use some keywords/functions which we check through Regex(very unreliable). Then we create a Microsoft script control object and use its eval function to parse the snippet. These snippets in turn call keywords/functions defined in class to get the output.

But to say this system has drawback is being very polite. Sometimes the it just overflows with error.

A very silly e.g. -

FVAL(A)/(FVAL(B)+FVAL(C))

Obviously there the tow stages this formula passes through.

First the validation stage where we do the following -

  • Check through regex whether there are any keywords used which are not present in predefined arrayList of keywords

  • Then check whether there are any variables(in this case - A, B, C are the ones) used which are not present in an arraylist of variable.

  • If all validation passes then we pass the formula string to a ScriptControlClass object's eval function and also add a static class with all functions defined in it(in this case consider FVAL()) and this function returns boolean true if the variable is a valid one to use in this context(like adding a date and number would return false)

Now after evaluating this expression the result is - DivideByZeroException, why because eval typecasts all boolean true to 1 and performs normal arithmetic to it.

FVAL(A)/(FVAL(B)+FVAL(C)) => 1/(1-1)

The second phase is almost similar but without any validation check, like without returning true we actually return the value of the variable from a DataTable which consists of values for each variables.

The system is antiquated and its seriously getting on my nerves and I really need help. Please suggest.


Solution

  • If you're just talking about excel style formulas, it's pretty easy to write a good interpreter, especially if you only have to deal with one data type. Someone with a background in compilers (or even parsers) could probably write you up on in less than a day. I've written a bunch myself.

    Embedding a scripting language is another option, but you'd have to be comfortable with the quirks of the given language. Ensuring that high-precision numbers are used could be difficult, if that's a business requirement.