chickadee » linden-scheme » context

(context (TEST BODY ...) ...)syntax

Similar to a cond form, but conditional to the context of a given rule. Evaluates BODY when the context given in TEST matches the current context of the rule being evaluated. Each TEST should be of the form:

    ((PREVIOUS-RULE [ARGS] ...) (NEXT-RULE [ARGS] ...) [: GUARD])

When PREVIOUS-RULE and NEXT-RULE match the names of the previous and next rules to the rule being currently evaluated, the associated BODY is evaluated at the exclusion of all other BODYs. The supplied ARGS are symbols that are bound to the values of the arguments of their respective rules. Either (PREVIOUS-RULE [ARGS] ...) or (NEXT-RULE [ARGS] ...) may be replaced with a *, indicating that any rule (including none) may match.

If desired, a GUARD form may be supplied, preceded by a :. This guard acts as an additional test before a BODY is evaluated. The guard may use any variables given as ARGS (as well as any other variables in scope).

The last TEST may consist of the symbol else, which is unconditionally evaluated.

For example:

   (define-rule (apex)
     (context
      (((stem len) * : (> len 2))
       '((leaf 1) (branch (leaf 1) (stem 1) (apex)) (stem 1) (apex)))
      (((stem len) *)
       #f)
      (else '((leaf 1) (stem 1) (apex)))))

defines a rule (apex) that creates a new branch when preceded by a stem whose first parameter (len) greater than 2, does nothing when preceded by a stem of length less than or equal to 2, and otherwise creates a new leaf and stem.