Smurf
No loops, no jumps, no numbers. To repeat anything, a Smurf program writes a new program and hands over control for good.
Matthew Westcott, 2001 · Turing complete: yes · ●●● hard · esolangs.org
Tested with 3 known programs · Test method
Run it
The idea
Matthew Westcott made Smurf in 2001, as a cut-down version of his language Muriel. The only data type is the string. There is a stack of strings and a store that maps names to strings.
Read the story
The one control command is x. It pops a string and runs it as the new program, after wiping the stack and the store. There is no way back. So a loop has to be a program that builds the next version of itself, with the loop's state baked into the code, and then runs it.
Conditionals use the same trick. You store each branch under a name, then build the name to look up from data, for example by taking the first character of a string. The specification notes that the original spec is ambiguous in places, and follows the creator's Perl interpreter.
The machine
A stack of strings and a store. Unset names read as the empty string, and popping an empty stack gives the empty string.
| Command | Does |
|---|---|
"..." | Push a string; \", \n and \\ are escapes, any other backslash stays |
+ | Pop two, push them joined in the order they were pushed |
i | Push the input (all of it the first time, then the empty string) |
o | Pop and print |
h t | Keep only the first character; drop the first character |
q | Quote: escape newlines, quotes and backslashes, and wrap in quotes |
p | Pop a name, then a value, and store the value under the name |
g | Pop a name and push its value |
x | Pop a string and run it as the program, with an empty stack and store |
Whitespace and other characters are ignored.
Example
Safalra's program from the specification reverses its input.
Safalra's program from the specification reverses its input. Each round takes the first character off the remaining input, adds it to the front of the output so far, and builds a new program that holds both strings as quoted literals. Then x runs it. When the input is used up, the first character is empty, so a lookup by that character picks the stored code that prints the result instead.
Patterns it teaches
- Stack machines: a stack, but of strings.
- Code as data: programs are built with
+andq, then run. - Self-modifying code: every loop step replaces the whole program with a new one.
- Turing tarpit: nine commands and one data type are enough.
Try this
- Print the input twice.
- Store
"Hi"under the namea, then print it withg. - Hard: print
xten times usingxto repeat.
Further
- esolangs.org/wiki/Smurf
- Muriel, the language it cuts down.
See also
Self-modifying
Stack-based
Write your own in Run Convert a program from Smurf
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: smurf.
Command line
shib run smurf -e '"Hello, world!"o'shib manual smurfSource
- Module
langs/stack/ports.js- Interpreter
langs/stack/smurf.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org