Skip to content
Dave DeLong edited this page Sep 29, 2011 · 2 revisions

Evaluation is part of the expression's logic, but it uses the Math Evaluator quite extensively.

Evaluating numbers

Evaluating a number expression is trivial. It simply returns the underlying NSNumber object.

Evaluating variables

Evaluating a variable is also relatively straight-forward. First, the variable is looked up in the passed substitutions dictionary. If no replacement is found, then an error is generated and evaluation aborts.

Otherwise, the object is examined:

  • if the substitution value is another expression, that expression is evaluated and its return value becomes the variables value.
  • if the substitution value is a string, that string is evaluated as an expression, and its return value becomes the variables value
  • if the substitution value is a number, the number is used directly
  • any other substitution value generates an error

Evaluating functions

Evaluating a function is where the math evaluator is used. The expression asks the evaluator for a DDMathFunction (a block) that matches the name of the function. If no match is found, a failure method is invoked on the evaluator (-functionExpressionFailedToResolve:error:), which generates an error and aborts evaluation.

Otherwise, the DDMathFunction is invoked with four arguments:

  1. the array of DDExpression objects that are the arguments to the function
  2. the substitution dictionary
  3. the math evaluator
  4. the NSError** (for reporting errors)

The return value of the DDMathFunction should be another DDExpression object, although some leniency is allowed: NSNumbers are returned directly and NSStrings are parsed and evaluated to extract a numerical value.

Clone this wiki locally