Swapper
Swapper is a simple self-modifying reversible language invented by User:Tailcalled in February of 2012.
Syntax
Swapper programs consist of a list of statements separated by semicolons:
program = statement [';' program] statement = <atom> '<->' <atom> statement = '(' <#> ')' ('<+->' | '<*/>') '[' <#> ']' statement = '<rev>' statement = '<prtmd>'
where <atom> is a string of characters in the groups A-Z, a-z, or 0-9, and <#> is just in the group 0-9.
Programming
Integers
Integer literals are created by putting an integer in parentheses.
(42)
Constant integer literals are created with brackets instead. These have the property of remaining unchanged during a pseudo-operation.
[42]
Operators
Operators are enclosed in angle brackets and self-modify the program when executed. They cannot be modified themselves.
Syntax | Action |
---|---|
A <-> B; |
swaps all occurrences of A and B, where both are alphanumeric patterns delimited by ; and <->. Invalid characters (spaces, newlines, symbols, etc.) are skipped in the pattern. Text inside [] or <> remains unchanged during the swap. |
(x) <+-> [y]; |
replaces (x) with (x+y) if the program flows forward, and (x-y) if backward. |
(x) <*/> [y]; |
replaces (x) with (x*y) if the program flows forward, and (x/y) if backward, rounding down. |
<rev>; |
reverses program flow. |
<prtmd>; |
toggles "print mode". In this mode, any number that <-> 's with out is printed to STDOUT.
|
Additional operators can be added at the programmer's discretion. User:Tailcalled encourages this notion to add power to the language while maintaining its reversibility.
Sample Programs
Operator Demo
This program prints 1 to STDOUT.
i <-> (0); i <+-> [1]; <prtmd>; out <-> i; <prtmd>;
Step by step:
1. All i
s are swapped with (0)
:
(0) <-> i; (0) <+-> [1]; <prtmd>; out <-> (0); <prtmd>;
2. All (0)
s are incremented.
(1) <-> i; (1) <+-> [1]; <prtmd>; out <-> (1); <prtmd>;
3. Enter print mode.
4. 1 printed because it is in print mode.
5. Exit print mode.
Print the ASCII #s of Hello, world!
<prtmd>; out <-> (72); out <-> (101); out <-> (108); out <-> (108); out <-> (111); out <-> (44); out <-> (32); out <-> (119); out <-> (111); out <-> (114); out <-> (108); out <-> (100); out <-> (33); out <-> (10);