0

Are all interpreted languages not eventually machine code? I'm curious if the reason is because companies don't think it's worth the effort, if there is an inherit conflict that makes it impossible, or some other reason. Is there a way to capture the script's executed machine code myself?

Edit I was speaking loosely because I didn't want the title to be too long. I understand there are no "interpreted languages". I'm talking about languages that are generally interpreted ( not c++, not rust, etc.. ).

"Are all interpreted languages not eventually machine code?" - this was a rhetorical question. The answer is a simple "yes". Because that's how computers work.

I am curious why most companies who create a language with an interpreter don't also supplement it with a compiler (that compiles to native machine code). And I'm curious if I can record the executed machine code myself.

Also, Jörg W Mittag's answer is misleading (and arrogant). Out of "all" these compilers I don't see one that compiles to native machine code. I don't think one of them even exists anymore (go to the Rubinius website). I've also worked with some of them and they have limitations (I can't arbitrarily take a non-trivial script that works with the standard ruby interpreter and compile it).

  • 2
    An interpreted language is not translated to machine code. This is why there is the word "interpreted". For an interpreted language you need an interpreter. This interpreter is the engine that sits between the CPU (machine code), and the language itself. The compiler compiles the script language into an intermediate code for the interpreter, it does not compile to machine code. Hence you can not just replace the compiler. You would need to build a new compiler AND interpreter, and this takes a LOT of work. – Casper May 23 '21 at 15:29
  • So your answer is companies don't think it's worth the effort. Thanks! –  May 23 '21 at 15:45
  • In ancient times an interpreter could be used to reduce the memory footprint, but an interpreter also allows programs to be interactively developed and shared across implementations. There are compilers for a variety of scripting languages such as Ruby and Python, so it would depend on your target (or market). – red_menace May 23 '21 at 15:50
  • All currently existing Ruby implementations have a compiler. Many even have more than one, e.g. YARV has two, Rubinius has two, JRuby arguably has between two and six, depending on how you count. Opal is a pure compiler, it doesn't have an interpreter at all. – Jörg W Mittag May 23 '21 at 18:54
  • @Casper Facebook actually did that for PHP with HHVM. Of course they had to actually fork the entire language to get it working. – max May 23 '21 at 19:25
  • I would love to see a command I could write in Ubuntu that compiles a ruby script that requires the amazon dynamodb gem. Thanks! –  May 23 '21 at 21:46
  • @max: "Of course they had to actually fork the entire language to get it working" – Really? I was under the impression that Hack is a proper superset of PHP, meaning that every semantically valid PHP program is a semantically valid Hack program. – Jörg W Mittag May 24 '21 at 08:16
  • @JörgWMittag HHVM runs PHP but you don't get the same speed benefits as with Hack. – max May 24 '21 at 08:24
  • Any interpreter can be mechanically turned into a compiler - see Futamura projections. – SK-logic Oct 14 '21 at 07:53

1 Answers1

0

Why don't most interpreted languages like ruby provide an optional compiler?

There is no such thing as an "interpreted language". Interpretation and compilation are traits of the interpreter or compiler (duh!) not the language. A language is just a set of abstract mathematical rules and restrictions. It is neither interpreted nor compiled. It just is.

Those two terms belong to two completely different levels of abstraction. If English were a typed language, the term "interpreted language" would be a Type Error. The term "interpreted language" is not even wrong, it is non-sensical.

Every language can be implemented by a compiler, and every language can be implemented by an interpreter. Most languages have both interpreted and compiled implementations. Many modern high-performance language implementations combine compilers and interpreters.

Are all interpreted languages not eventually machine code?

In some sense, every language is machine code for an abstract machine corresponding to that language, yes. I.e. Ruby is machine language for the "Ruby Abstract Machine" which is the machine whose execution semantics match exactly the execution semantics of Ruby and whose machine language syntax matches exactly the syntax of Ruby.

if there is an inherit conflict that makes it impossible

All currently existing Ruby implementations (with one caveat) have at least one compiler. Most have more than one. At least one has no interpreter at all.

  • Opal is purely compiled. It never interprets. There is no interpreter in Opal, only a compiler.
  • YARV compiles Ruby to YARV byte code. This byte code then gets interpreted by the YARV VM. Code that has been executed more than a certain number of times then gets compiled to native machine code for the underlying architecture (i.e. when running the AMD64 version of YARV, it gets compiled to AMD64 machine code, when running the ARM version, it gets compiled to ARM machine code, and so on).
  • Artichoke is … somewhat complicated, but suffice to say, it does not interpret Ruby.
  • MRuby compiles Ruby to MRuby byte code. This byte code then gets interpreted by the MRuby VM.
  • Rubinius compiles Ruby to Rubinius byte code. This byte code then gets interpreted by the Rubinius VM. Code that has been executed more than a certain number of times then gets compiled to native machine code for the underlying architecture (i.e. when running the AMD64 version of YARV, it gets compiled to AMD64 machine code, when running the ARM version, it gets compiled to ARM machine code, and so on). [Note: there are a couple of different versions of Rubinius. The original version had a native code compiler. This was then removed, and is in the process of being rewritten.]
  • JRuby compiles Ruby to JRuby IR. This IR then gets interpreted by the JRuby IR interpreter. Code that has been executed more than a certain number of times then gets compiled to JRuby compiler IR. This compiler IR then gets further compiled to JVM byte code. What happens to this JVM byte code depends on the JVM. On the HotSpot JVM, the JVM byte code will be interpreted by the HotSpot interpreter, which will profile the code, and then compile the code that is executed often to native machine code.
  • TruffleRuby parses Ruby to Truffle AST. This AST then gets interpreted by the Truffle AST interpreter framework. The Truffle AST interpreter framework will then specialize the AST nodes while it is interpreting them, including possibly compiling them to native machine code using Graal.

The last major, mainstream Ruby implementation that was purely interpreted and didn't have a compiler, was the original MRI, which was abandoned years ago.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • I’m curious how you would word my question. So are all these compilers compatible with scripts that require like the Amazon dynamodb gem? If you could provide a line you could type into the command line to compile the script that would blow my hair back. –  May 23 '21 at 21:45
  • Well, considering that *every single Ruby implementation that exists* has a compiler, a gem that is not compatible with compilers is completely useless because it will not work on any implementation at all. I haven't checked, but by pure common sense, I would assume that the dynamodb gem *at least* is compatible with YARV. Why don't you try it out yourself? Unless you deliberately did something special, when you "installed Ruby", you likely actually installed YARV. – Jörg W Mittag May 24 '21 at 08:15