I started to learn about compilers and interpreters. But I still can't understand how program execution happens under the hood.
This is what I have understood so far.
Compiler
The Compiler does not run a code, instead, just converts the code into another (low-level) code. This part is clear to me.
Interpreter
First I thought the interpreter converts some high-level code or byte code into machine language and then executes it. But then I realized that interpreters not convert code into machine code, instead, that instruction becomes an input to the interpreter application and it activate some particular function of the interpreter, which has been already converted into the machine code.
Refer to this pseudo code. Imagine I create a programming language that is interpreted by an interpreter created by python.
add 5 + 3
sub 5 - 3
Interpreter
def add(a, b):
print(a+b)
def sub(a,b)
print(a-b)
# Parsing
code = [] #list of instructinos
for instruction in code:
if instruction.command == "add":
add(instruction.a, instruction.b)
elif instruction.command == "sub":
sub(instruction.a, instruction.b)
else:
print("incorrect instruction)
I am not sure whether my understanding of the interpreter is correct. If it is not, I want to know how the interpreter works internally. I mean how the interpreter executes a code without translating it into machine code.