r/AskComputerScience • • Dec 11 '24
I’m in HS computer science and would like to know, how can one computer understand and compile every programming language?
.
5
Upvotes
r/AskComputerScience • • Dec 11 '24
.
15
u/bennyE31 Dec 11 '24
The "computer" doesn't compile a program on its own. There is a special piece of software (usually called the compiler) that knows how to translate high level code into instructions specific to the computer.
9
u/dmazzoni Dec 11 '24
Every computer has a main processor (the cpu). That processor knows one language, which we normally call "machine language".
Each type of processor has its own machine language - like ARM, used in newer Macs and most phones, or Intel x86-64, used in most Windows PCs.
The cpu ONLY understands that one language.
People wrote compilers and interpreters that turn other languages into that machine language. So you can run lots of programming languages by installing a compiler or interpreter for that language.
The compiler is just another program. It's in machine language. The very first compilers had to be written in machine language. Now even compilers can be written in other languages, and compiled into machine language.
1
u/MasterGeekMX BSCS Dec 12 '24
Computers don't understand languages. They only know machine code, which are the specific instructions the CPU can understand. Different CPU architectures have different sets of instructions, so the ones that x86 CPUs have (like the ones in desktops and laptops) are different than the ones that ARM CPUs have (the ones found in phones and tablets).
All code needs to be converted into this machine code. This is done in two ways: a compiler or an interpreter.
A compiler is a program that takes the code of some program and spits out the machine code that runs said program. If you make changes to the source code, you need to re-compile the code again so changes are reflected.
An interpreter on the other hand reads the source code of a program, but it does both translation and running on the fly, so you don't need to compile the code in order to run it, and instead you run the code "directly".
Now, if you don't have installed an interpreter or compiler for your CPU, there is no way to run the code unless you manually do the translation and craft the runnable file by hand, but that is quite technical.
3
u/aagee Dec 11 '24
Can you give some details on what is tripping you up?
3
u/KyleRochi Dec 11 '24
You should read the book "CODE" by Charles Petzold: https://www.codehiddenlanguage.com/Chapter00/
1
u/piecat Dec 12 '24
If I gave you a written recipe, could you follow it?
If I gave you a book of recipes, you could follow any of them?
If I gave you an internet of recipes, how could one human understand and make any one of those recipes?
1
u/found_on_web Dec 14 '24
It’s all just like 1s and 0s man