Local Variables · Crafting Interpreters

Local Variables · Crafting Interpreters

In this chapter, we’ll extend that to support blocks, block scope, and local variables. Since the compiler knows exactly which local variables are in scope at any point in time, it can effectively simulate the stack during compilation and note in the stack each variable lives. But that would mean a lot of boring changes to the code we already wrote, so here’s a global variable:

Here’s a little function to initialize the compiler:

When we first start up the VM, we call it to get everything into a clean state:

Our compiler has the data it needs, but not the operations on that data.

Source: www.craftinginterpreters.com