-
Notifications
You must be signed in to change notification settings - Fork 0
The Math Evaluator
Evaluation is part of the expression's logic, but it uses the Math Evaluator quite extensively.
Evaluating a number expression is trivial. It simply returns the underlying NSNumber object.
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 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:
- the array of
DDExpressionobjects that are the arguments to the function - the substitution dictionary
- the math evaluator
- 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.