Search code examples
common-lispmit-scheme

How to do macroexpand-1 and macroexpand in MIT Scheme?


In Common Lisp I can define a two-level macro and expand the macro like this:

(defmacro calc (a op b)
  (list op a b))

(defmacro twice (x)
  (list 'calc x '+ x))

(twice 10)
(macroexpand-1 '(twice 10))
(macroexpand '(twice 10))

Output:

20
(CALC 10 + 10)
(+ 10 10)

Now I am trying to do the same thing in MIT Scheme:

(define-syntax calc
  (syntax-rules ()
    ((_ a op b)
     (op a b))))

(define-syntax twice
  (syntax-rules ()
    ((_ x)
     (calc x + x))))

(twice 10)

How can I do the equivalent of macroexpand-1 and macroexpand in MIT Scheme?


Solution

  • In Racket, macroexpand would be

    (syntax->datum
      (expand-to-top-form '(twice 10)))
    ;; '(+ 10 10)
    

    and macroexpand-1 would be

    (syntax->datum (expand-once '(twice 10)))
    ;; '(calc 10 + 10)
    

    There are papers for macro-debugging in scheme by M. Felleisen. See here.