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

The "Variable Evaluation" 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:

Now that variable declarations are defined, Steve explains that the next step is to be able to evaluate them, and test both variable declaration and evaluation in the REPL.

Preview
Close

Transcript from the "Variable Evaluation" Lesson

[00:00:00]
>> Steve Kinney: We added define. We do need to go and make sure that we can evaluate it however. So let's get that in place.
>> Steve Kinney: Cool, we'll go into evaluate.
>> Steve Kinney: Now we have this new type of node. So let's go ahead and make the helper function first. We'll say,

[00:00:28]
>> Steve Kinney: I think define is a great name for it. But if you named yours something else, then define might not be the best name for you. It's just the best name for me. define, I will say node and all we have to do is add it to the environment.

[00:00:46]
>> Steve Kinney: And it'll be node.identifier,
>> Steve Kinney: .name = node.assignment.value.
>> Steve Kinney: And then we'll say
>> Steve Kinney: If the node.type now equals this new kinda syntax and yeah, could you come up with, is this gonna grow over time? Sure, right, could you come up with some other abstractions? Absolutely.
>> Steve Kinney: Declaration return.

[00:01:30]
>> Steve Kinney: define(node). All right, I think I'm gonna take it for a spin in my REPL cuz I'm worth it.
>> Steve Kinney: Let's see if made any mistakes.
>> Steve Kinney: (define x 10), whoop, cannot read property type of undefined. All right, I'm gonna take a second and take a look at that, and I'll get right back to you.

[00:01:58]
So I'm gonna take out that console.log unless it breaks again.
>> Steve Kinney: Cool, same way about definitions in JavaScript work like that. You could have it. If you want to have it return the value as well, you could just simply have it returned and do that. I'm gonna go with it not, and then it'll say add x and 10 is 12.

[00:02:27]
Right, I now have variable declarations. That was adding a, guess, probably an important language feature. Not that bad, right? And again, we chose a simple language with a very kind of easy syntax for that. But I still think it is super interesting to be able to have that in place.

[00:02:48]
All right sweet.
>> Speaker 2: So what was the fix?
>> Steve Kinney: What's up?
>> Speaker 2: Where did you?
>> Steve Kinney: The fix was when I wrote that transformation, right, originally this was a function that took the node and then returned it back out. Here, this is supposed to pass everything along through the pipeline.

[00:03:08]
And because I didn't return node, it returned undefined to evaluate. Evaluate tried to find type on undefined. Everything went poorly.

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