r/AskComputerScience • • 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

18 comments sorted by

View all comments

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.

2

u/lepriccon22 Jul 12 '16

Awesome, thanks :)

2

u/lordvadr Jul 12 '16

We wrote a compiler for one of my CS classes in college. The language was called YAPL (yet another programming language).

First thing first, as other's have mentioned, a compiler translates from one language to another...typically assembly...but could be any other language. Our compiler compiled YAPL, which was a lot like Pascal, into C, which we then fed to the C compiler...which in turn was fed to the assembler. We actually wrote working programs in YAPL. For my final project, I wrote a functional--albeit VERY basic--web server.

With that said, it's quite a bit different for an interpreted language, but the biggest part for each is still the same. By far, the most complicated part of a compiler is the parser.

The parser is what reads a source code file and does whatever it's going to do with it. Entire bookshelves have been written on this subject, and PhD's given out on the matter, so parsing can be extremely complicated.

In a theoretical sense, higher level languages abstract common or more complicated tasks from the lower level languages. For example, to a CPU, variables don't have sizes or names, neither do functions, etc. On one hand, it greatly speeds up development because the code is far more understandable. On the other hand, certain tricks you can pull of in the lower-level languages (that can vastly improve performance) can be abstracted away. This trade-off is mostly considered acceptable. An extra $500 web server (or 100 for that matter) to handle some of the load is far less expensive than 10 extra $100,000 a year x86 assembly developers to develop, optimize, and debug lower level code.

So generally speaking, the parser looks for what are called tokens, which is why there are reserved words in languages. You can't name a variable int in C because int is a reserved word for a type. So when you name variable, you're simply telling the compiler "when I reference this name again, I'm talking about the same variable." The compiler knows an int is 4 bytes, so does the developer. When it makes it into assembly, it's just some 4 bytes somewhere in memory.

So the parser starts looking for keywords or symbols. When it sees int, the next thing it's going to expect is a label, and if that label is followed by (, it knows it's a function, if it's followed by ; it's a variable--it's more complicated than this but you get the idea.

The parser builds a big structure in memory of what's what and essentially the functionality. From there, either the interpreter goes through and interprets the language, or for a compiler, that gets handed to what's called the emitter. The emitter is the function that spits out the assembly (or whatever other language) equivalent a = b + c; happens to be.

This is complicated, but if you take it in steps, it's not really that hard. This is the book we used. There's a much newer version out now. If I can find my copy, I'll give it to you if you pay shipping. PM me.