Enema

From Esolang
Jump to: navigation, search

Enema is an esoteric programming language in which most instructions can be redefined. It was inspired by the Unefunge language (one-dimensional version of Befunge).

Language overview

Programs in Enema consist of sequence of single-character instructions. Execution starts from the first instruction and continues until error occurrs (i.e. undefined word is executed). When execution reaches end of program it wraps back to the beginning. Program memory is separated from data memory, so programs cannot modify themselves in other ways than redefinition of instructions.

All operations are performed on stack. Stack items have size of single machine word (typically 32 bits).

Program has access to data memory. It consists of cells of the same size as stack items. Memory addressing is done modulo its size, i.e. cell with address 7 is the same as cell memory_size+7 or -7.

Instruction set

Enema has two categories of keywords - regular keywords and special constructs. Special constructs cannot be redefined and are treated more or less like preprocessor directives in C/C++. Regular keywords perform actual operations and can be redefined using : operator.

Special constructs

Command Description
{ Start of comment. All characters until matching } will be ignored. Note however

that standalone } character (without matching {) is just a regular keyword and can be used in program.

` Characters until next ` will be treated as path to file to be included. After

inclusion path (including both ` characters) will be replaced by contents of this file.

\n \r Ignored

Regular keywords

Command Description
0-9 Push this value on the stack
+ Add two top stack values
- Subtract two top stack values
* Multiply two top stack values
/ Division
% Modulo division
& Logical AND
| Logical OR
^ Logical XOR
I Input single character from standard input and put it on stack
O Take single value from top of stack and output it as a single ASCII character to standard output
X Drop topmost stack item
D Duplicate topmost stack item
S Swap two topmost stack items
R Rotate three topmost stack items
G Take topmost stack value, treat it as address in memory and push contents of this memory cell on stack
P Take topmost stack value and write it to memory address specified in second stack value
[ Start infinite loop block
] End infinite loop block and jump to matching [
B Break out of loop and jump to character following the next unmatched ]
Z Skip next instruction if topmost stack value is greater than 0
: Define or redefine word. Word to be (re)defined is the character following :. Body of defined

word are characters following until the next :. Execution will continue from word after : closing definition. Sample: :12Q: will redefine 1 to push value 2 on stack instead of default 1.

! Forget definition of keyword following the !. If forgotten keyword is one of default Enema

keywords it will return to default behaviour.

Q Return from redefined word. This instruction should end all word definitions. Otherwise after such word

is called execution will fall through : ending definition, which will be interpreted as start of another keyword definition.

" Push on stack ASCII codes of following characters until the next ". After that last character

of the string will be on top of the stack.

? Push current number of stack values on stack
# Push memory size on stack

See also

  • Emmental, another stack-based, instruction-rewriting esolang

External resources