def run(): args = parse_args() if args.c: code = args.file else: with open(args.file, "r") as f: code = f.read() with time_run(args.t): try: eval(code, append_newline=args.n, optimize=args.o) except ParserError as e: sys.stderr.write("%s: %s\n" % (e.__class__.__name__, e.message)) sys.stderr.flush() return 1 return 0
def eval(code, mem=Memory(), cmd_index=0): '''Compile BF Alpha code to brainfuck and run it''' result = ''.join(translate(code)) return brainfuck.eval(result, mem, cmd_index)
import argparse import brainfuck from sys import stdin, stdout, stderr, exit parser = argparse.ArgumentParser() parser.add_argument('-e', '--eval', help='eval a string of code') parser.add_argument('-f', '--file', help='execute a file') parser.add_argument('-r', '--repl', help='enter a really simple repl', action='store_true') args = parser.parse_args() if args.eval: code = brainfuck.read(args.eval) brainfuck.eval(code) elif args.file: with open(args.file, 'r') as infile: code = brainfuck.read(''.join(infile.readlines())) brainfuck.eval(code) elif args.repl: data = [0 for i in range(9999)] while True: stdout.write("bf> ") stdout.flush() try: line = stdin.readline() code = brainfuck.read(line) brainfuck.eval(code, data) except KeyboardInterrupt: exit(0)
import brainfuck from sys import stdin, stdout, stderr, exit parser = argparse.ArgumentParser() parser.add_argument('-e', '--eval', help='eval a string of code') parser.add_argument('-f', '--file', help='execute a file') parser.add_argument('-r', '--repl', help='enter a really simple repl', action='store_true') args = parser.parse_args() if args.eval: code = brainfuck.read(args.eval) brainfuck.eval(code) elif args.file: with open(args.file, 'r') as infile: code = brainfuck.read(''.join(infile.readlines())) brainfuck.eval(code) elif args.repl: data = [0 for i in range(9999)] while True: stdout.write("bf> ") stdout.flush() try: line = stdin.readline() code = brainfuck.read(line) brainfuck.eval(code, data) except KeyboardInterrupt: exit(0)
import brainfuck bf = '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.' print(brainfuck.eval(bf)) # Hello World!