chickadee » ck-macros » c-make-rules

(c-make-rules '(L ...) '(P X) ...) → '(syntax-rules ...)syntax

Build a CK-macro based on syntax-rules. Added in version 0.3.0.

Given a list of zero or more literal symbols, and one or more pattern/expression lists, yields a syntax-rules form for a CK-macro with the following behavior:

  • Given argument(s) that match any pattern P, yields the value of the associated expression X, which may use identifiers from P. Fails if no pattern matches.

Each pattern P is a list of zero or more sub-patterns, which will be matched (as with syntax-rules) against the already-evaluated and quoted arguments given to the new CK-macro. Alternatively, P may be an identifier which will capture all arguments as a list.

Each expression X is a single CK-macro expression or quoted value. Identifiers from the pattern can be used in the expression, as with syntax-rules.

Symbols in the literals list (L ...) will be treated as literal identifiers in patterns. Additionally, quote is always treated as a literal identifier.

Caveats:

  • Using ... in a pattern or expression may not work portably with all Scheme systems.
  • Symbols begining with %ck: are reserved for internal use and must not appear in any L, P, or X.
(ck () `(define-syntax c-flip-flop
          ,(c-make-rules '(flip flop)
            '(('flip 'x 'y)
              (c-list 'y 'x))
            '(('flop 'x 'y)
              '#f))))

;; The above is basically equivalent to:
;; (define-syntax c-flip-flop
;;   (syntax-rules (flip flop quote)
;;     ((_ s 'flip 'x 'y)
;;      (ck s (c-list 'y 'x)))
;;     ((_ s 'flop 'x 'y)
;;      (ck s '#f))))

(ck () (c-quote (c-flip-flop 'flip '1 '2)))
;; ==> '(1 2)

(ck () (c-quote (c-flip-flop 'flop '1 '2)))
;; ==> #f