Examples
Browse every example in the repository, with its source code, command and recorded output where available.
The test corpus is the collection of programs used to check the compiler. On every commit, the tests build and run them in three configurations; examples that need an unavailable C library are skipped. Where a page shows recorded output below the command, the tests compare the program’s output with that text byte for byte.
Start with the first group, which introduces one idea at a time in reading order. These files are checked for compilation errors and formatting, but their output is not recorded or tested. The remaining groups make up the test corpus, with programs such as a JSON reader, a small language interpreter, a SQLite ledger and calculations using raylib.
Start here · Reading and processing text · Numbers and algorithms · Data structures · Files and the command line · C libraries · Threads · Games and simulations
- 68programs
- 120files
- 18,836lines
Start here
-
00-firstA minimal program that prints a number, and the first one this compiler ran.
Read the code -
01-pointsDefine records, create values and call functions with method syntax.
Read the code -
02-tokensDefine a value with several possible forms and handle every case.
Read the code -
03-fallibleReturn failures as values and handle them explicitly.
Read the code -
04-loopsRepeat work with two kinds of loop and mark each change to a value.
Read the code -
05-mutationChange a copy without changing the original value.
Read the code -
06-genericsWrite one function for several types and let the compiler infer the types at each call.
Read the code -
07-stringsRead text as bytes or as whole characters, depending on the task.
Read the code -
08-ffiUse C libraries for features beyond the language basics.
Read the code -
09-holesMark unfinished code with ??? and see which type the compiler expects.
Read the code -
10-mapsLook up a map key and handle a missing entry explicitly.
Read the code -
11-treesBuild a tree whose nodes store their children as independent values.
Read the code -
12-interpolationPut a value inside a string, and let the compiler say when it has no text.
Read the code -
13-leaseHand a C library text it will keep, and say when you are done with it.
Read the code
Reading and processing text
-
calculatorA calculator that reads and evaluates arithmetic expressions with variables.
Read the code -
interpreterRead and run scripts written in a small language.
Read the code -
jsonRead a JSON document, describe its structure and print it in a consistent format.
Read the code -
markdownConvert Markdown to plain text, with the work split across modules.
Read the code -
csvRead a CSV table, including quoted fields, commas inside fields and doubled quotation marks.
Read the code -
iniRead a configuration file into sections and report invalid entries.
Read the code -
templateFill placeholders in a text template with values from a map.
Read the code -
rpnEvaluate reverse Polish expressions, where each operator follows its numbers.
Read the code -
assemblerAn assembler that translates instructions for a small simulated machine, then runs them.
Read the code -
romanConvert numbers to Roman numerals and back.
Read the code -
wordsCount how often each word appears in a passage.
Read the code -
wrapWrap text to a chosen line width and test the boundary cases.
Read the code
Numbers and algorithms
-
sieveFind prime numbers by crossing out multiples, then check the counts against published results.
Read the code -
nbodySimulate five bodies moving under gravity and check their energy against published results.
Read the code -
spectralCalculate the largest singular value of a matrix without storing the matrix.
Read the code -
fannkuchTry every ordering of seven numbered cards, flipping them until card 1 is on top.
Read the code -
binarytreesBuild binary trees, visit their nodes and release the memory they use.
Read the code -
checksumCompute CRC-32 and Adler-32 checksums, and encode the same bytes as Base64.
Read the code -
diffFind changes between two versions by comparing what they have in common.
Read the code -
datesCount the days between two dates, including leap years.
Read the code -
floatsPrint and compare infinity and NaN, the special floating-point value meaning not a number.
Read the code -
widthsConvert numbers between types and check what happens when they no longer fit.
Read the code -
palettePack a colour into one integer, extract its parts and combine them again.
Read the code
Data structures
-
queryCreate a table, save and read it back, then filter, join, sort and group its rows.
Read the code -
spreadsheetRead a spreadsheet file and evaluate its formulas.
Read the code -
treeBuild an expression tree, then evaluate it, measure its depth and print it.
Read the code -
routesFind the shortest route between two stations.
Read the code -
deckDeal, cut, reverse and riffle a deck of values.
Read the code -
readingsSummarise a week of sensor readings with map, filter, fold, find, any and all.
Read the code -
shapesImport the same module from files at different directory depths.
Read the code
Files and the command line
-
todoKeep a to-do list in a file.
Read the code -
logsTurn a log file into a readable summary.
Read the code -
argvTwo ways to read the arguments passed to a program.
Read the code -
tallyCount lines, words and bytes received through standard input.
Read the code -
pipelineRead lines and produce a report using two modules with the same name.
Read the code
C libraries
-
sqliteOpen, query and close a SQLite database directly from Heroes.
Read the code -
ledgerKeep a ledger in SQLite and check each total with a separate calculation.
Read the code -
curlCall libcurl from Heroes, including a function that accepts a variable number of arguments.
Read the code -
raylibUse raylib to calculate with colours, points and rectangles passed as C structs.
Read the code -
sdlCall SDL3 from Heroes and check which drawing drivers it provides.
Read the code -
ctimeRead the parts of a calendar date from a C struct.
Read the code
Threads
-
threadsStart threads and pass each one an integer to work with.
Read the code -
mandelbrotCount the calculation steps needed for points in the Mandelbrot set, using several threads.
Read the code -
matmulMultiply two matrices, assigning a band of rows to each thread.
Read the code -
dotproductCompute the dot product of two long vectors, using several threads.
Read the code -
montecarloEstimate pi by simulating dart throws, with a separate random seed for each thread.
Read the code -
nqueensCount the ways to place eight queens on a chessboard so none can attack another.
Read the code -
collatzFind the longest Collatz sequence below a chosen limit, using several threads.
Read the code -
firsthitSearch a long range with several threads to find the first position that meets a condition.
Read the code -
histogramCount how often each digit appears, with one thread for each digit.
Read the code -
wordbandsCount words in text generated separately by each thread.
Read the code
Games and simulations
-
adventureA text adventure that follows a script of player commands.
Read the code -
mazeFind and draw the shortest path through a maze.
Read the code -
boardA grid of cells that grow and die, with a scoreboard organised by two keys.
Read the code
Next: the language, one idea at a time, or the complete specification in one file to hand to a model. How to run an example.