Essay, 2026

How a computer reads code.

A programming language is a sequence of characters in a file. A running program is a sequence of decisions in memory. The bridge between them is a small, surprisingly readable piece of software. This essay builds it from scratch. By the end you will have read every line of a working interpreter for a tiny language called Tiny, and you will be able to run programs in it from inside your browser.

Reading time about twenty five minutes. Five interactive figures. Keyboard, screen reader, and reduced motion supported.

Begin reading

Part one

Words: the lexer.

The first job of any language implementation is to take a stream of characters and chop it into tokens. A token is the smallest unit of meaning the language recognises: a number, an identifier, an operator, a parenthesis. The component that does this is called the lexer or tokeniser, and you can write a reasonable one in around fifty lines of plain code. It walks the input one character at a time, branches on what it sees, accumulates into a buffer, and emits a typed token whenever it reaches a boundary.

The figure below contains a live lexer for Tiny. As you type, the token stream below your code refreshes. Try replacing the expression with something with errors, or with whitespace, or with a long identifier, and watch the lexer make sense of it. The boring shape of the output is the point. A lexer should be predictable and fast; its output should look like a list of small flat things.

Figure 1. The lexer.

Plain textarea. Use Shift Tab to leave the field.

Token stream

One detail worth pointing out. A lexer makes very local decisions. It does not know that + needs a left operand, or that let needs to be followed by an identifier. Local ignorance is a feature. The lexer is fast because it is dumb, and the parser is correct because the lexer is dumb in a documented way.

Part two

Structure: the parser.

The parser turns the token stream into a tree. The tree is the syntactic structure of the program: which operands belong to which operators, which expression nests inside which other. We will use a recursive descent parser, the cleanest hand-written technique for small languages. Each rule of the grammar becomes a function. Each function consumes some tokens and returns a node.

Tiny's grammar fits on one card. A program is a sequence of statements. A statement is either a let binding, a fn definition, an if expression, or a bare expression. An expression is a chain of additive terms; a term is a chain of multiplicative factors; a factor is either a literal, a variable, a parenthesised expression, or a function call. Precedence is encoded in the order the parser functions call each other. The figure below shows the resulting tree for any Tiny program you type.

Figure 2. The parser and the AST.

Abstract syntax tree

Show AST as a nested outline

    Two things to read off the tree. First, operator precedence is structural. The expression 3 + 4 * 2 parses as a single plus node with two children, where the right child is itself a star node. Multiplication is deeper in the tree, which is what makes it bind tighter. Second, function bodies are themselves expression nodes. Tiny has no distinction between a statement and an expression at the syntactic level. The last expression in a block is its value.

    Part three

    Meaning: the evaluator.

    A tree is not a program. It is a description of one. The evaluator is the component that turns a tree into a value. We will use the simplest possible technique, the tree-walking interpreter. Each node type has a corresponding clause in a switch. A number node returns its number. A plus node evaluates its left child, then its right, then adds them. A let node binds a name in the environment. A function call node calls the function. There is no compilation, no bytecode, no register allocation, and no optimiser. There is one function called evaluate and it recurses.

    The figure below runs your Tiny program and shows the evaluation trace. The trace records the order in which subexpressions were evaluated, with their intermediate values. The result of the last expression is printed at the bottom. Notice how the trace echoes the shape of the AST in part two: it is a depth first traversal of the same tree, with each visit decorated by the value the node produced.

    Figure 3. The evaluator and the trace.

    Evaluation trace

      Result

      —

      Tree walking is slow. For a real production language we would compile the AST to bytecode and run that in a virtual machine, or further to native code through a just-in-time compiler. None of that is in this essay because none of it is necessary to understand what evaluation is. The tree walker is the conceptual core. The rest is engineering.

      Part four

      Names and scopes.

      Two features remain. The first is variable bindings, introduced by let. The second is functions, introduced by fn. Both are implemented by a single data structure called an environment. An environment is a dictionary from names to values. To evaluate a name, you look it up in the environment. To bind a name, you write to the environment. To call a function, you create a new environment that extends the function's defining environment, populate it with the arguments, and evaluate the function's body in that new environment. When the body returns, the inner environment is discarded.

      This is enough to give Tiny closures. A function captures the environment in which it was defined, not the one in which it is called. The figure below traces these scopes explicitly. Each row of the environment table is one frame, with the parent it extends. Closures are the visible reason this matters: a function defined inside another can reach the outer function's variables even after that outer function has returned.

      Figure 4. Environments and closures.

      Environment frames

      Result

      —

      The two line make_adder example is the canonical closure demonstration. It is in the essay because it is the smallest program that distinguishes a real language from a calculator. Each call to make_adder produces a new function with its own captured a, and the returned function can be called many times after make_adder itself has returned. The mechanism that allows this is in the environment table on the right of the figure.

      Part five

      A small REPL.

      The final figure puts everything together. A read evaluate print loop. You type a Tiny expression and press run; it lexes, parses, evaluates against a persistent environment, and prints the value. Variables persist across submissions, so you can build up a session by binding names and then using them. Several sample programs are preloaded; pressing any of them places the program in the input area, ready to run.

      Figure 5. The REPL.

      History

        Type a Tiny expression and press Run.

        This is the complete language. Numbers, identifiers, the four arithmetic operators, parentheses, comparison, if, let, fn, and function call. The full implementation is in app.js and runs to about three hundred lines. The point of the essay is not to convince you that three hundred lines is small. The point is to convince you that the contents of those three hundred lines are entirely transparent, that a programming language is a thing a person can write, and that nothing in it relies on knowledge you do not already have.

        Colophon

        Tiny is a one-pass tree-walking interpreter implemented in plain JavaScript. The lexer is a single function that scans the input character by character. The parser is recursive descent with operator precedence climbing. The evaluator switches on node type and recurses. Environments are JavaScript objects with a parent pointer.

        Sources: Crafting Interpreters, Robert Nystrom, free at craftinginterpreters.com, on the tree walking model. Compilers, Principles, Techniques, and Tools, Aho, Lam, Sethi, and Ullman, the dragon book, on parsing. Programming Language Pragmatics, Michael Scott, on scope and binding.

        Built as a portfolio piece. The full source is on GitHub.