Subject: Re: macro for shorter array syntax
From: rpw3@rpw3.org (Rob Warnock)
Date: Sun, 20 May 2007 00:45:53 -0500
Newsgroups: comp.lang.lisp
Message-ID: <162dneMQRb0MfdLbnZ2dnUVZ_jednZ2d@speakeasy.net>
DanM  <ec366rh02@sneakemail.com> wrote:
+---------------
| Jon Harrop <j...@ffconsultancy.com> wrote:
| > Is the problem that Lisp macros require s-expr syntax, and non-sexpr syntax
| > requires Lisp read macros? So a better example would be something that can
| > be written in s-expr syntax?
| 
| Macros operate on data structures produced, typically but not
| exclusively, by the reader, and must produce something that's
| acceptable to the interpreter or compiler (possibly after processing
| by yet more macros). So in a sense, yes, macros "require" sexps.
+---------------

This is a common misunderstanding. Lisp-style macros do not require
s-exprs per se. What they *do* require, at a minimum, is that
the language processor [compiler/interpreter/whatever] provide
to the macro programmer, at macro-expansion time, the object
[DOM/parse-tree/semantic-graph/list/s-expr/whatever] representing
the macro call and its parsed-but-not-evaluated arguments, *and*
that it allow the macro programmer to use (the full power of) the
language itself to analyze/inspect/deconstruct/copy that object[1]
and construct a similar type of object [DOM/tree/graph/s-expr/...]
that will *replace* the macro call in the program being processed.

This is trivial -- and thus most convenient -- in Lisp where
the source code of the language *is* a sequence of just such
parsed-but-not-evaluated objects, but it it still quite *possible*
in other languages. "All" you need is (1) a reasonably-convenient
internal data representation for parsed-but-not-evaluated portions
of a program, (2) the ability for the macro programmer to define
a full-general program which, at macro-expansion time, accepts those
as input and produces new such objects as output, and (3) that the
language system accepts such output as a replacement for the macro
form in the program being processed.  Not easy, or convenient, but
possible.


-Rob

[1] I say "the object" for simplicity in discussion, assuming
    without loss of generality that all macros are defined with
    only (&WHOLE WHOLE) args. Extension to the full CL-style
    macro lambda parameters is trivial.

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607