This video demonstrates how to implement an evaluator for an Abstract Syntax Tree (AST) that recursively processes different node types (integers, booleans, expressions) and returns evaluated objects, with optimizations like pre-creating immutable values (true, false, null) to improve performance.
AST Evaluation: Integers, Booleans, and Null in Our Evaluator
Added:in the last video I showed a sneak peek of what the pseudo code for our evaluation function could possibly look like let's go ahead and formalize the method definition in an evaluator Dogo package inside of our repository it is going to accept any of the many node children we have defined in our as class thus far these could be integers booleans Expressions what have you the key is that no matter what node goes in we are going to get an object out that has been evaluated another important note is that this method will be recursive if we pass in our program node it will recurse down and begin to parse each of the statements which will recurse even farther down to our assignments and expressions let's start by evaluating integers these and booleans are the simplest items to evaluate when we type five in our interpreter we expect to see five jump right back out at us all we want to do is take the value of our integer literal and to create an integer object with it this may seem like a hat on a hat but for our more comp evaluations this conversion is necessary so add a switch statement to the evaluate function we are going to Branch depending on the type of node that we pass in in the first case we are going to tackle the integer literal type inside of the case Body just return an integer object using the node value is the body this looks great but it won't work because we are only passing in the top level program node of our pars tree to fix this let's add a couple more cases to our switch statement that will work down the branches to actually get to an integer literal let's start with program note inside of the case Body we're going to call a helper method called evaluate statements we will write this helper method in just a moment next add the case for an expression statement in the case Body we are just going to call the evaluate function on the expression field with this in place the next call will be able to parse our integer literal expression now start writing the evaluate statements method here we will accept a list of statements and we return an object note first Define the result object that we will end up returning next Loop over each statement and set the result variable to the output of the evaluate function called on that statement our result will end up just being the last statement that was called and at this point it is actually time to see the fruits of our labor unless you count writing test but those are joyless fruits let's go back to the console package that we wrote so long ago and make it to where we parse and evaluate what we type in first thing to do is instantiate our parser right after we create our lecture while we're here you can go ahead and remove this for Loop that prints out the results of our lecture next call the parse program method on our parser next check for errors and print them if necessary next instantiate our evaluator and evaluate the program if the object return Isn't null we want to print it out by calling the inspect method of The Returned object and if we run this in our console we can go ahead and play with it with that settled let's go back and add support for booleans in our valuator and surprised this is just as easy as adding the integers as booleans will end up evaluating to themselves give it a test drive in the console to make sure that we didn't screw anything up and here we can actually make a Teensy optimization the Boolean values for true and false are never going to change and it actually doesn't make sense to instantiate them each time and return a brand new object so we can actually precreate the Boolean objects for true and false go ahead and add them to our eval package above our eval function now it is up to just returning the correct object depending on the Node value in order to keep the switch clean you can extract out the if statement to a method in this case I've called it native Bo to Boolean object and to wrap up let's add support for null like booleans a null object can be created once and then referenced any time we work with a null value in our evaluator go ahead and pre-create the null object alongside the booleans and with that we're going to go ahead and wrap this one up in the next video we're going to continue to expand our evaluator and start taking a look at prefix Expressions see you there
Up Next

Understanding JavaScript Closures: A Simple Breakdown
@freecodecamp
14.6K views•2025-12-09

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

HTTP Requests Explained: GET, POST, PUT, DELETE
@codecademy
103.1K views•2021-10-07

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science









































![Essentials of Interpretation. Lecture [3/18] Compilers: AOT, JIT, Transpiler](https://i.ytimg.com/vi_webp/r1S9N4if__A/maxresdefault.webp)


