somehow missent
---------- Forwarded message ----------
More issues with this. Context in forwarded msg.
Here’s a (probably very naive) implementation of the discussed code.
It takes code blocks, turns them into individual statements, and
module_evals each individual statement. It does this by turning blocks
into code with Ruby2Ruby, splitting the code on new lines, and
module-eval()ing each statements in the resulting array.
If you think about it the problem is obvious. Here’s the code.
def generate(&block)
code = ""
statements_in_the_block(block).each do |statement|
code << Generators.module_eval(statement)
end
code
end
def statements_in_the_block(block)
statements = /proc
{\n(.+)}/m.match(block.to_ruby)[1].split(/\n/)
statements.map {|statement| statement.gsub!(/^\s+/, “”)}
statements.map {|statement| statement.gsub!(/\s+/, " ")}
statements
end
The problem, of course, is nested blocks. If you hit a block which
contains another block, you get eval trying to eval block-starting
lines like
eval(“block_taking_method do |variable|”)
which of course goes bonk.
Probably what I need to do instead (thinking out loud) is pull the
blocks and statements out of the actual s-exps, like have some method
which takes s-exps and then turns them into blocks and statements, and
work from there. Ironically the blocks it’s breaking on do fine, due
to a quirk in my code, the inner blocks are good but the larger blocks
break.
–
Giles B.
Podcast: http://hollywoodgrit.blogspot.com
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com
---------- Forwarded message ----------
From: Giles B. [email protected]
Date: Jan 4, 2008 8:07 AM
Subject: Re: parse tree?
To: ruby-talk ML [email protected]
def code_for █ block.to_ruby; end
=> nilcode_for do
?> single_line(statement)statement(:which => spans,
?> :multiple => lines)end
=> “proc {\n single_line(statement)\n statement(:which =>
spans, :multiple => lines)\n}”There are clean ways to ensure it isn’t a proc as well.
That totally solves my problem. Or at least, if it’s guaranteed
to turn multi-line statements into one-liners every time, that very
probably totally solves my problem. (I haven’t tried it yet, I’ve just
proved it to be true, etc.) I can just pull it out of the proc{} with
a regex and then split it on the newlines. Baddabing baddaboom.
–
Giles B.
Podcast: http://hollywoodgrit.blogspot.com
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com
–
Giles B.
Podcast: http://hollywoodgrit.blogspot.com
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com