flang the compiler proves your program cannot hang 0.9.0 GitHub Try in the browserTry it Русский

The catalogue of examples

What ready-made flang programs are there, and how do you run them? They are all in the docs/examples/ directory of the repository: 461 .flang files. To run them yourself, clone the repository and install flang (Installation).

The number of files comes from the command git -c core.quotepath=false ls-files docs/examples | grep -c '\.flang$' → 461.

How to run them

Tests are written inside functions with the word пример (example). The command flang test runs the tests of one file or of every file in a directory:

flang test docs/examples/dsl/shipping.flang   # one file
flang test docs/examples/leetcode/            # one set

The first command answers:

docs/examples/dsl/shipping.flang: примеров 8, прошло 8, не прошло 0

It reads "8 tests, 8 passed, 0 failed". For a directory the last line of the output counts the files and tests of the whole set. The time depends on the set: leetcode (82 files, 1010 tests) takes about half a minute, crypto about twenty minutes, because it computes AES and ECDSA test vectors. If a file fails the check, the command names it and the error code.

Sets by topic

Directory.flang filesAbout what
leetcode/82solutions to LeetCode problems, each in one file with no dependencies. The study of the set is 82 problems
rosetta/28Rosetta Code tasks, each with the Russian and with the English keywords. The study of the set is Rosetta Code
web/17HTTP: an orders service, the link shortener, a shop API of three services, a program for the browser
io/10input and output through effects: a binary file, bytes over the network, a child process, an HTTPS request, a temporary directory
crypto/9AES, ECDSA, RSA-PSS, X25519, X.509, a certificate revocation list, the first TLS message — on test vectors from the standards
https/7TLS: the handshake and records of RFC 8448, the system certificate store, a request to a real host
db/6talking to PostgreSQL (SCRAM included) and Redis, reading an SQLite file
dsl/14your own small language: discount rules, a calculator, an order state machine, shipping and access rules. The recipe is Your own language in 30 minutes
frameworks/6flang logic inside NestJS, React and Vue applications — see below
library-api/7the logic of a library REST service: lending, catalogue, fines. The only set made of several modules with its own directory layout; the layout rules are in Project layout
service-on-processes/4a service made of several processes that answers over the network
behavior/4business rules in two versions for the behavior table: access to a report, a discount on a total
driver/3device drivers made of always-terminating functions: a UART and MSI
proof-probes/3programs that test the proof kernel itself
measure/3termination by a declared measure (убывает, decreases): Euclid's algorithm, binary search, natural numbers
host-boundary/2flang computes a decision, a C program carries it out. Explained in The boundary between flang and the host
errors/2an error as a value: summing a column of numbers that arrived as strings; parsing numbers
wal/2a write-ahead log: reading, writing, recovery after a truncation
numbers/2Church numerals and the laws of rational numbers
genetic/2a genetic algorithm that evolves a given string
pythagoras/2what part of the Pythagorean theorem can be written in flang: Euclid's formula for triples
surfaces/2factorial with Chinese keywords and in Esperanto
package/2a package with a manifest. Explained in Modules and packages
allocator/1a memory allocator as a pure state machine
monad/1an order total written with the в монаде form
money/1money on the exact decimal type сотых
paths/1the shortest path in a network where some nodes are unreachable
service/1a call to a service

Files for the documentation pages

These sets are the examples shown on the pages of this site. Each page example is a separate file, so you can run it without copying it from the browser.

Directory.flang filesFor which page
reference/80Language reference
proofs/38the Proofs section
guide/33Which construct to use when, Command reference
tour/24the language tour: page examples and exercises with solutions
playground/24the example ladders of the playground: from simple to complex in every setup. The texts for the C and Python translators, the arguments and the formulas lie there too
diagnostics/18Diagnostics reference
usage/7the guides in the docs/guide/ directory
first-proof/5the five steps of Your first proof
cli/3Command reference
tutorial.flang, operations.flang, import-check.flang3single files: first steps, operations, importing a module by name

flang logic inside a JavaScript application

docs/examples/frameworks/ holds four applications. In each the logic is written in flang and generated into JavaScript with flang emit … --target js. The framework handles the network, the form state and rendering, and calls the functions of the generated module.

DirectoryFrameworkflang sourceWhat it computes
nestjs-orders/NestJScore/orders-api.flangan orders REST service: parsing the request, required fields, routing by method and path, the response code (201, 200, 400, 404, 405)
react-invoice/Reactcore/cart.flang, core/catalog.flanga cart invoice in kopecks: the sum, discount tiers, the total, the invoice lines
vue-roman/Vuecore/roman-numerals.flangRoman numerals both ways for numbers from 1 to 3999
react-ts-pure/React and TypeScriptcore/storefront.flanga storefront with all its logic in flang: the cart, discounts, writing a sum in roubles, the reducer

The generated module is in the printed/ directory of each example. It has no dependencies and runs in Node and in the browser. A flang list becomes an array, a record becomes an object. Function names are transliterations of the Russian ones: «В римские» → vRimskie, «Сумма корзины» → summaKorziny.

You can check a generated module without installing any packages:

cd docs/examples/frameworks/vue-roman && node --input-type=module -e \
  'import("./printed/roman_numerals.js").then(m => console.log(m.vRimskie(2024), m.izRimskih("MCMLXXXIV")))'
MMXXIV 1984

The application itself is built with the commands from its package.json: npm install, npm run build. That needs packages from the network. The files in printed/ are not edited by hand; a command generates them again, for example flang emit docs/examples/frameworks/vue-roman/core/roman-numerals.flang --target js --out docs/examples/frameworks/vue-roman/printed.

Next