trying to understand irb: why does it have its own lexer, instead of forwarding input to ruby ?

J

Joachim (München)

Dear experts,

I consider (ab)using irb as a starting point for developing a
scripteable
command interpreter for a special-purpose application. Before
deciding on this design, I need to better understand how irb works.

As far as I can see, input to irb is not forwarded to ruby's
interpreter
(which written in C and using yacc, if I understand correctly);
instead, irb has its own interpreter (written in ruby) which basically
duplicates ruby's interpreter.

Could you possibly explain me the reasons for this design decision?

Thank you very much, Joachim
 
R

Robert Klemme

Joachim said:
Dear experts,

I consider (ab)using irb as a starting point for developing a
scripteable
command interpreter for a special-purpose application. Before
deciding on this design, I need to better understand how irb works.

As far as I can see, input to irb is not forwarded to ruby's
interpreter
(which written in C and using yacc, if I understand correctly);
instead, irb has its own interpreter (written in ruby) which basically
duplicates ruby's interpreter.

Could you possibly explain me the reasons for this design decision?

Certain things are handled differently in IRB, namely local variables
and error conditions (exceptions). Also, IRB has to read and parse code
on a line by line basis while the main interpreter gets to see a
complete set of source code lines at a time.

These are just a few things I can think of.

Kind regards

robert
 
J

Joachim (München)

Robert and Tim, thank you for your clear explanations.

As mentioned before, I want to extend the ruby language.
Specifically, I want to twist the interpreter so that it expands
for instance w8s6ny into w[8].s[6].y.size(). Do you experts
think this can be achieved by modifying IRB's ruby code,
without diving into Ruby's C code?

Kind regards, Joachim
 
R

Robert Klemme

Joachim said:
Robert and Tim, thank you for your clear explanations.

As mentioned before, I want to extend the ruby language.
Specifically, I want to twist the interpreter so that it expands
for instance w8s6ny into w[8].s[6].y.size(). Do you experts
think this can be achieved by modifying IRB's ruby code,
without diving into Ruby's C code?

It depends on what rules should guide the translation. Here's a
simplistic example:

irb(main):003:0> "w8s6ny".scan(/../).map do |m|
irb(main):004:1* case m
irb(main):005:2> when /(\w)(\d)/
irb(main):006:2> then "#$1[#$2]"
irb(main):007:2> when /n(\w)/
irb(main):008:2> then "#$1.size()"
irb(main):009:2> else
irb(main):010:2* m
irb(main):011:2> end
irb(main):012:1> end.join "."
=> "w[8].s[6].y.size()"

Cheers

robert
 
J

Joachim (München)

As mentioned before, I want to extend the ruby language.
Specifically, I want to twist the interpreter so that it expands
for instance w8s6ny into w[8].s[6].y.size(). Do you experts
think this can be achieved by modifying IRB's ruby code,
without diving into Ruby's C code?

It depends on what rules should guide the translation. Here's a
simplistic example:

irb(main):003:0> "w8s6ny".scan(/../).map do |m|
irb(main):004:1* case m
irb(main):005:2> when /(\w)(\d)/
irb(main):006:2> then "#$1[#$2]"
irb(main):007:2> when /n(\w)/
irb(main):008:2> then "#$1.size()"
irb(main):009:2> else
irb(main):010:2* m
irb(main):011:2> end
irb(main):012:1> end.join "."
=> "w[8].s[6].y.size()"

This is a two-pass solution: first expand whatever I defined on top of
Ruby, then hand the result down to IRB. Worth thinking about. My
original intention was a one-pass solution: extended-IRB should
recognise w8 in the very same way as IRB recognises w[8].

Thanks again, Joachim
 
D

Dave Burt

Joachim said:
Robert and Tim, thank you for your clear explanations.

As mentioned before, I want to extend the ruby language.
Specifically, I want to twist the interpreter so that it expands
for instance w8s6ny into w[8].s[6].y.size(). Do you experts
think this can be achieved by modifying IRB's ruby code,
without diving into Ruby's C code?

Yes, easily.

You could put code like Robert's in the RubyLex#buf_input method (in
irb/ruby-lex.rb), for example, after @input.call but before @rests.concat.

Cheers,
Dave
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top