Jumping Back and Forth

Jumping Back and Forth

The simplest control flow construct is an if statement with no else clause:

The VM evaluates the bytecode for the condition expression. When the condition is truthy, we pop it right before the code inside the then branch:

Otherwise, we pop it at the beginning of the else branch:

This little instruction here also means that every if statement has an implicit else branch even if the user didn’t write an else clause. All that remains it a little disassembler support:

These two instructions have a new format with a 16-bit operand, so we add a new utility function to disassemble them:

There we go, that’s one complete control flow construct.

Source: craftinginterpreters.com