From Esolang
Jump to navigation Jump to search

BrainCursion is a variant of the brainfuck programming language in which the [] while loops are replaced with recursive loops. It was sketched up in a few seconds by Seven Inch Bread, with no intention of being implemented. However, an interpreter was written by User:Madk in 2010.


[ and ] are both removed from the language, with the following commands added

( - marks the beginning of a loop
) - marks the end
% - return back to the last ( command 
   (if there is none, then restart at the beginning of the program)
@ - move to the next ) command 
   (if there is none, the program terminates)
# - If the value of the current cell is non-zero, skip the next

Computational class

You can emulate a BF while loop like so:


However, as loops cannot be nested, this is not enough to show BrainCursion Turing-complete. Note, though, that BrainCursion loops are more featureful as Brainfuck loops, as you can write

(#@ aaa #@ bbb #@ ccc #@ ddd %)

to conditionally skip out of the loop at different spots. (Other constructions are possible, too.)

The following Python code converts any Brainfuck program into an equivalent BrainCursion program, provided that cells can hold values up to 2 times the number of loops in the Brainfuck program. Note that ( is not required.

def bf2bc(bf):
   """Convert Brainfuck code to BrainCursion"""
   loops = []
   s = 0
   code = ">>>+<<#>>#@-<<"
   for op in bf:
      if op in "+-,.":
         code += "<" + op + ">"
      elif op in "><":
         code += op*4
      elif op == '[':
         s += 1
         code += "+>>+<)<-#>>#@-<<"
      elif op == ']':
         (i,b) = loops.pop()
         s += 1
         code = code[:i] + '+'*s + "<#%>" + '-'*s + code[i:] + '+'*b + "<%)<-#>>#@-<<"
   return code.replace("><","")

External resources