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?
45
Upvotes
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.