Transcript from the "Looking Up Identifiers" Lesson
>> Steve Kinney: I get that math pi in there, it'd be cool if I could grab that out, I would also make this next test pass. So we have the ability to call functions, but what about identifiers? We just get an identifier, we can't do anything with it, it doesn't have a value, right but it can be associated with a value, right?
[00:00:20] And eventually we can define new identifiers, right? So cool pi is in there right now, but we can say like, define x is like 42,000, right? And we can add that to the environment, like be able to add more stuff to there and be able to look it up later, so we have something in there, let's be able to look at, let's learn how to look it up.
[00:00:39] And then later on, we'll be able to add some stuff in there as well. But if there's any functions that you wanna put, [LAUGH] we're gonna put the the ability at the max number of an array just as an example. But you can add as many things that object as you want and have it available it for you in the language.
[00:00:58] So all right, we kind of danced around it a little bit, it's pretty straightforward how we're going to get something out, we're in the evaluate function, we are looking at whatever node we're on. Hey, if you are a call expression go ahead, look up, define us a call expression, make sure it's a function and call it, what do you think looking for an identifier is like?
>> Steve Kinney: It's kinda like a simple subset of that, right? We can see that the node is an identifier, neat go ahead, look it up in that table and get me back the result, it's almost like less of actually calling a function, right? So let's kind of talk through it together.
[00:01:51] So we definitely know that we're gonna a check here, what kind of node type am I gonna look for?
>> Speaker 2: Identifier.
>> Steve Kinney: Identifier, yep, so we'll say if the node.type is == 'identifier' and again, I might use a helper for this, go ahead and return. Well actually, I could write it in this function, but I don't want to cuz I care about myself.
[00:02:16] So what we'll say is, we'll say let's get identifier, which we have not written yet.
>> Steve Kinney: With the node, right? All right, so what does getIdentifier need to do? It's not dissimilar from what's happening here, it's actually just less, right, what needs to happen?
>> Speaker 3: Just gonna take the name from the node and get it from the environment.
>> Steve Kinney: Yeah, yeah.
>> Steve Kinney: So let's get identifier given the node,
[00:03:11] So if the environment node name is in there, return
>> Steve Kinney: Environment [node.name]. So before we made the identifier you're why does it have a name? Cuz it's actually more similar to our expressions, right than it is to a actual literal. But if it's not in there, what should we do?
>> Speaker 4: Throw an error.
>> Steve Kinney: Throw an error, all right, one of the fun things about doing all this is we get the fun throw fun new errors that we don't normally get to throw. Let's throw a reference error yeah [LAUGH] we'll throw the reference error and we'll say no.name is not defined.
[00:04:23] Sometimes when you look at words too much you start to think they're spelled wrong, has anyone had that yet? Because we have a test suite, so test suite can tell me if I spelled things wrong, I should unskip the test if I wanna find out anything, though. That's the best part about skip test, is they don't fail, the worst part about skip test is that they don't fail.
[00:04:44] So we've got the pi in their, and so I should say that hey, look up pi, cuz all we're doing is evaluating and our evaluation function is handling the rest. This one, it's gonna be your mission to do in a second, so I'll leave it there as a teaser, but let's make sure the test suite passes, all right.
[00:05:06] Let's call it mind passes, all right, here's your mission, it's trying to call the function max. Now I know you all were studying the comprehensive standard library, but there's no maximum there. So, you need to add a max function in there so that somebody can call Max and if you wanna be a hero, you can also have min too.
[00:05:33] Honestly, you wanna change this test and make it something else, that's cool too, but here's an example, we wanna be able to add something to the standard library kind of like expand and grow our language. So your mission is to go into a standard library, add a new method on that environment object, right, that will effectively be able to be looked up, take all the arguments and do all the things.
[00:05:55] Max is an interesting one cuz it already has the ability to take a lot of arguments is actually a little bit easier, but I'll leave that as an exercise to you and then we'll come back in a few minutes and we'll do it together.