Working with the parse tree in Ruby?

M

Martin C. Martin

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?

Best,
Martin
 
A

Aaron Patterson

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?

Yes, you can get the parse tree using "ParseTree"

http://rubyforge.org/projects/parsetree/

Then convert the parse tree back to ruby with ruby2ruby:

http://rubyforge.org/projects/seattlerb/

HTH!
 
P

Pit Capitain

Martin said:
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.

Regards,
Pit
 
V

Vladimir Konrad

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

dim1[].length.times{|dim1_i|
dim2[].length.times{|dim2_i|
print dim1[dim1_i], dim2[dim2_i], value[dim1_i,dim2_i]
}}

i could not imagine to do this without a macro (or without code
generation and eval).

vlad

ps: i have not done the "universal netcdf dump" procedure as i do not
really need it for the import but it was an interesting thing to think
about .
 
P

Pit Capitain

Vladimir said:
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.

Regards,
Pit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,230
Messages
2,571,161
Members
47,796
Latest member
AlphonseNa

Latest Threads

Top