YABC

From Esolang
Jump to navigation Jump to search

YABC, acronum for Yet Another Brainfuck Clone, is a brainfuck-derivative scribbled up by oklopol for testing how uploading to the esolang wiki works.

Language overview

YABC has a brainfuck-like tape, that extends infinitely to the right. Tape cells hold signed bignums. Current cell is the leftmost cell when the program starts. Code is read one character at a time, executing the character read as an instruction. Instructions:

Command Description
> Move the pointer to the right
< Move the pointer to the left
+ Increment the memory cell under the pointer
- Decrement the memory cell under the pointer
^ If the cell to the right of current cell is nonzero, take the value of the current cell and jump that many characters backwards in code

Computational class

YABC is Turing complete.

YABC from Brainfuck

The following implements a Brainfuck dialect with nonnegative integer number cells, without I/O. We assume that ^ allows negative offsets, which are relative to the ^ instruction itself. Cell values will be kept between -37 and the larger value of 41 or the 1 plus the maximum value of the Brainfuck dialect.

Translation

First, there is a preamble.

  • repeat +++++++++++++++++++++++++++++++++++++++++>-------------------------------------> for each nesting level of loops of the Brainfuck code
  • +++++++++++++++++++++++++++++++>-->------>++>++>+>+++>+>------>+++>+>------>-<^>>>^<

This is followed by the translated Brainfuck code. Each Brainfuck operation is translated to 39 YABC instructions, according to the following table, where ? stands for an arbitrary instruction that will never be executed.

Command Code
+ ><><><><><><><><><<+>^??><^^??><^^>>>^<
- ><><><><><><><><><<->^??><^^??><^^>>>^<
> ><><>++++>+>------>-<^??><^^??><^^>>>^<
< ><><>+<++++++<-<----<^??><^^??><^^>>>^<
[ <-<<>^>+<<^>>+>^><<<^^>^>>^^?<<<^^>>>^<
] ><><><><<<<<<^?>^????^<<<<^^??>>^^>>>^<

Notes

The tape layout is as follows,

41 -37 ... 41 -37 31 -2 -6 2 2 1 3 1 -6
3 ?+1 -6 ... 3 ?+1 *-6 -1 ? 0 0 ? 0 0 ...

where the first line gives fixed constants, and the second line the data area. The question marks stand for tape values from the original Brainfuck program, and * indicates the current pointer at the start of each translated instruction.

The encoding of the instructions have the following format:

code******************^left*^right^enter

Normal execution uses the code part; the -6 values on the tape are used as offsets to skip over the other parts using the fixed ^ operations.

The left part is used for skipping over loops, moving to the left. It uses the beginning of the tape as a control area; in each left block, two instructions (<<, ><, >>) are executed, followed by a ^. Normally, the pointer will point at a 41, which is the offset for skipping to previous instruction's left block. The pointer can move to the left and right, keeping track of the nesting depth. The ] operation moves the pointer to the last 41 at the beginning of the tape. When the matching [ is found, the offset is 31, jumping to the enter part of the preceding instruction.

Similarly, the right part is used for skipping over loops, moving rightwards. The [ instruction is responsible for conditionally entering its right part while pointing to the last -37 at the beginning of the tape. -37 is the offset required to jump to the next instruction's right part. When the matching ] instruction is found, the offset will be -2 and execution continues at the enter part of the ] instruction.

Finally, the enter part, which is always >>>^<, is responsible for resetting the tape position to the current cell. It uses the magic 3 and -1 values in the data area of the tape, and the 2 1 3 part of the preamble for synchronization (the code works when the pointer points to either of the 31 or the -2 offsets in the preamble). Note that the preamble contains a copy of the enter code.