<commit_before>#===============================================================================
# PyFudge: A Python-based Brainfuck Interpreter
#-------------------------------------------------------------------------------
# Version: 0.1.3
# Updated: 07-11-2013
# Author: <NAME>
# License: MIT
#===============================================================================

'''PyFudge is a Python-based (2.7.5) Brainfuck interpreter.

I believe it's fully functional, but haven't tested it thoroughly yet.

'''

#===============================================================================
# IMPORTS
#===============================================================================

from __future__ import print_function

#===============================================================================
# MEMORY VARIABLES
#===============================================================================

MEMSIZE = 30000
BYTESIZE = 255

MEMORY = [0] * MEMSIZE

#===============================================================================
# PYFUDGE MAIN FUNCTION
#===============================================================================

def run(program, wrap=True):
    '''pyfudge.run(program, wrap=True)

    Interprets a Brainfuck program fed into it.

    '''

    pointer = 0
    progpos = 0

    program = program.replace(" ", "")

    outbuff = []

    while progpos < len(program):

        char = program[progpos]

        if char is '+':
            MEMORY[pointer] += 1
        elif char is '-':
            MEMORY[pointer] -= 1
        elif char is '>':
            pointer += 1
        elif char is '<':
            pointer -= 1
        elif char is '.':
            if MEMORY[pointer] == 10:
                outbuff.extend('\n')
            else:
                outbuff.extend(chr(MEMORY[pointer]))
        elif char is ',':
            MEMORY[pointer] = ord(raw_input())
        elif char is '[' and MEMORY[pointer] == 0:
            loops = 1
            while loops > 0:
                progpos += 1
                if program[progpos] is '[':
                    loops += 1
                elif program[progpos] is ']':
                    loops -= 1
        elif char is ']' and MEMORY[pointer] != 0:
            loops = 1
            while loops > 0:
                progpos -= 1
                if program[progpos] is '[':
                    loops -= 1
                elif program[progpos] is ']':
                    loops += 1
        elif char is '#':
            print(MEMORY[pointer])

        if wrap:
            if MEMORY[pointer] > BYTESIZE:
                MEMORY[pointer] = 0
            elif MEMORY[pointer] < 0:
                MEMORY[pointer] = BYTESIZE

        progpos += 1

    return "".join(outbuff)<commit_msg>Now uses a list for keeping track of loop jump points.<commit_after>#===============================================================================
# PyFudge: A Python-based Brainfuck Interpreter
#-------------------------------------------------------------------------------
# Version: 0.1.4
# Updated: 08-11-2013
# Author: <NAME>
# License: MIT
# Demo: http://repl.it/MQz/1
#===============================================================================

'''PyFudge is a Python-based (2.7.5) Brainfuck interpreter.

I believe it's fully functional, but haven't tested it thoroughly yet.

'''

#===============================================================================
# IMPORTS
#===============================================================================

from __future__ import print_function

#===============================================================================
# MEMORY VARIABLES
#===============================================================================

MEMSIZE = 'Auto'
CELLSIZE = 255
MEMORY = [0]

#===============================================================================
# PYFUDGE FUNCTIONS
#===============================================================================

def run(program, wrap=True, binary=False, debug=False):
    '''pyfudge.run(program, wrap=True, binary=False, debug=False)

    Interprets a Brainfuck program fed into it.

    '''

    mempos = 0
    progpos = 0

    outbuff = []

    program = program.replace(" ", "")
    proglen = len(program)

    looplst = findloops(program, proglen)
    looplen = program.count('[')

    if 'ERROR' in looplst[0]:
        return looplst

    while progpos < proglen:

        char = program[progpos]

        if char is '+':
            MEMORY[mempos] += 1
        elif char is '-':
            MEMORY[mempos] -= 1
        elif char is '>':
            mempos += 1
        elif char is '<':
            mempos -= 1
        elif char is '.':
            if not binary and MEMORY[mempos] == 10:
                outbuff.extend('\n')
            else:
                outbuff.extend(chr(MEMORY[mempos]))
        elif char is ',':
            cmd = raw_input()
            if cmd is not '':
                MEMORY[mempos] = ord(cmd)
            else:
                MEMORY[mempos] = 0
        elif char is '[':
            if MEMORY[mempos] == 0:
                for i in range(looplen):
                    if progpos == looplst[i][0]:
                        progpos = looplst[i][1]
        elif char is ']':
            if MEMORY[mempos] != 0:
                for i in range(looplen):
                    if progpos == looplst[i][1]:
                        progpos = looplst[i][0]
        elif char is '#' and debug:
            print(MEMORY[mempos])

        if MEMSIZE is 'Auto':
            try:
                test = MEMORY[mempos]
            except:
                MEMORY.extend([0])
        else:
            try:
                test = MEMORY[mempos]
            except:
                return ['ERROR: Not enough memory.']

        if wrap:
            if MEMORY[mempos] > CELLSIZE:
                MEMORY[mempos] = 0
            elif MEMORY[mempos] < 0:
                MEMORY[mempos] = CELLSIZE

        progpos += 1

    return "".join(outbuff), MEMORY 

def findloops(program, proglen):
    '''findloops(program, proglen) -> list of loop jump points'''

    temp = []
    looplst = []

    if program.count('[') == program.count(']'):
        for i in range(proglen):
            if program[i] is '[':
                temp.append(i)
            elif program[i] is ']':
                index = temp.pop()
                looplst.append([index, i])
        return looplst
    else:
        return ['ERROR: Mismatched number of loop brackets.']

def setmemory(memsize='Auto', cellsize=None):
    '''setmemory(memsize='Auto', cellsize=None)

    Sets the memory, and cell size to the specified sizes.

    '''

    global MEMSIZE
    global CELLSIZE
    global MEMORY

    if memsize is 'Auto':
        MEMSIZE = 'Auto'
    else:
        MEMSIZE = memsize
        MEMORY = [0] * MEMSIZE
        
    if cellsize is not None:
        CELLSIZE = cellsize

{'fraction_non_alphanumeric': 0.2985311059907834, 'fraction_numerical': 0.01238479262672811, 'mean_word_length': 2.0301047120418847, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2670995', 'n_tokens_mistral': 1958, 'n_tokens_neox': 1718, 'n_words': 470}