<insert original title here>

Programming, Haskell, and games

cfopu, a minimalistic esoteric language similar to brainfuck and Assembly language

leave a comment »

cfopu is a primitive, minimalistic language, similar to assembly and brainfuck. It can read input from stdin, and print to stdout. When a file is interpreted, its contents are placed in memory after being processed by the preprocessor.
There are two pointers: the data pointer, and the instruction pointer, both of which initially point to the first byte. Each byte is parsed, the instruction pointer is set ahead by one byte, and then the command is executed if the byte is a command. There are only 8 primitive commands:

0: End the program.
1: Print current byte.
2: Get one character (blocking, if necessary). If EOF has been reached, the data pointer is set behind by one.
3: Set the data pointer behind by one.
4: Set the data pointer ahead by two.
5: Decrement the value pointed to by the data pointer by one.
6: If the current byte is non-zero, the data pointer is set behind by one; otherwise, the data pointer is set forward by two.
7: Sets the instruction pointer to the data pointer.

Incrementing the data pointer by two might seem arbitrary, but is necessary, because otherwise it would be impossible to set the data pointer ahead of the instruction pointer.

When 0x00 or 0x30 is read at the instruction pointer, the program is ended; and so forth. If the byte read at the instruction pointer is not recognized, it is simply ignored, and the program continues as usual.

The memory will expand as much your system will allow. Every byte is 0x00, by default. Every cell in memory is a byte, and can only have 256 values: 0x00-0xFF.

Additionally, a preprocessor exists so that common code can be factored out.
#: This symbol is place before each symbol that should not be replaced or removed (it can be placed before another hash). These symbols are not treated specially *inside* comments, particularly delimited ones.
@: Delimited macro. This is simply a preprocessor shortcut, and it can also be used for readability. The length of the delimiter is the sum of one and the predefined symbols following the symbol. So “@0000macro07macro macro” evaluates to ” 07″. Macros can also be used for better readability. If a ‘@’ symbol appears within the body of the definition of a macro, it will be treated like any other non-command symbol. (The preprocessor removes the macro definitions and replaces the instances of the them with the body of them). Comments are removed by the preprocessor before macros are parsed. Macros are parsed in one pass entirely by the preprocessor before replacement actually takes place, so the relative positioning of the macros from the rest of the source doesn’t affect the functionality of the program, although the order of macro definitions themselves does matter, since a macro can appear in the definition of another one that is defined later in the program.
8: This begins a delimited comment. The end delimiter can be elided at the end of a file.
9: This is the other type of comments. When this is read by the preprocessor, any whitespace preceding the ‘9’ is removed as well as the rest of the line, but not including the newline character, which may be omitted, in which case the rest of the file is removed by the preprocessor.

After macros are parsed by the preprocessor, all unknown characters are stripped out by the preprocessor *until* the special character sequence @@ is encountered. The @@ is removed by the preprocessor and then it isn’t treated specially anymore, and then characters that would not be recognized by the instruction pointer are no longer stripped out.

Any file can be interpreted or compiled as cfopu source; but, of course, not all cfopu programs will run as intended. They can run indefinitely, or maliciously use up many resources.

A cfopu interpreter exists in a cabal package in the cabal package repository, known as Hackage; its name is cfopu. It was created primarily as a Haskell exercise.

The interpreter interpretively interprets interpretable cfopu source, the filename of which is passed on the command line as the first argument. Every other argument is optional. If the second argument is given, it is treated as a filename and the preprocessed version of the source is written to it. Since preprocessed cfopu source is often long and quite unreadable, a third argument can be passed, which specifies the filename of a file to which a more readable version of the preprocessed version of the source will be written. Long lines (not in macro definitions, or in other places in which the functionality of the program would be affected) are wrapped, and a few of some very common patterns are replaced with macros.

The main source file of the cfopu interpreter is actually itself a cfopu program. Assuming that the first number is greater than the second, it takes two single-digit integers from stdin, and prints the difference.

Advertisements

Written by bairyn

July 10, 2010 at 21:57

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: