esolang

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

Expected: olleh

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.

CommandDoes
"..."Push a string; \", \n and \\ are escapes, any other backslash stays
+Pop two, push them joined in the order they were pushed
iPush the input (all of it the first time, then the empty string)
oPop and print
h tKeep only the first character; drop the first character
qQuote: escape newlines, quotes and backslashes, and wrap in quotes
pPop a name, then a value, and store the value under the name
gPop a name and push its value
xPop 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

Try this

  1. Print the input twice.
  2. Store "Hi" under the name a, then print it with g.
  3. Hard: print x ten times using x to repeat.

Further

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 smurf

Source

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