• implementing scheme - implementing macros

    From Mario Rosell@mario@mariorosell.es to comp.lang.lisp on Fri Jun 12 21:15:13 2026
    From Newsgroup: comp.lang.lisp

    [ Repost from Reddit. r/lisp: https://t.ly/Fldzk ]

    Hello everyone.

    I am making a Scheme R5RS implementation and it is going pretty well.

    I am trying implement macros, and don't really know how to implement them. Scheme has a pretty complex macro system, so for now I am trying to
    implement defmacro and then implement define-syntax, syntax-case, etc.

    Any tips?

    Thanks in advance.
    --
    - mario
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From steve g@Sgonedes1977@gmail.com to comp.lang.lisp on Tue Jun 16 21:50:26 2026
    From Newsgroup: comp.lang.lisp

    Mario Rosell <mario@mariorosell.es> writes:

    [ Repost from Reddit. r/lisp: https://t.ly/Fldzk ]

    Hello everyone.

    I am making a Scheme R5RS implementation and it is going pretty well.

    I am trying implement macros, and don't really know how to implement them. Scheme has a pretty complex macro system, so for now I am trying to
    implement defmacro and then implement define-syntax, syntax-case, etc.

    Any tips?


    using eval is your friend.


    the old emacs way was hard to read.

    the emacs lisp info page: "

    14.1 A Simple Example of a Macro
    ================================

    Suppose we would like to define a Lisp construct to increment a variable
    value, much like the rCy++rCO operator in C. We would like to write rCy(inc x)rCO and have the effect of rCy(setq x (1+ x))rCO. Here's a macro definition that does the job:

    (defmacro inc (var)
    (list 'setq var (list '1+ var)))

    "

    you need to ``reevaluate'' the macro twice.

    the commmon lisp was uses symbols like @ , . etc..


    hope this is helpful.
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Lawrence =?iso-8859-13?q?D=FFOliveiro?=@ldo@nz.invalid to comp.lang.lisp on Wed Jun 17 03:14:11 2026
    From Newsgroup: comp.lang.lisp

    On Tue, 16 Jun 2026 21:50:26 -0400, steve g wrote:

    the old emacs way was hard to read.

    the commmon lisp was uses symbols like @ , . etc..

    ThatrCOs the traditional way of doing it, also supported by Emacs
    (naturally).
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Paul Rubin@no.email@nospam.invalid to comp.lang.lisp on Tue Jun 16 21:45:08 2026
    From Newsgroup: comp.lang.lisp

    steve g <Sgonedes1977@gmail.com> writes:
    you need to ``reevaluate'' the macro twice.
    the commmon lisp was uses symbols like @ , . etc..

    That's been built into Emacs Lisp for quite a while too. Before it was
    built into the reader, it was available as a loadable macro package so
    you'd write something like (` (setq (, var) (1+ (, var))) . Now it's
    like CL, `(setq ,var (1+ ,var)) .
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From antispam@antispam@fricas.org (Waldek Hebisch) to comp.lang.lisp on Wed Jun 17 14:26:05 2026
    From Newsgroup: comp.lang.lisp

    Mario Rosell <mario@mariorosell.es> wrote:
    [ Repost from Reddit. r/lisp: https://t.ly/Fldzk ]

    Hello everyone.

    I am making a Scheme R5RS implementation and it is going pretty well.

    I am trying implement macros, and don't really know how to implement them. Scheme has a pretty complex macro system, so for now I am trying to
    implement defmacro and then implement define-syntax, syntax-case, etc.

    Any tips?

    Naive macros are easy: when you see macro definition you handle
    it almost as a function, except for that you mark resulting
    object as a macro. In evaluator (or in compiler if you have one)
    you first check if the form is a macro call (that is head is marked as
    a macro), if it is than you execute corresponding macro function applying
    it to unevaluated arguments. When macro function returns you
    handle result as new source code.

    Once you have naive macros you can add niceties like quasiquote
    by preprocessing the macro definition. In other words, better
    macro can be implemented as macro definition which processes
    its argument and then passes it to naive macro.

    I am not familiar with details of Scheme macros, but it is quite
    likely that they could be implemented in similar way on top of
    naive macros.
    --
    Waldek Hebisch
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From steve g@Sgonedes1977@gmail.com to comp.lang.lisp on Fri Jun 19 23:15:13 2026
    From Newsgroup: comp.lang.lisp

    Paul Rubin <no.email@nospam.invalid> writes:

    steve g <Sgonedes1977@gmail.com> writes:
    you need to ``reevaluate'' the macro twice.
    the commmon lisp was uses symbols like @ , . etc..

    That's been built into Emacs Lisp for quite a while too. Before it was
    built into the reader, it was available as a loadable macro package so
    you'd write something like (` (setq (, var) (1+ (, var))) . Now it's
    like CL, `(setq ,var (1+ ,var)) .


    yes, I do remeber this. very compilcated to read; much better now.
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Kaz Kylheku@046-301-5902@kylheku.com to comp.lang.lisp on Tue Jun 23 20:59:00 2026
    From Newsgroup: comp.lang.lisp

    On 2026-06-12, Mario Rosell <mario@mariorosell.es> wrote:
    [ Repost from Reddit. r/lisp: https://t.ly/Fldzk ]

    Hello everyone.

    I am making a Scheme R5RS implementation and it is going pretty well.

    I am trying implement macros, and don't really know how to implement them. Scheme has a pretty complex macro system, so for now I am trying to
    implement defmacro and then implement define-syntax, syntax-case, etc.

    Any tips?

    There exists more than one algorithm for hygienic macros.

    The "grand daddy" is Kohlbecker's algorithm (1986)

    The offshoots:

    - Syntactic closures (1988)
    - Alan Bawden paper from the proceedings of the 1988 ACM conference on Lisp
    and functional programming.
    - Chris Hanson 1991 paper, "A Syntactic Closure Macro Facility".

    - Modified Kohlbecker's algorithm:
    - William Clinger's 1990 paper, "Macros That Work"
    - claims to address issues with syntactic closures
    - reduces Kohlbecker from quadratic to linear time
    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Stefan Monnier@monnier@iro.umontreal.ca to comp.lang.lisp on Thu Jun 25 10:59:18 2026
    From Newsgroup: comp.lang.lisp

    There exists more than one algorithm for hygienic macros.

    The "grand daddy" is Kohlbecker's algorithm (1986)

    The offshoots:

    - Syntactic closures (1988)
    - Alan Bawden paper from the proceedings of the 1988 ACM conference on Lisp
    and functional programming.
    - Chris Hanson 1991 paper, "A Syntactic Closure Macro Facility".

    - Modified Kohlbecker's algorithm:
    - William Clinger's 1990 paper, "Macros That Work"
    - claims to address issues with syntactic closures
    - reduces Kohlbecker from quadratic to linear time

    Might be worth mentioning the "sets of scopes" one used in Racket:

    Binding as sets of scopes
    Matthew Flatt. POPL 2016.
    doi: 10.1145/2837614.2837620


    === Stefan
    --- Synchronet 3.22a-Linux NewsLink 1.2