r/cprogramming • u/Maleficent_Bee196 • Feb 16 '26
Why don't interpreted languages talk about the specifications of their interpreters?
forgive my dumb question, I'm not too smart. Maybe I didn't search enough, but I will create this post even so.
I mean... when I was learning C, one of the first steps was understanding how the compiler and some peculiar behaviors of it.
Now I'm learning Ruby and feel a bit confused about how the phrase "all is an object" works on the interpreter level. I mean, how the interpreter assemble the first classes, and how does it construct the hierarchy (I'm learning about OOP also, so maybe it's one of the reasons that I cannot absorb it).
I simply don't know if I'm excessively curious trying to understand or if it's a real thing.
If you guys have some materials about this, please, share. I'll be glad. Currently I'm reading "The Little Book Of Ruby" by Huw Collingbourne.
Thanks for reading.
21
u/EpochVanquisher Feb 16 '26
One of the issues here is that some of the more popular languages have more than one interpreter.
Here’s a list of Ruby implementations: Ruby: Alternative Implementations
This is more or less the norm for popular languages. Python, Lisp, JavaScript, Scheme, Java, and C# all have multiple implementations. Same as for C and C++.
If you are interested in how interpreters work, maybe it would help to read a book that focuses on interpreters. There are a lot of different ways you can make interpreters, and a lot of weird techniques you can use to speed things up. Or you can find a book specific to Ruby: Ruby Under a Microscope
On the interpreter level, “all is an object” means (more or less) that every Ruby value has the type “object”. So if you take the C code:
The Ruby code is more like this:
Unfortunately, the exact way this works is a little complicated, because CRuby uses something called tagging to store immediate values (like small numbers) and pointers in the same type. This is a common way to save memory in languages with dynamically typed values.
4
u/WittyStick Feb 17 '26 edited Feb 17 '26
There are too many choices on how to implement, and they're usually discussed (if at all), in the comments in source code.
A good introductory resource is Gudeman's Representing Type Information in Dynamically Typed Languages, which covers a range of well-known techniques - however, this is slightly dated (1993), and there are several more "modern" techniques which have better performance characteristics.
The key consideration of all these techniques is that we want to represent a value and its type in a fixed size datum, such as a 16/32/64-byte struct, or even just an 8-byte machine word (or 4-bytes on a 32-bit machine). We reserve some bits of this for a type tag, and other bits for a payload - where this payload may contain one or more pointers to a memory location which can provide larger payloads or additional type information.
As a trivial example, consider a
struct { int64_t tag; intptr_t payload; }. This fits into 128-bits (16-bytes), and can be passed and returned by value on 64-bit SYSV platforms in two hardware registers. The payload is sufficiently sized to hold 64-bit values, which includes pointers,doubleanduint64_t/int64_t. We have enoughtagvalues to not need to worry about running out, so every type in the type system can just be given a unique ID - and the information about the type can be held in the pointed-to location, or we have have a global map oftag->typeinfo.In regards to "type hierarchies", again there are too many implementation choices, but abstractly, subtyping is considered using a partial ordering (
≼), which is a reflexive and transitive closure over types.A ≼ BmeansAis a subtype ofB, so ifB ≼ C, thenAis also a subtype ofC(transitivity), andCis also a subtype ofC(reflexivity). Thus, we can test if types are compatible based on their ordering.Expanding on the partial ordering we can also define a least upper bound (
⊔) of two or more types, which may represent a union, an interface, abstract base class, or row polymorphic type. Conversely, a greatest lower bound (⊓) can indicate a type that is a subtype of more than one other type, which can be used to represent multiple inheritance, intersection types, an so forth. We use bounded lattices in a typical type system, where the LUB is bounded by a "top type" (⊤), which represents any, as all other types are a subtype of top, and the GLB is bounded by a "bottom type" (⊥), which is a subtype of every other type, and uninhabited by any value.2
u/kombiwombi Feb 17 '26
One thing which hasn't been mentioned is the idea of a "programming language contract", ironically from the ANSI C standardisation committee.
The idea is that many aspects of a language aren't defined, but are implementation details of the complier or interpreter. Similarly, the aspects of the language which are defined can be absolutely relied upon by the application programmer.
This allows a wide range of C compilers. If you exceed the words of the contract and rely upon an implementation details of the compiler, well the trouble that brings is on you.
Python has much the same view. Although there is a canonical implementation of the interpreter, cpython is not what defines the language.
So there is an argument that you can and should program as if you have no insight into the compiler or interpreter.
2
u/Hot-Profession4091 Feb 18 '26
You’ve gotten good answers already, but if you want a good way to learn about how interpreters (and compilers too, really) work, here’s a great way to learn. You build one step by step.
http://www.craftinginterpreters.com/
2
u/integerdivision Feb 16 '26
In Ruby, Python, Javascript, and many other interpreted languages, the object is fundamental. You don’t have to build it from scratch. This makes these languages much easier to get the hang of, but you are at a higher level (not close to the metal) so have to deal with the performance cost — they can be orders of magnitude slower than compiled languages. It’s the simplicity/performance tradeoff.
Interpreted languages tend not to talk about their low-level implementation because they are usually focused on ease of use.
(Also, by POO, I assume you mean OOP, but many would contend your acronym is more apt.)
1
u/dkopgerpgdolfg Feb 16 '26
A question to ask yourself: Which one?
Most things you learned won't apply to all C compilers. And C doesn't need any compiler, it can be interpreted too.
The most popular "interpreted" languages tend to have multiple different interpreters available, as well as some solutions to compile it to native executables.
That's your answer - the language is independent of such details, and you don't do yourself a favor by mixing them.
6
u/zhivago Feb 16 '26
The most fundamental error here is the term "interpreted language".
Interpretation is an implementation strategy, not a property of a language.
There are C interpreters.
ls C an "interpreted language"?
2
u/gwenbeth Feb 17 '26
Because the interpreter is just one possible implementation of the language. Even in c there are implementation differences that are not defined by the language, like how big is an int in terms of value, sizeof(int), sizeof(char), sizeof(void*), the order of ++ and -- operations, etc
1
u/Blothorn Feb 16 '26
1
u/[deleted] Feb 17 '26
A language is specified at some abstract level. That's a deliberate choice and that's where it ends. If you want to "run" it on an /actual hardware/, you need to bridge the gap between this abstract level and the hardware (with an interpreter or a conpiler).
1
u/Ndugutime Feb 17 '26 edited Feb 17 '26
Even though Ruby is a focus. You might like Anthony Shaw’s CPython Internals: Your Guide to the Python 3 Interpreter. He even talks about how to make a mod
There is a language spec. But under the hood there are lots of choices.
Python at 3.3 went to this string implementation, at the language level all the same but different version of a compiler or interpreter can vary at this level
I did an article on medium about various representations of strings
Read “The new Empire of Strings“ by jon allen on Medium: https://medium.com/@jallenswrx2016/the-new-empire-of-strings-ac2aa41d8592
1
u/keelanstuart Feb 18 '26
I have read some wild, wild things about Perl...
1
u/binarycow Feb 21 '26
Why should the language care how the interpreter works?
There is a defined "contract" that an interpreter/compiler/execution environment must meet. Anything beyond that is fair game.
It doesn't matter how the interpreter does it.
What matters is that the type hierarchy has a "top type" - the type that all other types derive from.
-1
u/Pale_Height_1251 Feb 16 '26
Strictly speaking there are no interpreted or compiled languages. The language design is distinct from the various implementations
That's why we have C compilers as well as C interpreters, because the language design doesn't specify any particular implementation.
So Python the language is distinct from the dozens of Python implementations.