Next: , Previous: , Up: iklib expander   [Index]


6.14.9 Expand–time parameters

Fluid syntaxes and expand–time values can be used to implement a sort of expand–time parameters, Parameters. The following example shows the mechanism by defining a “parameter” ‘parm’:

(import (vicare))

(define-syntax (show-it stx)
  (fprintf (current-error-port)
           "parm ~a: ~a\n"
           (cadr (syntax->datum stx))
           (retrieve-expand-time-value #'parm))
  #f)

(define-fluid-syntax parm
  (make-expand-time-value #f))

(show-it "1, expect #f")
(fluid-let-syntax ((parm (make-expand-time-value #t)))
  (show-it "2, expect #t")
  (fluid-let-syntax ((parm (make-expand-time-value #f)))
    (show-it "3, expect #f"))
  (show-it "4, expect #t"))
(show-it "5, expect #f")

the output of the program is:

parm 1, expect #f: #f
parm 5, expect #f: #f
parm 2, expect #t: #t
parm 3, expect #f: #f
parm 4, expect #t: #t

Using the API described here, the example can be rewritten:

(import (vicare))

(define-syntax (show-it stx)
  (fprintf (current-error-port)
           "parm ~a: ~a\n"
           (cadr (syntax->datum stx))
           (syntax-parameter-value #'parm))
  #f)

(define-syntax-parameter parm #f)

(show-it "1, expect #f")
(syntax-parametrise ((parm #t))
  (show-it "2, expect #t")
  (syntax-parametrise ((parm #f))
    (show-it "3, expect #f"))
  (show-it "4, expect #t"))
(show-it "5, expect #f")
Syntax: define-syntax-parameter ?parm-id ?expr

Define a new syntax parameter bound to the identifier ?parm-id. The parameter is initialised to the result of evaluating the expand–time expression ?expr.

Syntax: syntax-parametrise ((?parm-id ?expr) …) ?body0 ?body

Bind one or more syntax parameters to new expand–time values and while the body forms are expanded. The arguments ?parm-id must be identifiers previously bound to expand–time values by define-syntax-parameter. The arguments ?expr must be expressions.

Function: syntax-parameter-value parm-id

Return the syntax parameter value bound to parm-id, which must be an identifier previously bound by define-syntax-parameter. This function must be called only from the extent of a macro expansion; in practice: only by a macro transformer.


Next: , Previous: , Up: iklib expander   [Index]