Building Your Own Programming Language Transform Practice
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.
[00:00:24] 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?
[00:01:16] 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.
[00:01:35] 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.
[00:01:54] 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.
[00:02:45] And we'll say, if,
>> Steve Kinney: Node.name.
>> 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.
[00:03:16] 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.
[00:03:40] You can have one that cares about multiple nodes. It does a whole series of things, absolutely, and then we'll expect. That ast.name,
>> 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?
[00:04:11] 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?
[00:04:39] 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.