-
Notifications
You must be signed in to change notification settings - Fork 0
Rewrite Rules
Rewrite rules are at the heart of rewriting expressions. A rewrite rule is a wrapper around three DDExpression
objects: a predicate, a condition, and a pattern.
The predicate describes how an expression must be structured in order for the rewrite rule to be applied.
There are four special kinds of expressions that are looked for. These are described in rewriting expressions, and are used to as representing "any expression", "any number", "any variable", or "any function". As these special tokens are encounted in the predicate, the corresponding values from the target expression are extracted and saved.
While the predicate dictates the structure an expression must have, the condition dictates values an expression may have. This is done via a simple boolean expression. For example, if we have the predicate of __exp1 / __exp1
, we can rewrite this as long as __exp1
is not equal to zero. If __exp1
is equal to zero, then the overall expression would result in a division-by-zero operation, which results in a mathematical error.
Thus, we can specify a condition for the rewrite rule that __exp1 != 0
. When an expression is found to match the predicate, it is then checked to see if the condition is met. If it is, then the pattern may be applied. If it is not, then the rule reports "no match".
If the expression is matched by the predicate and the condition is met, then the pattern is applied. The values extracted during predicate matching are substituted in to the pattern expression, and a new expression is created. This new expression is returned to the DDMathEvaluator
, where it replaces the original expression.