views:

111

answers:

2

Hi! A newbie question.

Do languages like e.g. Ruby (if running MRI, I mean not compiled to byte-code) run actually parsed everytime when an execution of, e.g., method or loop body is needed? I mean, to execute a loop, you need to parse its body N times?

I just always thought that all these programs are being parsed one time at the bootstrap, transformed in a ‘strongly-typed’ statements tree, etc. Is that not true?

+3  A: 

Interpreted is a word with a very loose definition. Even machine code instructions are interpreted by the processor.

In general a distinction is made between languages which are compiled before they are run and languages which do not have a compilation process and are run inside another program, called the interpreter. The latter types of languages are often referred to as interpreted languages.

The line is not that clear in some cases:

  • Some languages can be either compiled or interpreted, e.g. PHP.
  • Some interpreted code might be compiled at runtime into native machine instructions (JIT compilation).
  • Some compiled languages may have an exec functionality which allows code to be generated and executed at run-time, bypassing the normal compilation process.
Mark Byers
+2  A: 

I'll just be mean now and say that just about EVERY programming language is interpreted, whether it's a software (Ruby, Python, Java) or a hardware (C, C++) interpreter :)

For a real answer, while I don't know about the internal Ruby implementation, I'm sure as hell they don't parse the statement over and over and over again. While not using bytecode in the original implementation (or did they migrate already?), they use some intermediate representation (you can see it nicely when you want to write C-extensions for it), thus just executing those over and over again.

LukeN
" they use some intermediate representation (you can see it nicely when you want to write C-extensions for it) " - assuming it is stored in-memory, what could this representation look alike?
Bubba88
For the original implementation written in C, it's a series of struct's called "VALUE". Here's a little insight: http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html
LukeN
"While not using bytecode in the original implementation (or did they migrate already?)" The official ruby interpreter uses bytecode since version 1.9.
sepp2k