Interpreting: Making the Tree Run
The payoff stage: take the tree the parser built and *run* it. Walk the tree, do what each node says, and your program comes alive. It's simpler than you'd think โ and deeply satisfying.
You've got a tree that captures your program's structure. Now for the payoff: running it. A tree-walking interpreter does exactly what it sounds like - it walks the tree, and for each node, does what that node says: an addition node adds its children, an if-node checks its condition and runs a branch, a print node prints. It's the most approachable way to make a language actually execute, and watching your own tree come alive is one of programming's great satisfactions.
Walk the tree, do each node
Interpreting is a recursive walk: to evaluate a node, you often first evaluate its children, then combine them according to the node's type.
# to run an addition node:
# 1. evaluate its left child (maybe a whole subtree)
# 2. evaluate its right child
# 3. add the two results
#
# to run an 'if' node:
# 1. evaluate the condition child
# 2. walk the 'then' branch, OR the 'else' branch
# each node type has a simple rule for what to do.Real interpreters, readable
This is exactly how the stack's languages run - and how larzscript's money features work.
| Node | What the interpreter does |
|---|---|
| Number / string | Return the value |
| a + b | Evaluate a and b, then add |
| if / while | Evaluate the condition, walk the branch |
| call fn(args) | Evaluate args, run the function body |
| pay X from A to B (larzscript) | Move money, record the ledger |
The last row is the payoff of building your own language: <a href='/stack/larzscript'>larzscript</a>'s interpreter handles a <code>pay</code> node by actually moving money between wallets and recording a transaction - a money primitive, because the interpreter gives it meaning. Whatever your language should do, you define it here, node by node. <a href='/stack/larztrace'>larztrace</a> visualises this walk for Python, so you can literally watch execution flow.
Bring the tree to life
- Interpreting walks the tree and does what each node says - the program runs.
- Each node type has a simple rule; evaluating a node evaluates its children first (recursion).
- The whole interpreter is small per-node rules plus a recursive walk - less code than you'd think.
- You define the meaning - larzscript's interpreter makes
payactually move money.
๐งฉ Do it with LarzOS
Turn this lesson into action with the exact free tools and AI workflows for the job:
๐ฏ Key takeaways
- A tree-walking interpreter runs a program by walking the tree and doing what each node says.
- Each node type has a simple rule; evaluating a node first evaluates its children, so recursion handles any nesting.
- The whole interpreter is a set of small per-node rules plus a recursive walk - surprisingly little code.
- You define what constructs mean - larzscript's interpreter makes 'pay' actually move money and record a ledger.
Frequently asked
Once I have an interpreter that walks the tree, is that a 'real' programming language?
Yes - a working tree-walking interpreter that lexes, parses, and executes programs is a real, legitimate programming language implementation, even if it's small and simple. There's sometimes a mistaken sense that 'real' languages must be compiled, or industrial-scale, or have huge ecosystems, but none of that is what makes something a programming language. A programming language is a system for expressing computations that can be executed, and if you have source code that gets tokenized, parsed into a structure, and then run by an interpreter that gives the constructs meaning, you have exactly that - a real language. Tree-walking interpreters are a completely valid and common way to implement languages; many real, widely-used languages are (or started as) tree-walking interpreters, and some remain interpreted. The approach being simple doesn't make the language fake - it makes it approachable. Your language being small (few features) or slow (interpreters are generally slower than compiled code) doesn't disqualify it either; those are matters of scope and performance, not of whether it's a real language. The stack's larzscript is a genuine example: it's a tree-walking interpreter, it's small, and it's absolutely a real programming language - it has its own syntax, its own semantics (including novel money-native ones), and it runs programs. So when you build a working interpreter for a language you've designed, you have genuinely created a programming language, and you should feel entirely justified calling it that. Now, there are things that distinguish small learning languages from big production ones: performance (production languages often compile, or use sophisticated interpreters with optimisations, to run fast), breadth of features, standard libraries, tooling (debuggers, package managers), error handling quality, and robustness across edge cases. Building those out is a lot of additional work, and it's what separates a toy or educational language from an industrial one. But the core - a language that runs programs - you have with a working interpreter. The path from there is enhancement (more features, better performance via compilation, tooling, libraries), not a fundamental transformation into 'a real language' - it already is one. This is empowering to realise: the barrier to creating a real programming language is far lower than it seems, and a tree-walking interpreter you can build yourself crosses it. You won't have built Python or Rust, but you'll have built a real, if modest, language - which is a genuine and impressive accomplishment, and the foundation you'd extend to make it more capable. The interpreter is the heart; once it walks the tree and runs programs, the language lives. Everything beyond is making it bigger, faster, and more polished - real improvements, but improvements to something that is already a real language.
How does the interpreter handle things like variables and functions - not just calculating expressions?
The interpreter handles variables, functions, and other stateful or complex constructs by maintaining some runtime state alongside walking the tree - most importantly an 'environment' that maps names to values, plus mechanisms for function calls - so that beyond just calculating expressions, it can remember things and execute richer behaviour. For variables: the interpreter keeps an environment, essentially a lookup table from variable names to their current values. When it encounters a 'let x = 5' node, it stores x -> 5 in the environment. When it later encounters a use of 'x' in an expression, instead of that being a fixed value, the interpreter looks x up in the environment and uses its current value. Assignment ('x = 6') updates the stored value. So variables work because the interpreter carries this name-to-value state as it walks the tree, reading and updating it as nodes require. For scope (variables local to a function or block), the interpreter uses nested environments: each function call or block gets its own environment that can see the enclosing ones, so local variables don't leak out and the right variable is found by looking outward through the nesting. For functions: when the interpreter encounters a function definition node, it stores the function (its parameters and body, plus a reference to the environment where it was defined - which enables closures) as a value under its name, just like any other value. When it encounters a function call node, it evaluates the arguments, creates a new environment for the call with the parameters bound to the argument values, and then walks the function's body in that new environment - and when the body hits a 'return', the interpreter stops executing the body and produces the return value (often implemented by raising a special internal signal that the call-handling code catches). So a function call is 'set up a new environment with the arguments, run the body in it, capture the return value'. Recursion works naturally because each call gets its own fresh environment, so a function calling itself just creates nested call environments. For control flow like if and while: the interpreter evaluates the condition, then chooses which branch to walk (for if) or repeatedly walks the body while the condition holds (for while) - it's controlling its own tree-walking based on runtime values. The general principle is that the interpreter isn't just a pure calculator; it walks the tree while maintaining runtime state (environments for variables, a notion of the current scope, function values, and mechanisms for returning and looping), and different node types interact with that state - defining variables writes to the environment, using them reads from it, function calls create new scoped environments and run bodies, control-flow nodes direct which parts of the tree get walked. This is more involved than evaluating '2 + 3', but it's still built from the same tree-walking foundation, just with the added machinery of environments and call handling. You can see all of this concretely in larzscript's interpreter: it has an Environment with get/define/assign and parent-chaining for scope, it stores functions as values that close over their defining environment, it handles calls by creating a new environment and running the body while catching a return signal, and it handles if/while by evaluating conditions and walking the appropriate branches - plus its money-native additions (wallets, pay, subscriptions) which are extra state and node rules layered on. So the interpreter handles the full richness of a language by combining the recursive tree-walk with runtime state (environments) and the right rules for each construct - variables are environment lookups, functions are stored values run in new scoped environments, control flow directs the walk. It grows beyond a calculator by carrying and manipulating state as it goes, but it never loses the core simplicity of 'walk the tree, do what each node says' - the state-handling is just what some nodes do. Reading larzscript's interpreter is the best way to see exactly how these pieces fit together in real, readable code.
Related lessons
Parsing: Words into Structure
The heart of a language: take the flat list of tokens and build a *tree* that captures theโฆ
How Programming Languages WorkBuild Your Own Language
You've seen every stage. Now put them together: a tiny working language is a genuinely achโฆ
How Computers WorkFrom Code to Running Program
You write Python; the CPU only understands tiny binary instructions. Something has to bridโฆ