esolang
Learn · Ideas114 languages show it

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.

  • Quoted code. Code is a value, but you cannot look inside it, only combine and run it (Underload, Joy quotations).
  • Readable code. The program can read its own text as raw characters (Befunge's g, bff's shared tape).

How it works

Underload's stack holds strings.

Underload's stack holds strings. A few of its commands.

CommandDoes
(x)Push the string x
:Duplicate the top
~Swap the top two
*Join the top two (the lower one first)
aWrap the top in parentheses
^Pop the top and run it as code, right now
SPop the top and print it

A "double this" function, kept as data.

(:*)(ab)~^S
StepStack (top on the right)
(:*):*
(ab):* ab
~ab :*
^runs :* on ab: : gives ab ab, * gives abab
Sprints 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


  • twice starts as DUP ADD, so 33 + 33 = 66, the letter B.
  • After the DEFINE, twice means "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

  1. In Underload, write a program that prints ababab using a "triple" function.
  2. Predict what (a)(b)*:*S prints, then check it by hand.
  3. Hard: write an Underload program that pushes (:aSS):aSS without 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 like i and dip run them.
  • Forth. New words are compiled into the same dictionary the program can read and extend at run time.
  • Befunge. g reads a character of the program grid as a number; p writes 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

Where to see it