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.
;; Contrived example
(ck () `(define-syntax c-math
          ,(c-make-rules '(add subtract)
            '(('add 'x 'y)
              (c-+ 'x 'y))
            '(('subtract 'x 'y)
              (c-- 'x 'y)))))

;; The above is basically equivalent to:
;;   (define-syntax c-math
;;     (syntax-rules (add subtract quote)
;;       ((_ s 'add 'x 'y)
;;        (ck s (c-+ 'x 'y)))
;;       ((_ s 'subtract 'x 'y)
;;        (ck s (c-- 'x 'y)))))

(ck () (c-quote (c-math 'add '3 '1)))
;; ==> 4

(ck () (c-quote (c-math 'subtract '3 '1)))
;; ==> 2