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

The "Transform Practice" 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 live codes the traversing and transforming, and builds a new visitor function that can traverse the AST. Visitors add different ways to process items, in the instance of dropbear, a visitor modifies a node after traversing the tree.


Transcript from the "Transform Practice" Lesson

>> Steve Kinney: So my challenge to you is with these we wrote the reversal together, great, but now we have to figure out some of this transformation part right? And so my challenge for you is to instead taking some of that written, to write your own right? You're gonna need to modify your language, so I'm gonna take it for a spin.

All right, so let's try it together.
>> Steve Kinney: I'm not gonna give this a particularly good test name.
>> Steve Kinney: All right, anyone have any strong opinions on what we should do? If not, you're gonna leave me time [CROSSTALK].
>> Speaker 2: I did an inverse function.
>> Steve Kinney: Like swapping the arguments?

>> Speaker 2: Negative one exponent. You could swap. That's a good one. I like swapping.
>> Steve Kinney: For addition and multiplication, swapping the arguments will do literally nothing. Yeah, we did the inverse. I'm trying to think. We could also change some strings. We could also even if we wanted to add the ability to use a plus operator and swap that in for ad, right?

Let's actually go with that. That one sounds fun, so I'll grab this ast up here.
>> Steve Kinney: And now we wanna add the ability. Maybe it's in a new special syntax for a language where they can use regular operators, which you can do in scheme and closure and stuff along those lines.

We can have that. We wanna actually transpile it to be able to visit all the call express and see, okay if they're a name of plus. And we could extend this further, right? And so if you also wanna try it out yourself as well and maybe swap out minus for subtract, so on and so forth you can do that as well.

Cool, so that means that we care about every single call expression. We don't care about string laterals. I mean, we could have one that upper cases. Again, it's more about practicing the pattern than doing anything particularly useful. This point of our languages is pretty small. [LAUGH] But we can take a look and we can see that the visitor.

>> Steve Kinney: Let's go ahead and make one. We'll say,
>> Steve Kinney: Our visitor,
>> Steve Kinney: Cares about callExpressions.
>> Steve Kinney: And it's gonna do it on entering them.
>> Steve Kinney: So we care mostly about the node. We're not going up to the parents, or anything along those lines, so we'll go ahead and go do that.

And we'll say, if,
>> Steve Kinney:
>> Steve Kinney: Give it a little plus sign.
>> Steve Kinney: You know what? It doesn't anymore.
>> Steve Kinney: All right, and that's really effectively all we need to do because we've put the pattern in place. You get to have this kind of declarative way that you wanna go about doing it.

And so we traverse the tree as you can see up here, right? We've called it an ast. We can go in and visit it, and then it should make all the changes for us, unless we've made some kind of terrible mistake. So we'll traverse.
>> Steve Kinney: Ast, with our visitor, and I think you saw the very first test.

You can have one that cares about multiple nodes. It does a whole series of things, absolutely, and then we'll expect. That,
>> Steve Kinney: To be add. All right, let's run that real quick. Make sure I don't mess anything up because.
>> Steve Kinney: That's not the right one. Why is everything?

I don't want everything.
>> Steve Kinney: Cool, and so it works, right? We can now modify this. Now, we've danced around the fact that one of the reasons you might wanna modify is to have a compile target, right? These have been interesting and fun cuz they made sense. But what if we wanted to go ahead and visit in a way that we could get it ready to work with Babble, right?

So one of the things that you can do is look at what the Babble tree does. And if we get our ast to conform to one of the Babble respects, then we're able to use Babble as a generator, right? So let's go over to that ast explorer. And let's see what an add 2 + 2 function or whatever looks like there, right?

>> Steve Kinney: So let's say, I'm gonna give myself some space here so I don't have that five-times zoom in become a problem. Right, and so we know that our language currently has concepts like this.

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