2th

From Esolang
Jump to navigation Jump to search

2th (pronounced "tooth") is a brainfuck variant. It is meant to add some apparent "improvements" to brainfuck that don't actually help very much. 2th is like brainfuck, except it has an added mode and an added register called "R" which is initially set to 0, there are 2 additional commands: % and ^ which change the mode, which changes what the other commands do (see the table below). There is cell mode (the default) and register mode (new). In addition, the run length encoding of all instruction character types is allowed in 2th.

The file extension for 2th files is, ostensibly, .2th.

Benefits

Compared to the original, there are a few advantages that 2th has:

  1. The run length encoding reduces program length and increases readability.
  2. The R register then is useful for temporary values too, which also reduces program length.

Instructions

Instructions table
Instruction character Effect in cell mode Effect in register mode
% none switch to cell mode and replace the value of R with the value of the current cell
^ switch to register mode and replace the value of R with the value of the current cell none
> move right in memory move right in memory and load the value of the current cell into R
< move left in memory move left in memory and load the value of the current cell into R
+ increment the current cell increment R
- decrement the current cell decrement R
? read input into the current cell read input into R
. write out the value the current cell write out the value of R
[ loop begin based on the value of the current cell loop begin based on the value of R
] loop end based on the value of the current cell loop end based on the value of R

Run length encoding

To elaborate on the run-length encoding, any decimal integer > 0 is allowed in front of a command character. The decimal number must be series of consecutive decimal digits, so 99 4+ is equivalent to ++++ and not 994 pluses. At least one of the digits must not be a '0', otherwise, the whole number counts as a '1'. That is, 0+ and 0000+ are both equivalent to 1+, which is equivalent to +, and 03+ is equivalent to +++. There must be no other characters (not even whitespace) between the number and the command character for the number to have the effect of multiplying said command character. That is, 4 + is equivalent to + and not ++++.

Examples

"Hello world.2th" (this does not switch modes):

>8+[<9+>-]<.>4+[<7+>-]<+.7+..3+.>>6+[<7+>-]<++.12-.>6+[<9+>-]<+.<.3+.6-.8-.3>4+<8+>-]<+.

Implemenations