Algol*24
A retro-modern language: classic Pascal syntax over unbounded integers, full Unicode, gradual types, closures, extension functions, bitwise operators and a foreign function interface. The compiler is written in Algol-24 and compiles itself — and so are the bytecode machine that runs it and the library that comes with it.
v0.5.0 · library v0.3.1 · on Homebrew
class Doughnut; begin procedure Cook(); begin WriteLn ('Fry until golden!'); end end class BostonCream(Doughnut); begin procedure Cook(); begin super.Cook(); WriteLn ('Pipe full of custard and coat with chocolate!'); end end BostonCream().Cook();
Install
Two formulae, three platforms.
$ brew tap schildawg/algol24
$ brew trust schildawg/algol24
$ brew install algol24-vm
$ algvm --version
algvm 0.5.0
The middle line is required rather than advisory: Homebrew will not load a formula from a third-party tap until you have said that you trust it.
That is the machine with the library beside it, installed where a
uses looks — so a program says uses graph; by name and
the installed unit answers, with no path written in the program and a unit of
your own beside it still winning. It is what most people want, and it is the
main release.
The compiler is a second formula, and the two coexist. Install it when you want C out of the language, or a binary with no runtime to carry:
$ brew install algol24
$ algc --version
algc 0.5.0
Both formulae carry the library as of v0.5.0, pinned to the same release, so
either one alone is a working setup. Before it this second one installed a
language with nothing to write programs against: uses graph;
resolved nowhere, the library having been bundled only with the machine.
Bottled for arm64_sequoia, arm64_tahoe and
x86_64_linux. Anywhere else Homebrew builds from source, which
needs a C compiler and takes seconds. The specification is installed beside
either one.
Windows is a release download rather than a formula, since Homebrew is not a
channel there: unzip it anywhere and run algvm.bat. Foreign calls
work — an external reaches a DLL through the same declaration a
Unix uses.
All three are run rather than assumed, and only as of v0.4.1. Before it the Linux build of the machine did not finish and Windows mangled every non-ASCII path it was handed, neither of which anything here could see — what a second host asked is the whole of that release.
The platforms
Three processors agreed, one platform at a time.
Everything else on this page is Algol-24 agreeing with itself, and for four releases every gate that checked it ran on macOS. Put the same programs on a second C compiler and the compiled back end answered differently; put them on a second operating system and the runtime read text it had written and nobody else's. v0.4.1 is what a real session on Windows and a real session on Linux found, which is a different list from anything the harnesses here could produce.
Operands evaluate left to right, and in compiled C they did not. The back end emitted an operation's operands as a function's arguments, and C leaves the order of those unspecified: clang went left to right, gcc went right to left, so one expression read two lines of input in opposite orders on the two. The interpreter and the machine both go left to right, so the language does, and the compiled back end now does too.
var N := 0; function Next () : String; begin N := N + 1; Exit (Str (N)); end var Both := Next () + ', ' + Next (); WriteLn (Both); 1, 2 // under all three, and on either C compiler
Where order can matter at all, every operand but the last is now evaluated into a temporary through a comma expression, which C does sequence. Three conformance cases had said so all along; only one of the three processors was reading them on the compiler that happened to be installed.
On Windows, every path and every argument goes wide. The
runtime's narrow calls decode by the ANSI code page where the language's text is
UTF-8, so TextFile ('café.txt') opened a file nobody had named,
Entries listed names in an encoding the program could not read, and
before any of that algc café.a24 asked for a file that did not
exist — main's own argv having already been decoded.
One set of wrappers widens once and calls the wide form.
A round trip inside the runtime could not have caught that.
Mangling every name the same way, it found, listed and renamed its own files
perfectly. What showed it was a name written by something else:
git, checking a fixture out under the name it really has.
On Linux the machine did not build at all, which is why those
gates had never run there. The probe that finds the flag making a binary's own
symbols reachable tried Apple's spelling second, and GNU ld reads
-export_dynamic as -e xport_dynamic, the entry symbol:
it warns, links anyway, and the executable begins in the middle of nothing and
spins. That probe already caught a crash, by calling through the pointer, and a
silence, by comparing output. A hang was the third way, and output cannot catch
a hang.
Two more were about reaching a foreign library, which no conformance case can
ask — every conformance run is a build without foreign calls, so the corpus
opens no library at all. A shim is now searched in the working directory, where
a unit has always been found, so a library developed in place resolves its
uses and its first C call together. And a bare
external 'atan' resolves from the running program, which on Linux
means a libm that nothing had referenced and --as-needed duly
dropped.
One rule can be satisfied or not by how a correct program is
built, and it is the only one in the specification that can. A symbol
compiled into the program is reachable by name only if the program was linked to
export it — -rdynamic on ELF, --export-all-symbols on
PE, nothing at all on Mach-O. [FUN-014] names that limit rather
than leaving it to be discovered at the call, because there is no runtime answer
to it: opening the running program succeeds and the lookup still fails.
The rest are the kind of thing only a second host asks about. Standard output
was in text mode on Windows, so every #10 left as a pair, against
two rules that name the character and never the host's separator.
GetDir answers / on every host now, Windows included,
so a program joining a name on with GetDir () + '/' + Name builds
one path in one spelling. And eight recorded outputs had been passing by
accident: Git for Windows checks out with autocrlf, and text-mode
output was adding the same character back. Two wrongs agreeing, found from the
other side.
The build
It compiles itself.
What is checked in is the compiler's own output. bootstrap/ holds
the C that Algol-24 emitted when it last compiled itself, so building needs a
C compiler and nothing else — no toolchain, no package manager, no bootstrap
binary to trust.
// two stages: the seed builds with cc, then compiles the real thing
$ ./bootstrap/build.sh
$ algc --compile --out=build hello.a24
$ cc -std=c11 -O2 -o hello build/*.c
The build then diffs stage two's output against the checked-in seed. They must
be identical. A difference means the seed is stale, which is exactly what you
expect after editing the compiler — and --reseed is how you say so
deliberately rather than by accident.
The language
Types are optional, and checked when written.
An unannotated name is Any, compatible with everything in both
directions. Writing no type is never itself an error, and adding one can only
add checking — so annotating more is always safe.
var Name := 'Ada'; // inferred var Count : Integer := 0; // declared const Limit := 100; // cannot be reassigned function Add (A : Integer, B : Integer) : Integer; begin Exit A + B; end
Semicolons terminate rather than separate, so the one before end is
required. Exit returns; there is no return. Functions
are values, they nest, and they close over what they see.
The capability
Classic on the surface, not underneath.
An Integer is unbounded: arithmetic past the machine width grows
rather than wrapping or raising, and demotes again the moment a result fits.
There is no build switch. Source, identifiers and text are Unicode throughout,
and text is measured in characters rather than bytes — so
Length ('你好') is 2.
class Point; var X : Integer; Y : Integer; begin operator + (Other : Point) : Point; begin Exit Point (X + Other.X, Y + Other.Y); end end
Classes with operators a program may define, closures and nested subprograms, exceptions, enumerations, five collections, modules including circular ones, and a foreign function interface that reaches C — so a program can call SDL or libm directly. The examples draw a pie chart and the word hello in five scripts, with color emoji, into an SDL2 window.
The bits of an Integer are reached with &, |,
^, ~, << and
>>, and every Integer is two's complement extended
infinitely to the left — so every operation is total and no width is named
anywhere. A left shift grows rather than wrapping, and a right shift floors
where div truncates.
WriteLn (6 & 3); // 2 WriteLn (~0); // -1, every bit set WriteLn (-1 & 5); // 5, all ones selects everything WriteLn (1 << 70); // 1180591620717411303424
A routine obliged to pick a width instead is correct for the values that fit
it and silently wrong for the rest, with nothing in its declaration able to
say which — which is why these are operators rather than a library's
business. A class may define the five binary ones and unary ~
for its own instances, and and, or and
not are untouched: they test truthiness and short-circuit, and
writing one where a bitwise operator was meant now raises a
[WARN].
A Buffer is the language's record of bytes — a documented byte
order, PutInt and GetInt, PutDouble,
byte subscripting — and until v0.5.0 it could not reach a file. A
TextFile reads and writes lines of text, and a buffer
holding a zero byte has no text to write, so bytes could be held by this
language and never stored: an image, a record, a sound, a sprite sheet each
needed C, and two projects had already written that C. Load and
Save move the whole file either way, because a buffer holds all
of one or none of it, and GetText cuts a span of those bytes out
as a String — the inverse that appending never had.
The extension
Free functions that read like methods.
A top-level subprogram marked extension may also be
called with method syntax on its first parameter: Trim (S) and
S.Trim () are one call, and a unit of free functions composes
left to right — Line.Trimmed ().Shortened (12).Upper ().
extension function Shortened (S : String, To : Integer) : String; begin if Length (S) <= To then Exit S; Exit Copy (S, 0, To) + '...'; end WriteLn (Shortened (Title, 4)); // both spellings WriteLn (Title.Shortened (4)); // are one call
A member of the name always wins, so no call that compiles today can change
meaning — and where a member does hide an extension you can see, a
[WARN] names the class that took it. What a value responds to is
decided by the calling file's own uses clauses rather than
globally, so a unit that imports Strings for its own use does not
hand S.Trim () to its importers. extension is not a
keyword either — it is an ordinary identifier recognized by its position,
so a program may still name a variable after it.
The language was feature-complete at v0.1.0, and it is stable rather than
closed: it changes only on measured demand from a project using it, and the
measurement goes in the proposal. This was the first such change, because the
library now being written in Algol-24 had measured the alternatives — grow the
core one verb at a time, or wrap String in a class whose seam
answers 13 for the length of a five-character string — and a unit of functions
that composes is neither.
Every change since added a capability or corrected a mistake, leaving what a working program means alone. v0.3.0 is the first that does not: a comparison that was false is now true. The minor version moves for that reason and no other, which is what a version number is for.
v0.1.6 is three more on the same terms: the bitwise operators above, a
precision for Str that renders 2.675 to two places
as 2.67 because it rounds from the value rather than from a
rendering of it, and Buffer accessors for a four-byte unsigned
word and an eight-byte IEEE 754 Double — little-endian whatever the machine
is, so a record written on one and read on another agrees.
The reflection
A program can read its own shapes.
It can ask what a class declares, construct one from a type it was handed, read and write a field by a computed name, call a method by one, and ask whether a value satisfies a type — without a single new piece of syntax. Every one of those is a built-in, or a member of a value the language already had.
var Thing := Coin (7); var Shape := Fields (Thing); // name -> declared type for var F in Shape do WriteLn (F, ' : ', Shape[F], ' = ', Str (Member (Thing, F))); var Made := New (TypeOf (Thing), [7]); SetMember (Made, 'Worth', 11); WriteLn (Str (Invoke (Made, 'Scaled', [3]))); Worth : Integer = 7 33
A type is a value. TypeOf (X) answers it and a class
answers itself; T.Name, T.Parent,
T.Fields, T.Methods and T.Properties are
what a class says about itself, and T.Accepts (X) is the dynamic
form of is. Type values are interned, so TypeOf (1)
and TypeOf (2) are the same value and compare by identity — and a
type is not constructible, which TypeOf (42) () says rather than
answering an Integer instance.
No metaclasses, no attribute hooks, and nothing creates or alters a class at run time. The data is distilled when the program is built rather than kept as a live syntax tree, because a tree held for the life of a program is thousands of nodes retained for a few dozen bytes of metadata. There are no frames, no stacks and no source.
One sharp edge, stated in the specification rather than left to be found: a
computed name reaches a private member.
private is settled before the program runs and nothing records it
afterwards, so reflection can do what written code may not. The guard that
remains is uses.
The machine
Three processors, and they must agree.
algvm is a stack machine for Algol-24, written in Algol-24 and
compiled by algc like everything else here. It is a second binary
rather than a mode of the compiler: it imports the compiler's own front end, so
a program that will not parse fails the same way whichever of the three you
reach for. Its opcodes are numbered as Crafting Interpreters numbers
them, so the book reads alongside the source.
Each of those entries carries a worked example as of v0.4.1 — a whole
program, and the window of algvm --list where that instruction
appears, as the disassembler prints it. They are not transcribed: the
reference's own checker writes each program out, lists it, and requires the
window back contiguously and in order, so eighty hand-copied disassemblies
cannot quietly drift the first time the emitter changes. Seventy-six of the
eighty are run and compared against the machine, and the four that are not say
so themselves — the only program reaching them declares three hundred
globals.
This page is the evidence, and the check runs on every push. The generator that builds it is 2,374 lines of Algol-24 reading 30,000 lines of Markdown; run it under each processor in turn and the bytes that come out do not move.
$ ./build.sh # the compiled generator, 0.19s $ mv site site-cc && mkdir site $ algvm gen/Main.a24 # the machine, 4.41s $ diff -r site-cc site # and it prints nothing
Starting it meant compiling first, every time, and as of v0.5.0 it does not
have to. algvm --emit writes the bytecode out as a file and
algvm runs that instead of a source tree:
$ algvm --emit gen/Main.a24 --out=gen.a24b
$ algvm gen.a24b # the same four pages, 4.15s
Which is a quarter of a second off 4.41s, and saying so is more use than quoting the headline. The release measures a game whose start-up falls from 3.4s to 0.02, because almost all of it was compiling; this generator is the opposite case. Compiling it costs 248ms of the 4.41 — 7 to scan, 219 to parse, 5 to resolve, 8 to check, 9 to emit — so an image saves those 248ms and nothing else, the remaining four seconds being the machine actually running. The feature is worth exactly the ratio between those two numbers, and here that ratio is 6%.
A file says which kind it is by its first four bytes rather than by its name,
so .a24b is a convention and an image called .txt
runs. An image is a build artifact and not a second seed: it carries the
release that wrote it, and any other build refuses it rather than running
bytes it would read as different instructions.
$ algvm stale.a24b
This image was written by algvm 0.9.9 and this is algvm 0.5.0. Re-emit it.
That refusal is what makes an image safe to hand to somebody rather than a trap to document. An opcode's number has never been part of this language's interface — the machine renumbers them freely, and the advice for adding one is to append it and renumber nothing because the number promises nothing — so the same bytes mean different instructions in two builds. What the format cannot close is staleness: nothing checks that an image is newer than the source it came from, the language having no way to ask a file its age, so a stale image runs stale code until it is re-emitted.
A test run needs its own image, because --test changes what gets
compiled rather than what gets run — a unit's bare statements are left out and
its test bodies put in, so one file cannot serve both. This generator's own
124 tests run from one of each and report identically, and asking either
image for the other mode is refused by name instead of answered wrongly. Its own gate compares the two paths over the
whole corpus: 222 programs answer identically from a file and from their
source, and the 63 the emitter refuses are checked to refuse the same way
either way.
It also hosts the compiler: run algc on the machine and
ask it for C, and the C is byte-identical to what a native algc
emits — the whole front end and the whole back end, running as bytecode,
agreeing to the byte.
The tree-walking interpreter is the slow one — 48.9s for the same four pages, having gained a collector of its own in v0.2.4 — and it agrees to the byte as well. A disagreement would be a bug in whichever processor is the odd one out, and that is most of what building a third one was for.
Collection is precise rather than a guess. The compiler emits
the addresses of a program's module-level variables and a shadow stack for its
locals, so nothing has to ask whether a word might be a pointer. All three
processors collect without being asked now — the machine from the start, the
tree-walker since v0.2.4, and compiled code since v0.2.6, which also made
text collectable at all: a String used to come from an arena that never
freed, so every Str and every + was held for the life
of the process.
Building this site is the measurement. Peak memory fell from 84 MB to 38 MB compiled and from 321 MB to 64 MB on the machine, for about 15% more time on the machine and none measurable anywhere else. It is easy to misremember any of this as a feature of the machine, and it is not.
A runtime error now says where it happened, file and line, under the interpreter
and under the machine. Compiled Algol-24 deliberately carries no source
positions and will not: storing one before every statement is a cost
every program would pay forever so that a failing one could be read. A program
that wants that runs on the machine, which can disassemble itself —
--list walks every chunk, --trace N keeps the last N
instructions with the stack pointer and call depth, and --watch
dumps the instant a message is raised. Each processor finds the location its own
way, and the machine pays nothing at all until something fails.
The library
What alpha meant.
algol24-library v0.3.1 is eleven units, borrowing Turbo Pascal's names
as an homage rather than as a compatibility target. Its version is its own on
purpose: the library is a user of the language rather than part of it,
with its own repository, its own releases and its own record of what it has
found wrong.
graph and sound reach SDL2 through the language's own
foreign function interface, which is the argument for having one: a library
written in the language can go and get what the language does not have.
uses graph; InitGraph (640, 480, 'algol24.com', False); var V := ViewPort (0, 0, 640, 480, 1); SetColor (V, LightCyan); Circle (V, 320, 240, 180); Show (V);
The examples are the part worth singling out. Every one in the library's reference is run and its output compared against what the entry claims, so an example that stops being true fails a build rather than misleading a reader.
The library crossed to the other two platforms over its own 0.3.0 and 0.3.1,
and it paid for the crossing in the same coin: every unit's suite was run on a
Windows machine, the reference examples with it, and the example application
checked to agree interpreted and both ways compiled. graph had
named a Homebrew path to SDL in twenty-four declarations, which no amount of
renaming could make portable — a wrapper per call in its own C answers that, and
the build asks sdl2-config where SDL actually is. Every call that
takes or answers text is wide on Windows now, for the reason the runtime's own
were. And random stopped borrowing libc's drand48,
which Windows has not got: the generator is carried here and measured
bit-identical against the platform's own, so a seed answers the same
sequence everywhere — which it did not before.
This is also what the project meant by calling itself alpha. What the language
lacked was never a feature; it was a library written in Algol-24 rather than
bolted into the runtime. It needed exactly one addition to the language to be
possible — a uses now finds a unit in an installed library, not only
one beside the program — and nearer still wins, so installing a library can never
change what an existing program already means by a name.
The guarantee
The specification is the authority.
Where the implementation and the specification disagree, it is the implementation that is wrong. The difference is recorded as a defect and carries a program that reproduces it.
Every case runs interpreted and compiled, and every run must be green; the 285 conformance and refusal cases run on the machine as well. A case the interpreter gets right and the compiled program gets wrong is a gap, and a gap is a failure rather than a note. A defect's test is stranger still: it records the wrong behavior and passes for as long as that behavior lasts, turning red the day the defect is fixed.
None of which finds the bugs that a program finds. v0.2.4 was sixteen defects, and not one of them was found by reading the compiler: every report came from a game of about twenty units, or from this website — both written in Algol-24 by people meeting the language as users of it. Five of the sixteen turned out to be one bug, and its worst shape read a program-level variable as an imported enumeration's member, so every assignment to it was discarded in silence.
It has grown by six since, and this site found one of them: a line of text coming back two characters short, because a cache remembering how many characters a string has was keyed on where its bytes were. Safe while text came from an allocator that never freed; wrong the moment a heap recycled one. v0.2.7 is that fix and nothing else.
The other two came from the language contradicting itself. With
C a Char and S the one-character String of the same
text, every processor answered C <= S true and
S <= C true — and C = S false, which
describes an ordering no value can satisfy. v0.3.0 settled it the only way that
leaves the rest of the language standing, and two silent defects went with it,
in two programs that needed no edit: a filter that could never filter, and a
window that remembered its zoom and forgot where it was. The four-character key
worked and the one-character keys did not, which is why nobody noticed.
The three most recent are a different shape again, each found by running a
processor in a mode no gate here was running. algvm --test
crashed compiling a test block where algc tested the same file
clean; algc --test dropped the file and line every other mode
reports; and the third was answered in the specification rather than in code
— a test run evaluates a program's declarations and skips the statements
between them, so a declaration can take a value no run of that program could
produce.
$ ./defects.sh
0 reproducing, 22 fixed, of 22 checked
That harness is new, and it is the only one here written to fail: each report is reduced to the smallest program that shows it and run under whichever processors disagree, so the sweep goes green only when every one is fixed and stays green only while they remain so.
Then there is the compiler itself. It compiles itself, compiles itself again with the result, and the two emissions must be byte-identical. That is a determinism check as much as a correctness one — no hash-order iteration, no timestamps, no pointer values may reach the emitted text. A compiler can be entirely correct and still fail it, and nothing else would notice.
$ ./fixedpoint.sh
generation 2 is identical to generation 1
The name
Why Algol, and why 24.
Algol-24 is not a successor to ALGOL 60. It is its own language, and a modern one. The name is for the lineage that runs through Pascal, and because the two look strikingly alike on the page.
The 24 is a vintage, not a version. It marks 2024, when the work started. There will be no Algol-26.
Algol is also a star — β Persei, the Demon Star, an eclipsing binary whose two components pass in front of one another every 2.87 days. Two bodies that have to be observed together, whose agreement over time is the whole measurement. For a language with an interpreter and a compiler that are required to agree, the name turned out to be doing more work than it was given.
Provenance
It began as Lox.
Algol-24 descends from Bob Nystrom's Lox, from
Crafting Interpreters — a debt
that is still visible in the architecture, in the
Scanner → Parser → Resolver → Interpreter pipeline, and in the empty
vm/ directory waiting for the bytecode machine from the book's
third part. The syntax became Pascal's because Pascal was the first language
its author loved. Everything since has been the features worth keeping from
forty years of using other people's.