[plt-scheme] macros utilizing syntax parameters

From: David T. Pierson (dtp at mindstory.com)
Date: Thu Mar 5 00:00:32 EST 2009

Hi all,

I created some macros which utilize a syntax parameter.  I thought it
might be useful as an example for others.  I would also like criticism
if there is a better way to do what I wanted.  (I'm guessing there is.)

I wanted to create some macros to basically transform this:

  (workspace office
    (define-work developer (void)))
  (define-work hobo (void))

into:

  (define office
    (cons 'workspace 'office))
  (define developer
    (list 'developer
          office
          (lambda () (void))))
  (define hobo
    (list 'hobo
          '(workspace . the-world)
          (lambda () (void))))

This is what I came up with:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang scheme

(require scheme/stxparam)
(require scheme/splicing)

(define-syntax-parameter enclosing-workspace ''(workspace . the-world))

(define-syntax define-work
  (lambda (stx)
    (syntax-case stx ()
      ((define-work name body0 body1 ...)
       (let ((ws-syntax (syntax-parameter-value (quote-syntax enclosing-workspace))))
         (quasisyntax
          (define name
            (list (quote name)
                  #,ws-syntax
                  (lambda () body0 body1 ...)))))))))

(define-syntax workspace
  (lambda (stx)
    (syntax-case stx ()
      ((_ name body0 body1 ...)
       (syntax
         (splicing-syntax-parameterize ((enclosing-workspace (syntax name)))
            (define name
              (cons 'workspace (quote name)))
            body0 body1 ... ))))))

(workspace office (define-work developer (void)))
(define-work hobo (void))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Thanks for any tips.

David


Posted on the users mailing list.