Skip to content
← All work

03 · C interpreter

Loop Quest

Type a C loop and watch it run one step at a time. Every trip through the loop becomes a tile, and a tiny pixel hero jumps along them.

Loop Quest: C code on the left with the current line highlighted, and on the right a pixel hero jumping across numbered tiles.
Each tile is one trip through the loop; the number is the loop variable at that moment.

Why I built it

Loops are hard to picture

Loop Quest came from seeing people around me, myself included, struggle with C loops. On paper a loop is three short lines, but what happens on each round, and when exactly it stops, is hard to picture. So I made it visible.

At a glance

Runs
Entirely in the browser: one HTML file, no server, no libraries.
Understands
int, for, while, do-while, if/else, break, continue, and printf with %d, %i and %c.
Shows
The current line, a plain-English explanation, every variable, the output, and the loop itself as tiles.
Sounds
Chiptune effects generated live with the Web Audio API. No audio files.
Checked by
16 test programs that must print exactly what GCC prints.

How it works

A tiny C interpreter

There's no real C compiler in the browser, so Loop Quest carries its own small C interpreter, written in JavaScript. Your code goes through four stages:

  1. Your code
  2. Tokenizer
  3. Parser
  4. Step runner
  5. Screen
01

Tokenizer

Reads the code character by character and cuts it into tokens: numbers, names, operators like <= and ++, and text in quotes. Comments and #include lines are skipped. Every token remembers its line number, so an error can point at the right line.

02

Parser

Turns the tokens into a tree, using recursive descent: each level of operator precedence (||, then &&, then ==, then <, then +, then *) is its own small function, so 1 + 2 * 3 groups the way C does. Mistakes come out in plain words, like: Expected ";" but found "}".

03

Step runner

A JavaScript generator function (function*) walks the tree. After every meaningful action (a declaration, a condition check, an update, a printf) it pauses with yield and hands the screen one event: which line, what kind of step, and a sentence explaining it. The Step button asks for the next event; Play does the same on a timer.

Check i <= 53 <= 5TRUE. Round 3 begins!

04

Screen

Each loop gets its own lane: the first runs along the ground, the second and third on raised platforms above it. Every check adds a tile showing the loop variable at that moment; the final, failed check adds a red X tile. The hero jumps from tile to tile and printf text floats up from him. It's drawn on a 260×130 canvas and scaled up 3× for crisp pixels.

Behaving like C, not JavaScript

  • Every value is forced to a 32-bit integer (v | 0), so numbers wrap around the way C's int does.
  • Multiplication uses Math.imul, so even a huge y * y overflows like C instead of turning into a big float.
  • Division cuts toward zero (-7 / 2 is -3), and % keeps the sign of the left side.
  • Every { } block gets its own scope, so an inner int i hides the outer one until the block ends.
  • After 2,000 steps it stops and explains that the condition probably never becomes false.

Checked, not guessed

Tested against GCC

A visualizer that teaches the wrong answer is worse than none. So the interpreter is tested against GCC, a real C compiler.

  1. 1The script scripts/loop-quest-vs-gcc.mjs pulls the interpreter straight out of loop-quest.html, so it tests the exact code visitors run.
  2. 2Each test program is wrapped in #include <stdio.h> and int main(void) { … }, compiled with GCC and run.
  3. 3The same program runs through Loop Quest. The two outputs must match character for character.

16 programs: the 8 examples in the game, plus 8 cases where JavaScript and C easily disagree:

  • Negative division and %
  • int overflow
  • printf width and %c
  • Inner scope hiding an outer variable
  • i++ vs ++i
  • && short-circuit
  • break inside nested loops
  • Compound assignment
TERMINAL
$ node scripts/loop-quest-vs-gcc.mjs
PASS  Count to 5
PASS  Add 1 to 5
PASS  Countdown (while)
      … 10 more, all PASS
PASS  && short-circuit
PASS  Nested break only exits inner loop
PASS  while with compound ops

16/16 programs print exactly the same as GCC.

One honest footnote: in C, overflowing an int is technically undefined. GCC on normal computers wraps around, and Loop Quest matches that.

Honest about it

What it can't do (yet)

  • Use arrays, pointers or strings. Only whole-number variables exist; there's no [ ], no *p, no text variables.
  • Call functions. printf is the only one. No scanf, and no functions you write yourself.
  • Handle decimals. No float or double, and printf knows only %d, %i and %c.
  • Tell long from int. long and char are accepted but stored as a 32-bit int, so a long past about 2.1 billion wraps where real C wouldn't.
  • Use switch, ?: or bit operators. They give a friendly error instead of running.
  • Draw every loop. Only the first three loops in a program get a lane on screen. A fourth still runs, just without tiles.
  • Run very long programs. The 2,000-step safety stop also cuts off long programs that would have finished.