Visualizations
488 / 500

488 · Algorithms

Tiny Compiler

A whole compiler, from source to running bytecode, rebuilt on every keystroke.

Five panels are five stages of a real compiler for a small language of assignments and arithmetic. The lexer's tokens slide out as chips, a precedence climbing parser builds a tree per statement, and the optimizer folds constants, propagates them through variables, applies identities like x*1 and x*0 and deletes dead assignments, animating each pruned node into the node that absorbed it. The folded program is lowered to three-address code, liveness analysis builds an interference graph, and Chaitin's simplify and select steps color it with three registers, spilling to memory when it needs more. The same program also compiles to stack bytecode that runs one instruction at a time on a live operand stack.

Try it. Click the source and type (arrow keys move, Enter adds a line); every keystroke recompiles all five stages, and a syntax error dims the later ones. Drag the x or y input in the VM panel to rerun with new values, hover a register node to see what it interferes with, or pick a program below.

  • Precedence climbing
  • Constant folding
  • Liveness analysis
  • Graph coloring register allocation
  • Stack machine

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a tiny live compiler visualizer with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Use a textarea for source code in a tiny language: lines like a = x * 2 + 1 and a final return expression, with x and y as inputs. Draw everything else on a full-window canvas that stays sharp on high-DPI screens.
- Write a lexer (numbers, names, operators, parentheses, newlines) and draw its tokens as colored chips in a panel.
- Write a precedence climbing parser that builds a syntax tree for each statement, and draw the trees with a simple layout: leaves in consecutive slots, each parent centered over its children.
- Compile the trees to stack bytecode (PUSH, LOAD, STORE, ADD, MUL, RET) and step through it twice a second, drawing the operand stack as boxes that push and pop.
- Recompile on every input event so all panels update as you type, and show parse errors without wiping the last good result.

Once that works, make it beautiful:
- Add constant folding: evaluate subtrees whose children are numbers, propagate variables that hold constants, and simplify x * 1 and x + 0. Animate the tree from its original shape into the folded one.
- Connect the panels with curved pipes and dots flowing from one stage to the next, brightening whenever you type.
- Give each token type, tree node and bytecode operand a consistent color so you can follow a value through the whole pipeline.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as three-address code with register allocation by graph coloring, dead code elimination, or a ghost typist that writes a demo program by itself.
PreviousMondrian ForestDecision trees cut the plane into a De Stijl painting; a forest blurs it into glazes. NextSundial GardenA pastel garden lit by the real sun, with shadow-mapped shadows and a working sundial.

Related visualizations

  • Breadboard CPURetro A microcoded 8-bit breadboard computer paints cellular automata on a 16x16 LED matrix.
  • CPU PipelineRetro A five-stage RISC pipeline as an isometric factory: bubbles, bypasses and flushes.
  • Symbolic RegressionMachine learning Genetic programming breeds formulas until it rediscovers the law behind the data.
  • Garbage CollectorAlgorithms A heap as a warehouse: crates turn white, grey and black, then the dead get swept.
  • Core ArenaAlgorithms Assembly warriors battle for 8,000 cells of shared memory on a real Redcode VM.
  • Turing MachineRetro A brass Turing machine runs busy beavers, including all 47 million steps of BB(5).
  • Regex AutomatonAlgorithms Type a regex and watch it compile into an NFA, a DFA and a minimal DFA.
  • PCB AnnealerAlgorithms Simulated annealing places chips on a circuit board, then a maze router lays copper.
  • Marble ComputerPhysics A wooden pegboard computer where falling marbles count, add and multiply in binary.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs