アットウィキロゴ

macro

* Runtime first-class macro
 
Traditional macro: S-expr => S-expr
S式変換とマクロへのmapに分解。
 
;Gauche
(define-macro (swap! a b)
  `(let ((c ,a))
     (set! ,a ,b)
     (set! ,b c)))
 
(define swap!
  (macro
    (lambda (a b)
      `(let ((c ,a))
         (set! ,a ,b)
         (set! ,b c)))
 
Hygienic macro: Env + S-expr => Env + S-expr
S式変換、環境捕捉、マクロへのmapに分解。
 
;R5RS
(define-syntax begin0
  (syntax-rules ()
    ((_ a b ...)
      (let ((ret a))
        (begin b ... ret)))))
 
; ... (2)
(define begin0
  (macro
    (lambda args ; ... (1)
      (hygiene-code
        `(let ((ret ,(car args))
          (begin ,@(cdr args) ret))))) 
 
; macro-dynamically enclosing environment
; AAA identifier AAA
; ----------------
;   VVV symbol VVV
; macro-statically enclosed environment
; | closure ... (1)
; | global environment (toplevel namespace/selected module) ... (2)
 
(define storage
  (let ((sym (gensym)))
    (macro
      (lambda (x y)
        `((,x ,sym ,y)))))
 
(storage define 4)
(storage update! add1) ; update! in Gauche
 
最終更新:2011年09月04日 11:28