2

I am looking to create a basic expression language that I can leverage within an iOS application. I will be pulling these values in from a file at runtime. In the past I have just leveraged regular expressions for this, but it has its own limitations (as I basically just have two operands and an operator). I want to have a bit more robust support for expressions like the following:

Some examples:

valueA != valueB
(valueA == valueB) && (valueC != valueD)
valueA >= valueB

Basically, I would want to provide a dictionary to this expression evaluator and have it pull the values for the variables from that dictionary.

Any thoughts on an efficient way to get this done? I've done about 5 minutes of research this morning on CoreParse and ParseKit, but I am new to both (and parser generators as a whole).

dtuckernet
  • 7,817
  • 5
  • 39
  • 54

1 Answers1

4

You could use NSPredicate, for example:

NSString *expr = @"($valueA == $valueB) && ($valueC != $valueD)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:expr];
NSDictionary *vars = @{@"valueA": @7, @"valueB" : @7, @"valueC": @3, @"valueD": @4};
BOOL result = [predicate evaluateWithObject:nil substitutionVariables:vars];

Note that all variables have to start with a $ in the predicate string.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382