← Pablo Murad

Note XXI

huh.guru is a new site of mine. It publishes the research notes of the Quantum Guru, a fictional researcher at the Institute for Unnecessary Research: short, serious-sounding reports on problems nobody asked to have solved, like the USB plug that always needs three attempts, the socks that go into the washing machine in pairs and come out as individuals, or the printer that knows when a document is urgent. Every page of it is generated by a program written in LOLCODE.

The front page of huh.guru on a white background in a serif typeface: the title Huh Guru with a small meditating figure, the line "The Quantum Guru, Institute for Unnecessary Research", the motto "Quantum answers to questions nobody asked. Peer-reviewed by absolutely nobody.", links to About and CAN HAS COLOPHON?, and the first findings, The topological USB paradox (HUG-0001) with two crossed-out hypotheses, and The thermodynamics of printers (HUG-0002).

What LOLCODE is #

LOLCODE is an esoteric programming language, put together in 2007 by Adam Lindsay out of the broken English people wrote in the captions of cat photos, the lolcats.1 A program opens with HAI and closes with KTHXBYE, a variable is declared with I HAS A, and a comment starts with BTW. It looks like a joke, and it is one, but it's also a real language with variables, loops, conditions and functions.2

The smallest program prints one line:

HAI 1.2
VISIBLE "O HAI, HUH.GURU"
KTHXBYE

Variables don't need a type written next to them. The value decides: a whole number is a NUMBR, a decimal is a NUMBAR, text is a YARN and a boolean is a TROOF, which is either WIN or FAIL. VISIBLE accepts several values in a row and glues them together:

HAI 1.2
I HAS A NAME ITZ "GURU"
I HAS A FINDINGS ITZ 30
I HAS A SERIOUS ITZ FAIL
VISIBLE "THE " NAME " HAS " FINDINGS " FINDINGS"
KTHXBYE

That prints THE GURU HAS 30 FINDINGS. A loop names itself, says which variable to count with and when to stop:

HAI 1.2
IM IN YR LIST UPPIN YR N TIL BOTH SAEM N AN 3
  VISIBLE "FINDING NUMBER " SUM OF N AN 1
IM OUTTA YR LIST
KTHXBYE

It prints FINDING NUMBER 1, 2 and 3, one per line. Arithmetic puts the operation first, so SUM OF N AN 1 is n + 1. And a function, with a condition inside, that turns a number into an ID in the site's format:

HAI 1.2
HOW IZ I ID YR N
  BOTH SAEM BIGGR OF N AN 10 AN N, O RLY?
    YA RLY, FOUND YR SMOOSH "HUG-00" AN N MKAY
    NO WAI, FOUND YR SMOOSH "HUG-000" AN N MKAY
  OIC
IF U SAY SO

VISIBLE I IZ ID YR 4 MKAY
VISIBLE I IZ ID YR 27 MKAY
KTHXBYE

That prints HUG-0004 and HUG-0027. HOW IZ I opens a function and IF U SAY SO closes it, FOUND YR returns a value, and SMOOSH ... MKAY joins strings. There's no "greater than" in the language, so the condition asks which of the two is BIGGR and checks whether the answer was N.3

An interpreted language #

LOLCODE is interpreted. In a compiled language like C, a compiler reads the source once and turns it into machine code, a separate binary that runs on its own afterwards. An interpreted language skips that step: another program, the interpreter, reads the source and carries out each instruction as it gets to it. Nothing new is left on disk, and the source is read again every time the program runs.

For huh.guru the interpreter is lci, written in C by Justin Meza. It reads a .lol file and runs it, and the program it runs reads the findings and writes the HTML of the site. Python is interpreted too, so the site went from one interpreter to another.

Easy, just repetitive #

The program is long, but writing it wasn't hard. LOLCODE is easy. It's repetitive, because the language has almost nothing built in:

  • An if takes four to six lines. In LOLCODE it's O RLY?, YA RLY, the code, NO WAI, the code and OIC, each on its own line. What Python writes as if x: y turns into a block.
  • There's no text library. The language has no substring, split, replace, strip, lowercase or search. I had to write each of them by hand (sub, trim, findc, lower), all of them walking the string one byte at a time with STRING'Z AT.
  • There are no regular expressions. The Python version marked up emphasis, code and strikethrough with three calls to re.sub. In LOLCODE that became a state machine of its own, plus a separate function for links.
  • HTML goes out one line at a time. Python has strings that span several lines. In LOLCODE every line of the template is a separate call, with every " written as :" and every : as ::.
  • The colons needed one more piece of code. lci resolves escape sequences again every time a string is touched, so a colon from a finding can turn into a line break after a few concatenations. The program swaps every colon for an invisible sentinel character when it reads a note and swaps it back when it writes the page.

LOLCODE can't list the files in a folder either, so the program finds the notes by trying names in order, hug-0001, hug-0002 and so on, and stops after a hundred misses in a row. Even so, the whole site builds in about a quarter of a second.

The colophon tells all this in the Institute's voice, as a short LOLCODE course with numbered lessons in the wrong order.

The CAN HAS COLOPHON? page of huh.guru: a short introduction saying the site is generated by a program written in LOLCODE, followed by Lesson 1, a three-line program with HAI 1.4, VISIBLE "HAI WORLD" and KTHXBYE, and Lesson 3, a program that declares ATTEMPTS as 3, adds 1 to it and prints it.

Notes

  1. The captions came from the lolcats craze of the mid-2000s. I Can Has Cheezburger?, the site that made it famous, started in January 2007, the same year as the language. ↩

  2. Esoteric languages got their own part in this blog's series on unusual computers: A different kind of computer I. ↩

  3. The commas in that function work as line breaks. Without them, each part of the condition would need a line of its own. ↩