Using Ruby as a preprocessor for another language

D

debbie

I have the misfortune of being stuck programming in a very bad
scripting language that has poor support for functions, variable
substitutions, loops, etc. etc. (It's a proprietary language called
Aegis scripting language, for scripting NPCs in a game.)

I've been using Ruby as a macro pre-processor so that I can write more
maintainable scripts without violating the DRY (Don't Repeat Yourself)
principle. Presently, I have the following functionality:

1. #{...} strings are evaluated by Ruby
2. lines starting with @ are evaluated by Ruby

Thanks to Ruby's power and simplicity, the ENTIRE pre-processor is
just these few lines:

while line = gets
line.chomp!
if line =~ /^\s*\@(.*)$/
# Line starts with a @; it is Ruby code
puts $1
else
# Line should be printed, with #{} substitution
available
puts "puts \"#{line.gsub('"', '\"')}\""
end
end

This allows me to "compile" a template like this:

http://theanimaro.com/debbie/ruby/template.sc

into a script like this:

http://theanimaro.com/debbie/ruby/test.sc

Now my question is, am I reinventing the wheel here in that
pre-processor code I wrote---is there already an existing module in
the RAA that does this? I didn't want to use ERuby because I find that
those <% %> <%= %> tags everywhere make the script unreadable (ERuby
is more for HTML IMO).
 
J

James Edward Gray II

I didn't want to use ERuby because I find that
those <% %> <%= %> tags everywhere make the script unreadable (ERuby
is more for HTML IMO).

Are you aware that you can set ERuby (and ERb, the little brother
library included standard with Ruby) to treat any line beginning with
a simple % as code?

Example:

% npcs.each do |thug|
The thug's name is: <%= thug.name %>
% end

Just wanted to point that out. It's covered in the documentation for
the library, if you want to learn more.

James Edward Gray II
 
G

Gavin Kistner

Are you aware that you can set ERuby (and ERb, the little brother
library included standard with Ruby) to treat any line beginning
with a simple % as code?

And further said:
Example:

% npcs.each do |thug|
The thug's name is: <%= thug.name %>
% end


% nps.each do |thug|
% _erbout << "The thug's name is: #{thug.name}"
% end
 
W

William James

I've been using Ruby as a macro pre-processor so that I can write more
maintainable scripts without violating the DRY (Don't Repeat Yourself)
principle. Presently, I have the following functionality:

1. #{...} strings are evaluated by Ruby
2. lines starting with @ are evaluated by Ruby

Thanks to Ruby's power and simplicity, the ENTIRE pre-processor is
just these few lines:

while line = gets
line.chomp!
if line =~ /^\s*\@(.*)$/
# Line starts with a @; it is Ruby code
puts $1
else
# Line should be printed, with #{} substitution
available
puts "puts \"#{line.gsub('"', '\"')}\""
end
end

/^\s*\@(.*)$/

could be changed to

/^\s*@(.*)$/
 
D

debbie

% npcs.each do |thug|
The thug's name is: <%= thug.name %>
% end

It looks like the % only works if it's at the beginning of the line. It
won't work if there's tab characters before it. Is there a way to
change this?
 
M

Malte Milatz

Gavin Kistner wrote:
[erb]
% nps.each do |thug|
% _erbout << "The thug's name is: #{thug.name}"
% end

Furthermore, if erb is supposed to send its output to STDOUT, there's no
problem to simply use "puts" here.

Malte
 
R

Robert Klemme

It looks like the % only works if it's at the beginning of the line.
It won't work if there's tab characters before it. Is there a way to
change this?

Preprocess the file. SCNR :)

robert
 
G

Gavin Kistner

Gavin Kistner wrote:
[erb]
% nps.each do |thug|
% _erbout << "The thug's name is: #{thug.name}"
% end

Furthermore, if erb is supposed to send its output to STDOUT,
there's no
problem to simply use "puts" here.

There is if you have any literal text in your ERB file. (And if you
don't have ANY literal text, then there's not much point in using
ERB, is there?) For example:

require 'erb'

template1 = <<'ENDTEMPLATE'
Hello
% puts "you crazy"
World
ENDTEMPLATE

ERB.new( template1, nil, '%' ).run
#=> you crazy
#=> Hello
#=> World

template2 = <<'ENDTEMPLATE'
Hello
% _erbout << "you crazy\n"
World
ENDTEMPLATE

ERB.new( template2, nil, '%' ).run
#=> Hello
#=> you crazy
#=> World


However, you can use my patch to ERB which makes puts behave as might
be expected/desired in the above situation. See http://
blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/152909 for more
information.
 
G

Gene Tani

Looks like your template's distilled your business logic pretty well,
but maybe you can locate a copy of Herrington's book on eRB for code
generation. i've seen it at Borders in the past (doesn't talk about
avoiding the '<%' '%>' and '<%' '=%>', except to say that you can use
'<%%' etc for ASP, JSP.) Actually, i find that if those tags aren't
set off by themselves and surrounded by blank lines, they're hard to
pick out of HTML templates.

http://www.manning.com/books/herrington
 
J

James Edward Gray II

It looks like the % only works if it's at the beginning of the
line. It
won't work if there's tab characters before it. Is there a way to
change this?

I don't believe so, no.

James Edward Gray II
 
J

James Edward Gray II

Gavin Kistner wrote:
[erb]
% nps.each do |thug|
% _erbout << "The thug's name is: #{thug.name}"
% end

Furthermore, if erb is supposed to send its output to STDOUT,
there's no
problem to simply use "puts" here.

Yes there is. A call to puts() would be resolved when it is
interpreted, while the rest of the template output would come out
once the whole thing has been generated. The output would be out of
order. Avoid puts() in ERb, or use the patch floating around that
makes it work as expected there.

James Edward Gray II
 

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
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top