In Lisp, it's easy to get the parse tree of an expression, and to manipulate those parse trees. That means Lisp code tends to use macros, i.e. lisp functions whose return values are parse trees. These macros are evaluated at compile time and inserted into the place where the macro call appears.
Is there anything equivalent in Ruby? Is there a way in Ruby to get the parse tree of, say, x + y? Is there a way to execute parse trees?
On Sat, Jan 27, 2007 at 09:20:55AM +0900, Martin C. Martin wrote:
Hi,
In Lisp, it's easy to get the parse tree of an expression, and to
manipulate those parse trees. That means Lisp code tends to use macros,
i.e. lisp functions whose return values are parse trees. These macros
are evaluated at compile time and inserted into the place where the
macro call appears.
Is there anything equivalent in Ruby? Is there a way in Ruby to get the
parse tree of, say, x + y? Is there a way to execute parse trees?
In Lisp, it's easy to get the parse tree of an expression, and to manipulate those parse trees. That means Lisp code tends to use macros, i.e. lisp functions whose return values are parse trees. These macros are evaluated at compile time and inserted into the place where the macro call appears.
Is there anything equivalent in Ruby? Is there a way in Ruby to get the parse tree of, say, x + y? Is there a way to execute parse trees?
Martin, in addition to what Aaron has written, I'd like to ask you what you need this for. I've been playing with implementing macros in Ruby a few times, but I found no compelling reason to support them in Ruby. Yes, I could do some nice tricks, but I think they haven't been worth the trouble. So I'd be interested in your use cases.
Martin, in addition to what Aaron has written, I'd like to ask you what
you need this for. I've been playing with implementing macros in Ruby a
few times, but I found no compelling reason to support them in Ruby.
well, i have a use case (but this is doable, i think, with existing ruby
using code generation and eval).
i wanted to write a "universal netcdf dump" procedure for netcdf files
(the netcdf library for ruby exists). the format i have has few
dimensions and one multi-dimensional array. the problem is that the
number of dimensions can vary - e.g. you can have value(dim1) or
value(dim1,dim2,dim3...).
what i wanted was a general way to dump the multidimensional variable
value + each related value in each dimension as a table for import to
SQL. this translates to nested iterators (where number of iterators is
the number of dimensions) e.g for 2 dimensions something like:
# this is pseudo code, dim1, dim2, value are arrays from netcdf
well, i have a use case (but this is doable, i think, with existing ruby using code generation and eval).
(...)
Vlad, thanks for your example. I too think that you can use eval to build the code. Further, it seems that you could implement a recursive solution without using eval at all.
The macros I have toyed with took some existing Ruby code and then changed it according to some rules.