Pattern 25Tree-Based Interpreter

Purpose

This pattern executes programs by constructing an AST from the source code and walking the tree.

This tree-based interpreter pattern builds a complete scope tree before executing a program. That means it can support both statically typed languages like Java and dynamically typed languages like Python. (It can resolve all symbols statically before execution.)

Discussion

The previous pattern directly executed source code, without processing it in any way. At the opposite extreme, a compiler translates source code to machine code, which we can then run natively on the processor. Per the compiler application pipeline from Chapter 1, Language Applications Cracked Open, a compiler builds an intermediate representation ...

Get Language Implementation Patterns now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.