Opened 3 years ago

#1770 new enhancement

expand1: Expand macro-form only once

Reported by: John Croisant Owned by:
Priority: not urgent at all Milestone: someday
Component: core libraries Version: 5.2.0
Keywords: Cc:
Estimated difficulty:


These patches add the expand1 procedure to module (chicken syntax), and add the ,x1 toplevel command to csi. This has been a personal wish of mine for several years and I finally decided to implement it. :)

expand1 is like expand but it expands the form only once, instead of repeating until a non-macro form is produced. This is helpful when debugging macros because you can see the intermediate results of the expansion.

,x1 is like the ,x command (expand and pretty-print the form) but it uses expand1 instead of expand. This is not difficult for users to implement in their .csirc once expand1 is available, so you can ignore the second patch if you don't think it is useful enough to build into csi.

I tried adding the tests below to tests/syntax-tests.scm, but they only pass when using csi, not csc. Specifically, (expand '(rec-macro 1)) and (expand1 '(rec-macro 1)) both expand to (rec-macro 1). I suspect this is because the rec-macro macro is not visible at run time in compiled code. The only solution I have found so far is to use (eval '(begin (define-syntax rec-macro ...) (expand '(rec-macro 1)))), which is messy because every test must repeat the macro definition. Maybe someone more knowledgeable than me can find a cleaner way to make the tests work.

;;; expand / expand1

(define-syntax rec-macro
  (syntax-rules ()
    ((rec-macro 1) (rec-macro 2))
    ((rec-macro 2) "done") ) )

;; `expand` expands repeatedly until it finds a non-macro form.
(t "done" (expand '(rec-macro 1)))

;; `expand1` expands only once.
(t '(rec-macro 2) (strip-syntax (expand1 '(rec-macro 1))))
(t "done" (expand1 (expand1 '(rec-macro 1))))

Attachments (2)

0001-Add-expand1-procedure-in-module-chicken-syntax.patch (2.2 KB) - added by John Croisant 3 years ago.
0002-Add-x1-toplevel-command-to-csi.patch (1.5 KB) - added by John Croisant 3 years ago.

Download all attachments as: .zip

Change History (2)

Changed 3 years ago by John Croisant

Note: See TracTickets for help on using tickets.