[Rd] access to R parse tree for Lisp-style macros?
Andrew Piskorski
atp at piskorski.com
Mon Oct 3 09:25:35 CEST 2005
R folks, I'm curious about possible support for Lisp-style macros in
R. I'm aware of the "defmacro" support for S-Plus and R discussed
here:
http://www.biostat.wustl.edu/archives/html/s-news/2002-10/msg00064.html
but that's really just a syntactic short-cut to the run-time use of
substitute() and eval(), which you could manually put into a function
yourself if you cared too. (AKA, not at all equivalent to Lisp
macros.) The mlocal() function in mvbutils also has seemingly similar
macro-using-eval properties:
http://cran.r-project.org/src/contrib/Descriptions/mvbutils.html
http://www.maths.lth.se/help/R/.R/library/mvbutils/html/mlocal.html
I could of course pre-process R source code, either using a custom
script or something like M5:
http://www.soe.ucsc.edu/~brucem/samples.html
http://groups.google.com/group/comp.compilers/browse_thread/thread/8ece2f34620f7957/000475ab31140327
But that's not what I'm asking about here. As I understand it,
Lisp-style macros manipulate the already-parsed syntax tree. This
seems very uncommon in non-Lisp languages and environments, but some -
like Python - do have such support. (I don't use Python, but I'm told
that its standard parser APIs are as powerful as Lisp macros, although
clunkier to use.)
Is implementing Lisp-style macros feasible in R? Has anyone
investigated this or tried to do it?
What internal representation does R use for its parse tree, and how
could I go about manipulating it in some fashion, either at package
build time or at run time, in order to support true Lisp-style macros?
Whenever I try something like this in R:
> dput(parse(text="1+2"))
expression(1 + 2)
what I see looks exactly like R code - that '1 + 2' expression doesn't
look very "parsed" to me. Is that really it, or is there some sort of
Scheme-like parse tree hiding underneath? I see that the interactive
Read-Eval-Print loop basically calls R_Parse1() in "src/main/gram.c",
but from there I'm pretty much lost.
Also, what happens at package build time? I know that R CMD INSTALL
generates binary *.rdb and *.rdx files for my package, but what do
those do exactly, and how do they relate to the REPL and R_Parse1()?
Finally, are there any docs describing the design and implementation
of the R internals? Should I be looking anywhere other than the R
developer page here?:
http://developer.r-project.org/
Thanks!
--
Andrew Piskorski <atp at piskorski.com>
http://www.piskorski.com/
More information about the R-devel
mailing list