[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Working with the parse tree in Ruby?

Martin C. Martin

1/27/2007 12:21:00 AM

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

4 Answers

Aaron Patterson

1/27/2007 1:15:00 AM

0

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?

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

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

Then convert the parse tree back to ruby with ruby2ruby:

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

HTH!

--
Aaron Patterson
http://tenderlovem...

Pit Capitain

1/27/2007 2:44:00 PM

0

Martin C. Martin schrieb:
> 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

Vladimir Konrad

1/29/2007 9:21:00 PM

0

> 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 .

--
Posted via http://www.ruby-....

Pit Capitain

1/30/2007 11:40:00 AM

0

Vladimir Konrad schrieb:
> 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