From Esolang
Jump to: navigation, search

Swap is a language based on string rewriting. It was created by User:revcompgeek in August 2009.


When the program is run, the first character is removed. If it is not a ~ or a \ it is output. When a \ is removed the next character is removed and output. When a ~ is encountered, a construct is read like this:


Escaped characters are treated as themselves in string1 and string2, then instances of string1 and string2 are then swapped in the program code. Since swapping nothing with nothing is useless, if both string1 and string2 are empty then text is read up to the next ~, and then the text is replaced with a character read from stdin. Example: in this

~Hello~World~World Hello!

~Hello~World~ is removed from the beginning, leaving this:

World Hello!

Then the two strings Hello and World are swapped in the text. Every time Hello appears, it is replaced with World and vice versa. The code then becomes this:

Hello World!

Which is then sent to stdout.

Since if string1 and string2 are equal swapping is useless, the code is scanned for the string. If only 1 instance is found then the left and right halves of the code are swapped around the found string. Example:

~|~|~ 123|456



If two instances are found then the code is swapped like 12|34|56 -> 56|34|12. And if 3 instances are found then the code is swapped like this: 1|23|45|6 -> 1|45|23|6. Right now this is as far as the swapping construct goes, but more may be added if it can be shown to be useful.

When instances of string1 and string2 overlap, the one that occurs first takes precedence, and if they start on the same character string1 takes precedence. Example:

~123~12312~ 12312




Hello World!

Hello World!

Or a slightly more complicated version

~World~Hello~~!~?~World Hello?


The official interpreter is here. It is written in D and uses the Tango standard library. I have been unable to find any bugs so far in the current revision, but if any are found I can be contacted either through GitHub (preferred because of email notifications) or this wiki (which I may not check often).


~ was chosen due to it's similarity to the transpose revision symbol.