Building Your Own Programming Language

Compilers & Interpreters Overview

Steve Kinney

Steve Kinney

Building Your Own Programming Language

Check out a free preview of the full Building Your Own Programming Language course

The "Compilers & Interpreters Overview" Lesson is part of the full, Building Your Own Programming Language course featured in this preview video. Here's what you'd learn in this lesson:

Steve defines what a compiler is, what the different stages of a compiler are, and what an interpreter is.


Transcript from the "Compilers & Interpreters Overview" Lesson

>> Steve Kinney: So let's handle the kind of terminology part of the show. What even is a compiler, right? The kind of like simple answer, it is something that turns a higher-level language into a lower-level language. I put an asterisk there cuz I don't know, we know that a lot of languages these days compile to what, JavaScript, right?

It'll be interesting to see how much of that changes to WebAssembly and stuff along those lines. But right now, many, many, many languages, even JavaScript is effectively compiled to JavaScript, right? As kind of the lingua franca of the browser, and the only game in town. So yeah, it could be a higher-level language to a lower-level language.

But all things considered, JavaScript is a fairly high-level language, right? But ReasonML, Elm, CoffeeScript, right, those are all languages that kind of go from one kind of browser-based language to another. And at some point, even JavaScript, right, is effectively, that parser and compiler is written in C++, right?

And so it's just from one language to another language in a lot of cases. Source code is meant to be human readable, right? And so it's the compiler's job to figure that out, right? And so there's a lot of languages, like this lovely one here whose name I won't mention cuz it has a bad word in it.

Mark told me I was allowed to use the word, but I'm not going to [LAUGH].
>> Speaker 2: Brain f.
>> Steve Kinney: Brain f, so yeah, this kind of breaks the rule of a high-level language into a lower-level language. It's basically shifting bits around to do things. This console logs hello world, in case you're wondering.

And yes, I did spend the time to pull it into a repl, transpile it into this to make this slide, you're welcome. So yeah, this kinda breaks the role of a higher-level language that gets transpiled into a lower-level language because it's kinda the opposite, but I think it makes a general point.

So really, a compiler is just a bunch of smaller programs that are running. We're gonna actually play with both a little bit. We're gonna stick one toe into playing around with compilers. We'll stick one toe into playing around with an interpreter, right, the thing that actually runs our code.

We're gonna build a repl, we'll build something that you can just run a command line tool and evaluate the code. We'll also talk a little bit about like pulling apart an AST, rearranging it so we can just hand it off to Babel and turn it into JavaScript. Right, so we're kind of gonna have our cake and eat it too, and have one foot in both worlds here.

But the parts of a compiler, effectively, you've got parsing, transformation, and generation, right? Parsing is, your source code, at the end of the day, is just a big old string of text, [LAUGH] that you could read off the file system you can pull in from the network, so on and so forth.

And you've got to make some kind of sense of that, right? And parsing, we'll see, has effectively two parts, right? First of all, breaking it into what we call tokens, right, the little tiny pieces, what is a word, okay, that's a for, that's an if, right, so on and so forth, right?

And then once we have all those little tokens, right, then we can go ahead and figure out, all right, what dpes it all mean, right, so there's kinda two stages there. Once we've figured out what the code is and what it means, to get it to that, whether it's a lower-level language or, in Brain f's case, a higher-level one, right, we have to go and transform it into that target, right?

And that could be JavaScript, you could also target LLVM and compile to machine code or even web assembly through that, right? You can target the JVM if you want, right? There's a bunch of different options that you can kind of look at. And then finally, generating that code, once we've kind of taken the general, when we parse, we're trying to figure out what the code means in our language.

And when we're transforming it, we're trying to figure out how to put that in the logic that the target language can understand. And then we kind of generate that language out, right? And what we're gonna do in this course is effectively take one or two small, little examples, and basically use it so that we can basically turn the tree into a tree that Babel's generator just understands.

And we see JavaScript spit out the other side. Right, we'll just do it with effectively our own syntax into just traditional function calls, but I think it'll kind of show how it works. Also talk a little bit about an interpreter, right, interpreter isn't necessarily going ahead and turning it into different source code, it's actually running it, right?

And we will leverage JavaScript to make that happen, right? We're not gonna have to implement the idea of an array ourself or anything along those lines. But we'll have the ability to kind of take code and execute it by leveraging another platform, right?

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now