Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Text-to-Brainfuck Generator
- This program reads a textfile from stdin and
- writes Brainfuck to stdout
- The output only uses one block in a Brainfuck,
- which is the currently selected block. It leaves
- the block with value 0.
- Written in Python 3.5.2
- '''
- from math import sqrt
- from sys import stdin, stdout
- def refactor(nr):
- root = int(sqrt(nr))
- rest = nr - (root**2)
- return root, rest
- def main():
- old = 0
- stdout.write('[-]')
- for character in stdin.read():
- _ord = ord(character)
- if old == _ord:
- stdout.write('.')
- continue
- deviation = _ord - old
- mult = refactor(abs(deviation))
- if 0 < deviation:
- if deviation < 6:
- stdout.write('{}.'.format('+'*deviation))
- else:
- stdout.write('>{}[<{}>-]<{}.'.format('+'*mult[0],'+'*mult[0],'+'*mult[1]))
- else:
- if deviation > -6:
- stdout.write('{}.'.format('-'*abs(deviation)))
- else:
- stdout.write('>{}[<{}>-]<{}.'.format('+'*mult[0],'-'*mult[0],'-'*mult[1]))
- old = _ord
- stdout.write('[-]')
- stdout.flush()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement