umlol, a programming language whose source is a flowchart
esolangflowchartinterpreterparsingunicodejavascriptjsdocconformance-tests
umlol is an esoteric language in which the program is a flowchart drawn in plain text with
box-drawing characters (or ASCII). The source is parsed into a graph of shapes joined by arrows,
and the graph is what runs. Live playground: https://umlol.jhampton.io/ (repo ~/dev/umlol, vanilla
JS). Its design choices are a useful model for any "picture as program" language and for any
language meant to have more than one implementation.
โญโโโโโโโโโฎ
โ fib(n) โ
โฐโโโโฌโโโโโฏ
โผ
โฑโโโโโโโโโโฒ โญโโโโโโโโโโโฎ
n < 2 โโโyesโโโถโ return n โ
โฒโโโโโฌโโโโโฑ โฐโโโโโโโโโโโฏ
โ no
โผ
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ return fib(n โ 1) + fib(n โ 2) โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
Graph parsing, not grid walking
Two-dimensional esolangs such as Befunge and AsciiDots move an instruction pointer across the
character grid, so layout is behavior. umlol instead parses the drawing into nodes and edges
once: layout has no meaning, and two drawings with the same shapes, text and connections are the
same program. That makes programs readable as diagrams, lets the tool print the graph as JSON
(--graph) and trace execution over it (--trace), and makes errors precise
(error at ROW:COL: ..., pointing at the misdrawn cell).
Reading a drawing reliably
- Work in display cells, not characters. East Asian wide characters and most emoji take two cells, combining marks and variation selectors zero, so columns match what a monospace terminal shows. Tabs are an error (their width is ambiguous).
- Shapes by their corners: square corners
โโโโ(or+) are process boxes, rounded cornersโญโฎโฐโฏ(or.and') are terminals (start,end,return x, or a subroutine entryname(params)), and three-row hexagons are decisions. - Edges leave only through ports drawn on a border (
โฌ โด โ โค), follow line characters, and end at an arrowhead; the target is whatever shape the head points into. - Make ambiguous ASCII glyphs context-dependent. An ASCII arrowhead (
> < v ^) only counts when it points the way the edge is already moving, sovand<stay ordinary letters and operators in text. ASCII+is a crossing if the line continues straight, otherwise a corner or junction. - Edges may merge but never split: at a junction exactly one branch may reach an arrowhead.
- Labels: text written into an edge (
โโnoโโโถ) or loose text next to it; a decision's two exits must be labeled yes/no (several spellings, including check marks). Any other loose text is a comment. - Every box-drawing character must belong to some shape or edge; a stray one is an error, which catches broken lines that would otherwise silently disconnect.
Language inside the shapes
Process boxes hold statements (x โ e, print, read), decisions hold one boolean expression,
and rounded name(params) terminals start subroutines whose return terminals give back a value.
Integers are arbitrary precision (JS BigInt), lists are shared references with deep =, there are
no globals, and recursion is allowed. Each diagram is everything reachable from its entry; diagrams
may not share shapes.
The spec and the tests are the language
SPEC.mdplus a folder of conformance programs (tests/programs/*.umlolwith.out,.in,.errand.graph.json) define umlol, not the JavaScript implementation. The test runner drives any implementation through its command line (UMLOL_CMD="<your umlol>"), so a port to another language is checked against the same folder.- The CLI interface is part of the spec: exit codes, the exact first stderr line
(
error at ROW:COL: MESSAGE/runtime error at ...), and a canonical--graphJSON with nodes numbered in reading order. Defining the interface is what makes the tests portable. - Every language change edits the spec and adds or adjusts a conformance test. That keeps the first implementation's quirks from silently becoming the definition.
Interpreting without blowing the host stack
The first interpreter ran each call as a nested JavaScript generator (yield* per call level, so a
debugger could step it). V8 overflowed its own stack at about 900 nested calls. The fix was a
frame-stack machine: each node's text is compiled lazily to a small stack-code array, and
execution keeps an explicit stack of frames {node, code, pc, operand stack, environment, caller}.
Calls push a frame instead of recursing in JS, and/or short-circuit with jumps, and the depth
limit becomes a deliberate number (the spec requires at least 10,000 nested calls, tested; the
implementation stops at 100,000 with a runtime error, never a crash). The same lesson applies to
any interpreter written in a language with a small native stack: don't map guest recursion onto
host recursion.
No build step: vanilla JS with JSDoc types
The code is plain ES modules with // @ts-check and JSDoc types; TypeScript's compiler only
type-checks (npm run check), and there is no build step. Two checks run: src/ with no Node or
DOM type libraries (proving the core runs in a browser and in Node unchanged), then web/ with DOM
types. A recursive JSDoc type needs two steps (Value = ... | List, then List = Array<Value>);
writing Value[] inside Value directly fails with TS2456.
Playground editing
The browser playground edits the drawing like grid paper: typing overwrites, Enter moves down like a
typewriter, the caret can sit past the end of a line, and shortcuts insert shapes and draw edges
(Alt+Shift+arrows; backing up erases). Grid edits are pure functions with their own tests. Keyboard
shortcuts use event.code (layout-independent) and avoid Alt+E and Alt+F, which open Chrome's menus
on Linux. Every shortcut also exists as a button for touch screens.
Gotchas
- Drawing large test programs by hand is slow; a throwaway Python "canvas" helper (
put(row, col, text),box, hexagon, vertical line) laid them out. It only handled single-width characters, so wide characters broke box alignment. - Generate expected
.errand.graph.jsonfiles from the CLI, then review them, rather than writing them by hand.
See also
- Designing a programming language's syntax and semantics (lessons from H)
- Writing a native compiler in C that emits x86-64 assembly
- A production vanilla-JS single-page app (no framework, no build)