r/AskComputerScience • u/No_Cook_2493 • Mar 05 '26
How do interpretted languages run?
So from my understanding, languages like python are not compiled, but are instead interpreted. You compile a python binary that runs your code within its stack.
How does the compiled python "run" this code? Like I can only picture high level code -> assembly code -> binary code as the process of getting runnable code, how do interpreters differ? And if they don't differ, why arent they just compiled instead of interpreted?
12
Upvotes
18
u/8dot30662386292pow2 Mar 05 '26 edited Mar 05 '26
The same way compiled languages run, actually! Compiled languages - even though they are binary - are actually interpreted by the CPU. The CPU has microcode that is used to interpret the raw binary commands, therefore you can later change how it works.
Anyway, You seem to know about python. Let's create an interpreter in python!
In our language we can set variables and print them. Here is an example code:
a = 50
b = 60
print a
print b
Would you be able to make a program that interprets this code? Here we go:
Now try to create jump instruction and arithmetic instructions in this language!
Python is not executed directly though. It is compiled to a byte code. The byte code is then executed, in a very similar way my little interpreter above works.
https://godbolt.org/z/r6Tn5hr8v With this tool you can try all kinds of code and see the compiled output.
2
u/Poddster Mar 05 '26 edited Mar 05 '26
A quick look at your post history shows not only do you program, but you were writing an assembler at some point. So let's imagine this assembly:
In an assembler, you:
Well, interpreters are :
If we think about just that last step, and assume something has already tokenised and built a syntax tree:
I've just mashed that out into the reddit comment box now, so it's poorly thought out in terms of abstraction (e.g. why are we converted some of the strings at the interpreter stage? :)) but it should illustrate what I mean.
If you're looking at it thinking "isn't this just an emulator", then yes. An emulator is an interpreter, and when talking about machine code like this is looks like a CPU emulator. Heck, a CPU itself is an interpreter, we just rarely call it that. The traditional steps of the "instruction cycle" are "fetch, decode, execute". This is identical to the above.
Technically python is compiled. The
pythonprocess first compiles your python source code into "PythonVM byte code", which is just a machine code language, and then it runs that machine code through it's internal interpreter.Java is also converted into machine code, JVM machine code (Java Virtual Machine). We tend to call Java a compiled language and Python an interpreted one because of the semantics of the interpreter (Python does a lot more "late binding" stuff, i.e. at runtime it's actively looking up what function is bound to the name "my_func", whereas in Java they do that stuff at compile time).
Some languages, such as bash, are purely interpreted on the fly and never compiled.
But there's nothing stopping you taking a similar approach to an existing higher level languages. e.g. you can write code to parse and execute this LISP code:
Infact you can get interpreters for languages like C. There's nothing to say that C must be compiled, it's just that it's traditional use case was for compiled environments.
or what about a shell, such a bash? That's just an interpreter that splits up your input into tokens and then runs it, e.g.:
Here bash splits the input into "curl" and "www.google.com", and then based on its syntax it knows the first thing is a program name, so it finds the curl executable on disk, and then insantiates the program using the OS's processess creation calls and passes the parameters to it.
As an exercise, I encourage you to:
1
u/Ronin-s_Spirit Mar 05 '26 edited Mar 05 '26
If we take a really simple example: it's a switch loop in any language you want. I wrote a text preprocessor and a DSL interpreter in JS (need the the interpreter because I can't use
eval). It starts out by reading a list (array actually) of tokens, does some optimizations, and gives that to the interpreter, the interpreter is also a switch loop.All you have to do is have some code that does something when it sees a token, then that code is compiled into a program and that's what runs whatever you're interpreting. You have to write and compile the interpreter itself is what I'm saying.
Of course you don't have to do a stack machine, you can have a more complicated state machine, or a tree interpreter, or something else.
p.s. Minecraft famously will ask people to install the JVM so that it could interpret the files of Minecraft. Afaik the game files are relatively easy to fiddle with (pirate, mod, automate with tools), on the other hand the JVM is in a less human format.
2
u/jibbit Mar 05 '26
you can imagine a calculator app that can evaluate something like: '3 * 2 / 5 - 6' ? add in function calls and you've got an interpreted language
1
u/Ok_Leg_109 Mar 05 '26
You might want to study JonesForth which is a very heavily commented source for a Forth Interpreter.
https://github.com/nornagon/jonesforth/blob/master/jonesforth.S
Forth is an old language that more closely resembles the JVM. It is stack based and very low level. Programmers are expected to code the virtual machine directly, which is an acquired tasted. :-)
However the principles used might give you some insights into _one_ way to build an interpreter.
All part of your journey.
1
u/smarmy1625 Mar 05 '26
imagine simulating a CPU by running a program by hand using paper and pencil.
looking at the instructions, accessing memory, executing the instructions, storing the results back to memory or registers.
(if you're familiar with assembly than surely you're aware that at the lowest levels CPUs are literally just like little machines.)
then just write a program to do that.
anything else is just an optimization to make it run faster.
1
u/Dan13l_N Mar 05 '26 edited Mar 06 '26
It's basically a simulated CPU.
Imagine you have three variables, a, b, c. You keep all variables in an array (or vector etc). You have a line like this:
a = b + cthe first phase can translate it to these numbers:
113 414
After everything is translated, these numbers are "executed". Imagine the first digit means:
1 = load variable
4 = add variables
So, the first number means "store the value in the variable 3 into the variable 1". And variable 3 is where the compiler placed
b, while 1 isa.The next number will add the value in the variable 4 (
c) to thea. And your code is executed, nowacontainsb + c.This is one common way, and the "simulated CPU" is usually called "virtual machine". Its code is basically a loop executing "instruction" after an "instruction", which are all basically just numbers. But this is the same as actual CPU's work. So interpreting is usually compiling for an imagined CPU and then simulating that CPU.
You can learn much more from the book Crafting Interpreters. It can be read online and it teaches you how to write your own interpreter in 2 ways.
The last question is an important one. Your imaginary CPU can have everything you need. It can have thousands of memory locations which can be used for variables, for example.
Real CPU's are complicated. You can have 5 registers you can use for variables. But they hold only integer numbers. Then you can have additional registers which can be used for floating-point numbers. But then your CPU could allow only some operations with some registers.
For example, your ideal CPU allows you to perform integer division between any two variables.
But real Intel CPU's don't allow that. If you want to divide 2 integers, you need to use specific registers! OK, you will copy numbers to these registers. But what if you decided to use these registers to hold some immediate values or other variables? You can move these values to stack, and remember that these variables are now on stack. But when you want to do some operations, you have to move them again to registers. You get the picture.
Your imagined CPU can contain 100 "instructions" that allow any variable as an argument. Your real CPU could have 1000 instructions with huge differences in their flexibility.
Writing a compiler for a real CPU can be really difficult, and compilers typically have 5-10 more lines of code than interpreters.
edit: grammar, style...
1
u/SCD_minecraft Mar 06 '26
Do you know command blocks and commands in Minecraft?
It too is interpreted language
Interpreter is a middle man in code execution
Binary written for linux won't work on windows, but Minecraft world with command blocks will work on every copy of Minecraft
Middle man lets you write code without worry of system specyfic things