Advertisement
Wilfred-kun

Text to Brainfuck

Mar 4th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. '''
  2. Text-to-Brainfuck Generator
  3. This program reads a textfile from stdin and
  4. writes Brainfuck to stdout
  5. The output only uses one block in a Brainfuck,
  6. which is the currently selected block. It leaves
  7. the block with value 0.
  8.  
  9. Written in Python 3.5.2
  10. '''
  11.  
  12.  
  13.  
  14.  
  15. from math import sqrt
  16. from sys import stdin, stdout
  17.  
  18.  
  19. def refactor(nr):
  20.     root = int(sqrt(nr))
  21.     rest = nr - (root**2)
  22.     return root, rest
  23.  
  24.  
  25. def main():
  26.     old = 0
  27.  
  28.     stdout.write('[-]')
  29.  
  30.     for character in stdin.read():
  31.         _ord = ord(character)
  32.            
  33.         if old == _ord:
  34.             stdout.write('.')
  35.             continue
  36.  
  37.         deviation = _ord - old
  38.  
  39.         mult = refactor(abs(deviation))
  40.  
  41.         if 0 < deviation:
  42.             if deviation < 6:
  43.                 stdout.write('{}.'.format('+'*deviation))
  44.             else:
  45.                 stdout.write('>{}[<{}>-]<{}.'.format('+'*mult[0],'+'*mult[0],'+'*mult[1]))
  46.         else:
  47.             if deviation > -6:
  48.                 stdout.write('{}.'.format('-'*abs(deviation)))
  49.             else:
  50.                 stdout.write('>{}[<{}>-]<{}.'.format('+'*mult[0],'-'*mult[0],'-'*mult[1]))
  51.        
  52.        
  53.         old = _ord
  54.  
  55.     stdout.write('[-]')
  56.     stdout.flush()
  57.  
  58.  
  59. if __name__ == '__main__':
  60.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement