Smurf

From Esolang
Jump to: navigation, search

Smurf is an esoteric programming language designed by Matthew Westcott in an attempt to cut down Muriel to the bare minimum, retaining that language's self-propagation paradigm. The only native data type is the string.

The runtime environment consists of a program (a series of characters), a stack of strings, and a variable store that behaves like an imperative language.

Memory

Smurf programs can access a stack of strings and a variable store, which maps variable names (which can be any string, including the empty string) on to values, which are also strings. An uninitialised variable will map on to the empty string.

Commands

Smurf programs consist of commands separated by whitespace.

The Smurf specification contains several ambiguities[1]. The description below takes into account the behaviour of the Perl interpreter written by the language's creator, which can be regarded as a reference interpreter for the language.

The string command

The string command, represented by a quotation mark, pushes the string that follows it onto the stack. A string is delimited by quote marks and may contain newlines, quotation marks and backslashes, so long as these are escaped by backslashes. Any invalid escape sequence is treated as a backslash followed by the escaped character (for example, "\x" is treated as if it were "\\x"). Examples of usage:

"example"
"\"\n\\"
"\"\"""\"\""

The first example pushes the string 'example' onto the stack. The second example pushes onto the stack a string consisting of a quotation mark, a newline and a backslash. The final example pushes two strings onto the stack, each consisting of two quotation marks.

The specification does not specify the effect of a newline occuring inside a string in a program. To ensure compatibility, programmers should instead use the escape sequence to encode newlines.

Stack commands

The command represented by a plus sign concatenates the two strings at the top of the stack, with the string pushed first appearing first in the resultant string.

The command represented by a lowercase 'i' inputs a string from the user and pushes it onto the stack. The specification does not define the behaviour of this command in non-interactive programs, so interpreters may read the input one line at a time, or alternatively use the whole input the first time the 'i' command is executed. The specification does not define the behaviour of this command when no input is present, so interpreters may push the empty string onto the stack, or alternatively not push a value onto the stack at all, although the former behaviour makes programming easier.

The command represented by a lowercase 'o' outputs the string at the top of the stack.

The command represented by a lowercase 'h' replaces the string at the top of the stack with its head (that is, the first character).

The command represented by a lowercase 't' replaces the string at the top of the stack with its tail (that is, all except the first character).

The command represented by a lowercase 'q' 'quotifies' the string at the top of stack - it escapes all newline, quotation marks and backslashes, and places quotation marks at the start and end.

Variable commands

The command represented by a lowercase 'p' pops two strings from the stack, and sets the variable with the name specified by the first string to the value specified by the second string.

The command represented by a lowercase 'g' pops a variable name from the stack, and then pushes the variable's value onto the stack.

The execute command

The execute command, represented by a lowercase 'x', executes the string at the top of the stack as a Smurf program. The stack and variable store are erased before the string is executed - in other words, the string replaces the currently running program, and there is no way to return to the program.

Example code

Reversing input

The following program by Safalra reverses its input, and demonstrates the simulation of conditionals and loops in Smurf. Newlines have been inserted every 80 characters for greater readability.

"+"i+""p""gtg""gt"i"p"\"\"p\"i\"gh\"o\"g+\"o\"p\"i\"gt\"i\"p\"\\\"+\\\"\\\"\\\"p
\"\"i\"gq+\"tg\"+\"i\"gq+\"\\\"i\\\"p\"+\"o\"gq+\"\\\"o\\\"p\"+\"\"gq+\"\"g+\"\"
p\"o\"gq\"o\"+\"+\"pgx"""p"\"+\"\"\"p""i"gq+"tg"+"i"gq+"\"i\"p\"\""+""gq+""g+""p
"""+""i"g+pgx

See also

External resources