#!/usr/bin/env python

'''
Brainy: A brainfuck interpreter/repl written in python
    Copyright (C) 2012  Joel Buchheim-Moore

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

import sys
from brainy import bfinter
bf = bfinter.Brainy()

if len(sys.argv) > 1:
    try: f = open(sys.argv[1])
    except:
        print "File not found"
        quit()

    code = f.read()
    f.close()
    bf.eval(code)
    quit()

print "Welcome to the Brainy REPL"
print "For help type 'h'. To quit just type 'q' or press ctrl+d\n"
print '''Brainy  Copyright (C) 2012  Joel Buchheim-Moore
This program comes with ABSOLUTELY NO WARRANTY; for details type `w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `c' for details.
'''
user_in = ''
tape_start = 0
tape_end = 10

while True:
    try: user_in = raw_input(">>> ")
    except (EOFError, KeyboardInterrupt):
        print "\nThank you, bye!"
        break

    if user_in == 'q':
        print "Thank you, bye!"
        break

    elif user_in == 'w':
        print '''This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
        '''

    elif user_in == 'c':
        print '''This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
        '''

    elif user_in == 'st':
        tape_start = input('Set beginning cell: ')
        tape_end = input('Set ending cell: ')

    elif user_in == 'h':
        print 'st: allows you to change your view of the tape'
        print 'q: quit the repl'

    else:
        bf.eval(user_in)
        print bf.get_tape(tape_start, tape_end)
