The compiler, in Go

Hills, one of the two pieces timed in the table below, built by the compiler in Go.
The compiler is now written in Go. The old one, in Dart, has been deleted. Before it went, the two agreed on everything I knew how to ask them. Since then the new one has been sorted into folders that make sense, and its tests have been checked as closely as it was.
What was done
It was a translation, not a redesign. Each file of the old compiler became a file of the new one, in the same order, with the same names, so the two could be read side by side. No new words in the language, no tidying, no clever ideas along the way. The old compiler was 14,400 lines. The new one is 26,000. Go says things at greater length.
The work went in three parts:
- The part that writes and shrinks the JavaScript.
- The part that reads a piece and finds its libraries.
- The middle, which is the rules of the language and most of the compiler.
The first two were done at the same time. The third had to wait for both.
How I know it worked
The same way I said I would. The new compiler was put through the same checks as the old one.
| Check | Result |
|---|---|
| The rules and messages of the language | 637 of 637 |
| The sketchbook | 29 of 29 |
| Builds the same, byte for byte | 180 of 180 |
On top of that, each part was compared with the old compiler on its own, before the next part was built on it. The shrinking step was run on 12,800 inputs and gave the same output on every one. The reader was fed 30,000 deliberately broken pieces and gave the same complaint, on the same line, each time. The whole compiler was then given about 16,500 more pieces, plain and damaged, and matched on all of them.
And every piece was built twenty times over, to see that the file never changes from one run to the next. It does not.
The two things I was worried about
Numbers. Dart and Go do not write numbers the same way. Dart writes one as 1.0, switches to a different form
for very large and very small values, and rounds halves in its own direction. A printer was written for Go that
copies all of it, and it was checked against three million numbers.
Order. Go shuffles some of its collections on purpose, differently on every run. Left alone, that would give a different file each time a piece was built. Everything the compiler walks through is now kept in the order it was added.
There was a third I had not thought of. When a piece says sin of a fixed number, the compiler works out the
answer while building. Dart asked the computer’s maths library, and that library is very slightly wrong: off in
the last digit about twice in a thousand. Go now works out the exactly correct value. None of my pieces lands on
one of the bad numbers, so nothing changed. One day a piece will, and it will differ from the old compiler in its
last digit. I have decided that is fine. The new answer is the right one, and it is the same on every machine,
which the old one never was.
What it found
Comparing two compilers this closely shows up things that one alone hides. Three faults turned up in the old compiler. All three were copied faithfully into the new one, because the job was to match, and they are now on the list to fix.
- A layer that names an effect from a library that does not exist makes the compiler fall over, where it should say what is wrong.
- A piece that declares an effect of its own cannot get a compile-time table on the SVG target. No piece of mine loses bytes to it today, the real layer included, but a new one could.
- In the sketchbook, the short names in the built file creep upward with each save. Harmless, but untidy.
What it did not give me
Speed. I said it would not be much faster. It is slower.
| Piece | Dart | Go |
|---|---|---|
| hills | 1.2 s | 1.4 s |
| One real layer | 2.3 s | 2.8 s |
Nearly all of that time is the search, which builds a piece over a hundred different ways and keeps the smallest. The Go version still does them one after another, exactly as the old one did, so that the two could be compared like for like. Go can run them side by side. That is on the list, and I expect it to be several times quicker when it does.
What I did get is what I wanted. One program file, about ten megabytes, with nothing to install beside it. It already builds for Windows, for Mac, for Linux, and to run inside a web page.
Finding my way around
The translation kept the old layout on purpose. That was right for proving it and wrong for living in. It left one flat folder, with the middle of the compiler in files numbered two to seven, cut by length and not by subject.
Once the translation passed, it was sorted into folders: one for reading a piece, one for finding its libraries, one for the rules and the drawing code, one for what is worked out while building, one for writing the JavaScript, one for shrinking it, one for packing, one for the search, and one for the sketchbook. The largest file went from 3,959 lines to 921.
One part did not go to plan. The plan put the rules of the language in one folder and the drawing code in another, with a folder for each target. That could not be done as a move. The middle of the compiler checks a rule, gives the message and writes the output in the same breath, inside the same functions. Pulling them apart would be a redesign, and this step was strictly a move. So the middle is one folder of about fifty files, named by subject: let, loop, curve, SVG marks, canvas commands, records, colour. It is not the split I planned.
To know the move changed nothing, every one of the 1,102 functions and types in the old layout was compared with its counterpart in the new one. 1,098 matched exactly. The other four were one name that had to change, and what followed from it. Then the same three checks passed again: 637, 29, and 180 of 180, byte for byte.
The layout itself was tested too. Someone who had never seen the code was asked questions like “where is a canvas circle drawn?” and had to guess the file from its name alone. The first time, six of ten were right. After a round of renaming files and the names inside them, eight of twelve were right, three half right, and none wrong. The half rights are fair: a curve is shaped in one file and written out in another.
The old one, gone
Then the old compiler was deleted. The folder is just called compiler now.
On the way, the tests were read over closely. That turned up faults, not in the compiler but in the tests themselves, and they are the interesting part.
- Of the 536 recorded builds the tests compare against, 122 were pointed at folders by their full address on one computer. On any other computer those builds were quietly skipped, and the test still said it passed. Twenty-six of them pointed at a temporary folder that would have vanished at the next restart.
- Four tests worked by running the old compiler live, so they would have gone when it went. Their results were recorded from it one last time before it was removed, and they still run.
- The tests read their inputs from my own working folders. The first time I edited a piece, they would have failed, and could not have been fixed, because the recordings can no longer be made. Every input is now a frozen copy kept beside the tests.
- Several tests printed a count and never checked it. About forty counts are now checked.
A test that passes is not the same as a test that checked something. So each fix was proven by breaking something on purpose and watching the test fail.
Leaving Dart behind
Matching the old compiler digit for digit was only ever the proof. It was the one cheap way to know that 26,000 lines of translation were right. Now that it is proven, there is no reason to keep printing numbers the way Dart did, or to keep things in the order Dart kept them. From here the compiler can do whatever is simplest, or whatever ships the smallest file.
The order still has to be fixed. Just not Dart’s.
There is a catch. The tests are now a fixed record of a compiler that no longer exists. That is right for proving a translation. It is wrong for a compiler I mean to change, because any change I make to the output on purpose will fail them. Before the three faults can be fixed, the tests need a way to accept a new recording, with the difference shown to me first.
Next
- Let the tests accept a new recording, with the difference shown first.
- Run the search side by side.
- Fix the three faults.
- Then the editor support, which is what all of this was for.
The piece at the top
The image at the top comes from this token, built for the svg target: 0x4737992cbb00dd6c423428f3eeb3a0881a5596b96160aea65dc0d67324afe245
// hills: ridges of smooth curves under a gradient sky, each worked with brush sweeps cut to its own outline
plate(1920, 2400)
use noise v2, colour v2, brush v2, spline v2
random layout, paint, marks
// three ramps, dark to pale; the token takes one
let ramps = [#1d2b3a, #3e5c76, #9fb4c7, #ece6d8, #3b1f1a, #8c3b24, #d98e4a, #f2dfc2,
#2a1f33, #6b3f6e, #c98fae, #f3e3ea]
let pick = paint.int(0 ..< 3) * 4
let ramp = [ramps[pick], ramps[pick + 1], ramps[pick + 2], ramps[pick + 3]]
let field = noise.table(layout)
// n points round a circle, the radius wobbling a little
fn blob(c: vec, r: num, n: int): vec[] {
let out = vec[]
for i in 0 ..< n { out.add(c + polar(TAU * i / n, r * layout.float(0.95 ..< 1.05))) }
return out
}
// the sky, and a low sun with its glow
background(linear(vec(0, 0), vec(0, 1700),
[colour.darken(ramp[0], amount: 0.35), ramp[1], colour.at(ramp, along: 0.85)]))
let sun = vec(layout.float(500 ..< 1400), layout.float(900 ..< 1250))
let glow = radial(sun, 700, [rgba(ramp[3], 0.85), rgba(colour.lighten(ramp[2], amount: 0.3), 0.3), rgba(ramp[2], 0)])
curve(blob(sun, r: 900, n: 8), closed: true, fill: glow)
curve(blob(sun, r: 170, n: 10), closed: true, fill: radial(sun, 170, [#ffffff, colour.lighten(ramp[3], amount: 0.5)]))
// four ridges, far to near, each lower and darker
let ridges = 4
for k in 0 ..< ridges {
let near = k / (ridges - 1)
let base = 1250 + near * 900
let outline = vec[]
for i in 0 ..= 12 {
let x = -160 + i * 175
outline.add(vec(x,
base + 340 * noise.octaves(field, point: vec(x * 0.0011, k * 7 + 3), layers: 3) * (1 - near * 0.4)))
}
outline.add(vec(2100, 2600))
outline.add(vec(-200, 2600))
let shade = 0.78 - near * 0.62
let ink = colour.mix(colour.at(ramp, along: shade), toward: ramp[0], amount: near * 0.55)
let top = base - 380
// the body: dark at the foot, lit along the crest
curve(outline, smooth: 0.9, closed: true, fill: linear(vec(0, top), vec(0, top + 900),
[colour.lighten(ink, amount: 0.18), ink, colour.darken(ink, amount: 0.4)]))
// brush sweeps across it, faded together as one wash and cut to the same curve
place(opacity: 0.55 + near * 0.3, clip: curve(outline, smooth: 0.9, closed: true)) {
for s in 0 ..< 9 + k * 4 {
let y = layout.float(top + 100 ..< base + 800)
let drag = vec[]
for i in 0 ..= 10 { drag.add(vec(-100 + i * 220,
y + 90 * noise.at(field, point: vec(i * 0.5 + s, y * 0.004 + k)))) }
let v = colour.mix(ink, toward: colour.at(ramp, along: layout.unit()), amount: 0.35)
brush.sweep(marks, path: spline.sample(drag, steps: 8), width: layout.float(110 ..< 260),
ramp: [colour.darken(v, amount: 0.3), v, colour.lighten(v, amount: 0.25)],
opacity: 0.8, density: 0.6, body: 0.4)
}
}
}