Shakespeare
Programs are plays. Characters are variables, insults are negative numbers, and every adjective doubles the value.
Karl Hasselström and Jon Åslund, 2001 · Turing complete: yes · ●○○ easy · esolangs.org
Tested with 3 known programs · Hello World on Try It Online: identical except the line end · Test method
Run it
The idea
Karl Hasselström and Jon Åslund created the Shakespeare Programming Language (SPL) in 2001. The aim was source code that reads like a play: a title, a cast list, acts and scenes, stage directions and dialogue. A compiler translates it to C.
Read the story
The trick is that the theatrical words carry real meaning, and the rest is decoration. A character's name is a variable. Telling someone they are "a big old cow" assigns them a number. Asking "Am I better than you?" is a comparison.
The result is verbose and very funny. Hello World is a long scene of compliments and insults whose only purpose is to arrive at the right character codes.
The machine
| Part of the play | Means |
|---|---|
| Title (up to the first full stop) | A comment |
Cast list: Romeo, a young man. | Declares a variable; the description is a comment. Names must be Shakespeare characters |
Act I: / Scene II: | Labels for jumps; the text after the colon is a comment |
[Enter Romeo and Juliet], [Exit Romeo], [Exeunt] | Who is on stage. At most two at once |
Romeo: followed by sentences | Romeo speaks to the other character on stage, "you" |
Inside the dialogue:
- Numbers. A noun is 1 if it is nice or neutral (
king,cat) and −1 if it is nasty (pig). Each adjective in front doubles it:a big old catis 4.nothingis 0. - Arithmetic.
the sum of X and Y,the difference between,the product of,the quotient between,the square of,twice. A character's name, oryourself, stands for its current value. - Assignment.
You are as good as X!sets the listener to X. - Output and input.
Speak your mind!prints the listener's value as a character;Open your heart!prints it as a number.Open your mindandListen to your heartread input. - Jumps.
Am I better than you?sets a flag;If so, let us proceed to scene II.jumps on it. - Stacks. Every character is also a stack.
Remember me.pushes the speaker's value onto the listener's stack, andRecall ...pops the listener's stack into the listener.
Example
Print Hi (72 and 105):
Print Hi (72 and 105):
The Two Letters.
Romeo, a young man who speaks.
Juliet, a lady who listens.
Act I: A greeting.
Scene I: Two letters.
[Enter Romeo and Juliet]
Romeo:
You are as good as the sum of a big mighty proud rich happy sweet king
and a big red old cat! Speak your mind! You are as good as the sum of
yourself and the sum of a little tiny big old red cow and a cat! Speak
your mind!
[Exeunt]
Traced by hand:
a big mighty proud rich happy sweet king: six adjectives on a nice noun, 2^6 = 64.a big red old cat: three adjectives, 8. Juliet becomes 72, and speaksH.a little tiny big old red cowis 32, plusa catis 33, plusyourself(72) is 105:i.
Every word above is in SPL's word lists, which is the part beginners usually get wrong. A word that is not in the lists (say loud) is a syntax error, and so is a nice adjective on a nasty noun.
Run it
The whole play can sit on one line: line breaks are only whitespace.
The whole play can sit on one line: line breaks are only whitespace. Print ! (32 + 1):
A loop: Romeo prints Juliet as a number, adds one, asks whether she is past five, and jumps back if not.
Input: Listen to your heart reads a number into Juliet, and twice you doubles it.
The interpreter follows spl2c, the original compiler that TIO runs. Numbers are 32-bit integers, Open your heart prints no newline, and printing a character outside 0 to 255 stops the play.
Patterns it teaches
- Camouflage: the classic "program disguised as literature". Compare Chef (recipes) and Rockstar (song lyrics).
- Encoding tricks: numbers are built in binary, one adjective per bit of doubling, and added together.
- Stack machines: with
RememberandRecall, two characters are two unbounded stacks, which is enough to simulate a tape.
Try this
- Make Juliet speak
!(33) in one sentence. - Print
Helloby reusingyourselfso each letter is a small step from the last. - Hard: write a countdown from 5 with
Am I better than nothing?and a jump back to the scene.
Further
- esolangs.org/wiki/Shakespeare
- The original documentation and compiler: shakespearelang.sourceforge.net
- A modern Python interpreter: shakespearelang on GitHub
See also
Pseudonatural
Stack-based
Write your own in Run Convert a program from Shakespeare
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: shakespeare.
Command line
shib run shakespeare program.txtThe program is long; save it as program.txt first.
shib manual shakespeareSource
- Module
langs/tio/ports.js- Interpreter
langs/tio/shakespeare.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org