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

The "Generation & Babel generate" 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 explains what generation, or parsing in reverse is, and gives an overview of the babel generator.

Preview
Close

Transcript from the "Generation & Babel generate" Lesson

[00:00:00]
>> Steve Kinney: We'll talk a little bit about generation. And generation is effectively parsing in reverse, right? You have taken your syntax, you've figured out all the tokens, you've figured out it's semantic meaning and all of those things, and now you want to do a thing with it. We evaluated it, but if we wanted to turn it into some other compile time target, this is where that would happen.

[00:00:23]
And there's a lot of options, you can write your own low level CPU instruction compiler. That's a great idea. You can use a compiler framework like LLVM, which is even what like Rust target, Swift, I think of Objective C these days. With LLVM you can actually target web assembly with a target as well, right?

[00:00:47]
And so you can create a web assembly first language that you wanna use and target the JVM, the Java Virtual Machine. And you can transpile it, right? Which is gonna be the avenue that we take cuz I think at this point is, I'm building this for the browser or to do JavaScript stuff with it.

[00:01:05]
It's heavily reliant on JavaScript as its effective platform that it's running on. So that makes the most sense for us. Now, when you wanna target another language, you have a few options, right? You can, our language is simple right now. If we wanted to, we could say, all right, I got you an identifier, right?

[00:01:24]
Read the identifier, then put a bracket, then join all the arguments with a comma in between, then put the closing brace and a semicolon, right? And as our language grows, we could customize this and do more and more. And that's a fine option, and maybe this language for you isn't targeting JavaScript, right?

[00:01:43]
Maybe it's targeting, maybe you are trying to output CPU or Bash, or something along those lines. Those are options and things you can absolutely do. The other option that you have is, and we kinda talked about this a little bit earlier, is that you can traverse that tree and manipulate it, and get into the point where it becomes a tree that some other generator that exists how to use.

[00:02:06]
And so we saw that, there is a specification, kind of that is an agreement in the JavaScript community. If the tree meets these facts, most generators can then generate some amount of the syntax based on it, right? And the Babel one is actually pretty forgiving. We saw in the AST explorer, a lot of the property, the Babel parses and makes available.

[00:02:37]
And those are super useful for things like source maps, and stuff along those lines. But, the actual amount that you need to able to be truly generate stuff is a lot smaller. We also I mentioned earlier made some like purposeful choices to kind of already be kind of close here to the Babel specs.

[00:02:59]
All we have to do is kind of massage it the rest of the way, and kind of get a taste of that. So the Babel generator, right? And there's a lot of tools, Babel has not only just Babel thing that we use, but it's got a lot of tools to it through traversing trees and stuff along those lines.

[00:03:19]
We're gonna write some more about ourselves and rely too much on Babel for that. But the generator is super useful because joining arrays of arguments with commas is fun, but I'd rather not. It takes effectively three options. The code is kind of important for the source maps. So really, as long as we give it an abstract syntax tree, which guess what?

[00:03:41]
We have, right? As long as we get an abstract syntax tree, then we should be able to spit out that code, right? So, what we need to do, is we need to figure out a way to transform our existing abstract syntax tree into one that Babel can understand, right?

[00:04:01]
We kind of a taste for that, right? And you can kind of like take it to its natural conclusion as well.

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