BrainCursion

From Esolang
Jump to: navigation, 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.

Commands

[ 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
    command

Computational class

You can emulate a BF while loop like so:

(#@SomeCode%)

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 += "+>>+<)<-#>>#@-<<"
         loops.append((len(code),s))
      elif op == ']':
         (i,b) = loops.pop()
         s += 1
         code = code[:i] + '+'*s + "<#%>" + '-'*s + code[i:] + '+'*b + "<%)<-#>>#@-<<"
   return code.replace("><","")

External resources