Code as data
When a program can pick up a piece of code, pass it around, glue it to another piece and only then run it.
●●○ moderate · where to see it
The idea
In most languages, code is something you write and data is something the code touches. They live in different worlds. Some languages erase the line, and a piece of code is a value like any other. You can store it, copy it, join it to more code, print it, and run it when you choose.
Read the story
Lisp made this famous (John McCarthy, 1958 to 1960). A Lisp program is written as lists, and lists are Lisp's main data type, so a program can build another program and hand it to eval. People call this homoiconicity; the code looks like the data because it is the data.
Esolangs push the idea to its limit. In Underload there is almost no other kind of data. The stack holds strings, every string is a program, and ^ runs one.
Two levels of the idea.
How it works
Underload's stack holds strings.
Underload's stack holds strings. A few of its commands.
| Command | Does |
|---|---|
(x) | Push the string x |
: | Duplicate the top |
~ | Swap the top two |
* | Join the top two (the lower one first) |
a | Wrap the top in parentheses |
^ | Pop the top and run it as code, right now |
S | Pop the top and print it |
A "double this" function, kept as data.
(:*)(ab)~^S
| Step | Stack (top on the right) |
|---|---|
(:*) | :* |
(ab) | :* ab |
~ | ab :* |
^ | runs :* on ab: : gives ab ab, * gives abab |
S | prints abab |
The function :* sat on the stack like any string until ^ turned it into action.
The same commands give the classic Underload quine, (:aSS):aSS. It pushes a piece of code, duplicates it, wraps one copy in parentheses and prints both. The program's text is its data.
Quotations
In the kernel, [ … ] pushes the text between the brackets as a list of characters. Nothing runs yet. CALL takes a list and runs it, reading it with the language’s own rules.
So a list is both a string and a piece of program.
Push code, run it twice
Expected output
AA
Output
Print code as text
The same value printed instead of run. Its characters come out.
Expected output
65 EMIT
Output
Underload does the same with ^
(S) is data until ^ runs it. Then it prints the text below it.
Expected output
xyz
Output
Changing the language while it runs
DEFINE takes a name and a body text and rebinds a word, or a reading rule, for the rest of the run.
DEFINE takes a name and a body text and rebinds a word, or a reading rule, for the rest of the run. The program changes its own language.
Redefine a word halfway
Expected output
Bc
Output
twicestarts asDUP ADD, so 33 + 33 = 66, the letter B.- After the
DEFINE,twicemeans "three times", so 33 × 3 = 99, the letter c.
An esolang is a dictionary
The language file takes this idea furthest.
The language file takes this idea furthest. A language for the kernel is a list of rules. "text" => kernel tokens reads text, and name => tokens defines a word. The brainfuck dictionary is eight such lines. Underload is nine, Befunge-93 is 36 rules and 8 words.
So "learning brainfuck" means growing eight lines, and "a new esolang" means a new dictionary. The last lesson of the learning path has you write one.
Try this
- In Underload, write a program that prints
abababusing a "triple" function. - Predict what
(a)(b)*:*Sprints, then check it by hand. - Hard: write an Underload program that pushes
(:aSS):aSSwithout the program containing that text literally.
Seen in
- Underload. Strings on a stack are the only data, and every one is code.
- Joy. Quotations
[ ... ]are programs as lists; combinators likeianddiprun them. - Forth. New words are compiled into the same dictionary the program can read and extend at run time.
- Befunge.
greads a character of the program grid as a number;pwrites one back. - Emmental. Goes one level up.
!takes a program off the stack and makes it the new meaning of a symbol inside the interpreter. - bff. One tape holds both code and data, so copying bytes is copying code.
Further
- esolangs.org/wiki/Underload
- John McCarthy, "Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I" (1960).
- See also metacircular interpreter, concatenative, self-modifying code.