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

The "Testing Traverse" 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 writes the traverse test and gives a brief overview of the traverse and transform exercise.


Transcript from the "Testing Traverse" Lesson

>> Steve Kinney: So now we're able to traverse the entire tree. It's not really a lot we can do with that just yet. [LAUGH] But it is, in fact, it is there, right? We get there, I have a console log, and if we really need to see iterate account or something along those lines.

But let's do a thing. Let's try something really simple, let's try to go ahead, and we're gonna change all of the ads to subtracts for instance, right? Why? I don't know, I wasn't feeling particularly original. So, what would this look like? Here you can see we've kind of gotten implementation of it, now that we've got the traversal feature in place, we've got our visitor, and it's saying, for every call expression, I care about when I enter a call expression.

We're also gonna do some of the numeric laterals too. But, I'm gonna say when I care about a call expression, I'm gonna go ahead, I'm going to enter that node on the entrance in, and if the node name is add, I'm going to change it to subtract, right?

And this will walk your tree, and effectively perform a code modification for you. Right, and this one, it's silly, but it's also easy to see, right? Sometimes when you see, what I showed you is changing the variable from far to lat, made sense when I explained it, but it wasn't immediately apparent what it was doing.

This one is dumb, but obvious, so we can do that. And then if I come across an American literal on the way up, and it really doesn't matter, this is rad really rely on anything else, on the way up out of those, I will double the value. Now what this will do is, other just completely wreak havoc, I can make a fabulous one that goes ahead and just removes half of all nodes, will be great.

So, on so forth, right? So this actually is play. So let's actually, what I'm hoping now, is if I give it this ASC of a call expression with ad, right? What I hope to see at the end, is that now it actually has the value of subtract. Right?

But we have modified the code, and yes, a simple way to start out with. Let's go ahead and we'll run it
>> Steve Kinney: Cool. So now, I have modified my code. If I sat there and ran this as part of the CLI or the Math would be weird, because I'd still be typing ad, but you would modify the code and do stuff along those lines.

Now that's mildly interesting for changing some of my functions, right? It is also, there's again, it's one of the things that these tools come with a lot of other uses, we know that this is how a lot of our transpire this work. I think some of you have played with code mods before.

Right, they can go in effectively like refactor large parts to your code base. You could go find all the F five classes and changing the ESL classes, right? Stuff that might be otherwise, squirrely to do and time consuming can be done. Fairly easily, right? Usually when some libraries come out, a new version of react, they will release a code of deprecated things when it's just a syntax change, right?

You can also do really powerful and interesting things to your own code, or other problems you might have to solve along the way. Let's also make sure the doubling numbers, in fact works too, so we'll skip that test as well. So here you've got 12 and six that should end up being 24 and 12, right?

And so you can see, this one doesn't do anything to the call expression, right? You can have the same, but it's using the same reversal. It doesn't, it is agnostic to whether or not you want to traverse up or down, or think along those lines.
>> Steve Kinney: So, let's give a shot with that one.

>> Steve Kinney: Cool. So, what I wanna do is, give you a chance to try this out, right? You can add one additional test. And we want to change, maybe multiply to divide, or we can change every string of the text, Thanos to Captain America, right? And then we'll write one together from scratch, in about five minutes.

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