r/AskComputerScience • u/lepriccon22 • Jul 11 '16
How do you program a programming language?
I.e. How do you get a computer to understand a language? How does it actually make the transition from that language into assembly language?
3
u/anamorphism Jul 11 '16
you write a compiler or interpreter.
1
u/lepriccon22 Jul 11 '16
What does that mean practically? How do I teach a computer to understand what naming a variable is? How do I teach it to understand what a for loop is, etc?
5
u/anamorphism Jul 11 '16 edited Jul 11 '16
you're not teaching the computer anything.
you're basically just writing a translation program.
you would write a program that takes in
for (int i = 0; i < 10; ++i);and outputs assembly to represent what you want that line of code to do.something like
allocate 32 bits of memory for i
write 0 to all of those bits
allocate 32 bits for 10
write 10 to those bits
declare label 1
compare i to 10
jump to label 2 if i is less than 10
add 1 to i
jump to label 1
declare label 2
1
u/lepriccon22 Jul 12 '16
allocate 32 bits of memory for i write 0 to all of those bits allocate 32 bits for 10 write 10 to those bits declare label 1 compare i to 10 jump to label 2 if i is less than 10 add 1 to i jump to label 1 declare label 2
How does it know how to understand this, though?
1
u/juuular Jul 12 '16
At the end of the day these simple statements like these (move bit A to spot B, etc) boil down to hardware instructions (aka just 1's and 0's). At the very bottom is the circuit board - different electronic parts that interact through electricity.
The most important part is the transistor - you can apply some voltage to "set" it to 1 of 2 states, and you can also apply some voltage differently to get which state it is in. That "move bit" command would be converted to an electrical signal to a transistor that makes it output it's state, and then route that signal to a different transistor that gets set.
This is massively oversimplified of course, but at the end of the day these instructions are converted into electrical signals being sent from one place to another and changing the state of different electrical devices.
Theoretically you could implement a CPU (and "compile" assembly instructions) by having slaves run around shuffling notecards with different numbers on them, just like we shuffle numbers around on silicon transistors. That would be much slower and more memory inefficient, though.
6
Jul 12 '16 edited Jun 18 '23
[removed] — view removed comment
2
u/anamorphism Jul 12 '16
sure, but you don't try and teach someone who doesn't know algebra multivariable calculus.
2
u/giggly_kisses Jul 12 '16
That talk with Anders Hejlsberg was very interesting. Thanks for sharing!
1
u/CQQL Jul 11 '16
The book Structure and Interpretation of Computer Programs is a great introduction into that topic.
16
u/simonorono Jul 11 '16
First, you define your language. Define its syntax, its semantics, everything.
Then, it comes the hardest part: developing a compiler/interpreter for it.
A compiler is just a translator, it takes source code from the language you defined and pours the equivalent program in another programming language (generally, with a lower level).
For example, the C compilers, read C code and translate them to assembly, them an assembler converts this assembly code into an executable.
3
u/trenchgun Jul 12 '16
So C-compiler is written in assembly? And assembler is written in machine code?
12
u/Teslatronic Jul 12 '16
The first compilers/assemblers were. But once you write one C compiler in assembly, you can write another one in C itself, and use the assembly-based compiler to compile the C-based one, and then compile each new version of the compiler with the previous one. This is called bootstrapping.
Edit: see /u/bhrgunatha's comment for more detail.
7
u/PastyPilgrim Jul 12 '16
This is the kind of thing that takes years of study to become proficient at, so it's very difficult to explain in just a handful of words but I'll try.
The core of it is that you need to write a compiler. A compiler takes your code and all of its friendly syntax, and turns it into assembly (the simplest instructions understood by your computer). Then an assembler takes your assembly and turns it into machine code (0s and 1s). The assembler's job is super easy because there's usually a 1 to 1 correspondence between a line of assembly and a line of machine code (each line of assembly becomes precisely one line of machine code and vice versa (with the exception of pseudo instructions)).
So the hard part in writing a language is writing the compiler, but that's usually because your compiler does a ton of optimization to make what you write run especially fast on your specific configuration of hardware. It wouldn't be very difficult to write a simple compiler, it just wouldn't be very efficient.
Anyway, assembly itself is already a pretty capable language (in most implementations), you can do any and everything (by definition) in assembly. So the core of your compiler will just be recognizing the syntax you create and translating that into the equivalent assembly code, which is then translated into machine code for whatever machine is running the code.
For example, assembly doesn't really have variables. Instead, you have registers (lightning fast little memory cells that are used to perform mathematical operations) and memory (slow but plentiful long-term storage). You can't keep everything in registers because there aren't enough of them. So what you might do to get variables in your language is to use a lookup table. To do this, you'll create a table in memory that maps a variable name to a type (if desired) and the location in memory where the value is stored. This way, when I create a variable in my language, my compiler will translate that instantiation into the instructions needed to allocate memory for the variable's value and to store the metadata in the lookup table. Then, when I use the variable, my compiler will translate my syntax into the equivalent assembly instructions needed to read the table and then load the value into a register (so that I can do work on it).
That's a compiler in a nutshell. You come up with rules, grammar, and syntax for a language, then you write the necessary code for understanding those rules and translating them into the equivalent assembly code, which is then translated into machine code.
What's interesting, is that in the modern day you don't even need to write your compiler in assembly. Instead, most languages use a language like C to implement their compiler, which has its compiler(s) written in assembly. By using a well-developed language like C, you can exploit the heavy optimization that other people that really know hardware and assembly are doing, and take advantage of the fact that C is easier (and more portable) to write than assembly.