<commit_before>#!/usr/bin/env python

# This is the class that contains all the basic methods for the Diddy VM
#
# This machine operates entirely in RAM
# There are registers for special uses,
#   Input
#   Output
#   Exiting
# Instructions can have a variable number of arguments
# The machines memory width is 16 bits

import sys

class VMBase(object):

    def __init__(self):
        self.ram = dict.fromkeys(xrange(2 ** 12), 0)
        self.instructions = {}

        self.position = 100

        self.registers = {}
        r = self.registers
        r[0] = self.exit
        r[1] = self.output

        self.running = True
        self.status = 0

    # General purpose functions
    def executeNextInstruction(self):
        """
        Retrieves the value from the memory location pointed to
        by the position pointer and executes it
        """
        inst = self.ram[self.position]
        self.instructions[inst]()


    def setInstructionPointer(self, address):
        """
        Sets the instruction pointer to the specified address
        """
        self.position = address

    def next(self):
        self.position += 1

    def getMem(self, addr=-1):
        if addr == -1:
            output = self.ram[self.position]
        else:
            output = self.ram[addr]
        return output

    def setMem(self, addr, value):
        if addr in self.registers:
            self.registers[addr](value)
        else:
            self.ram[addr] = value

    def exit(self, value):
        """
        Set the running flag to false so the external program loop stops
        Also sets the exit status of the VM
        """
        self.running = False
        self.status = value

    def output(self, c):
        """
        Print a char to stdout
        """
        sys.stdout.write(chr(c))


    def loadProgram(self, filename):
        fp = open(filename)
        for line in fp:
            value = int(line)
            self.setMem(self.position, value)<commit_msg>Adding docstrings to the VMBase class<commit_after>#!/usr/bin/env python

# This is the class that contains all the basic methods for the Diddy VM
#
# This machine operates entirely in RAM
# There are registers for special uses,
#   Input
#   Output
#   Exiting
# Instructions can have a variable number of arguments
# The machines memory width is 16 bits

import sys

class VMBase(object):

    """
    The base VM class
    Contains the internal data structures as well as general purpose methods

    A VM Class with the instruction methods should inherit this
    """

    def __init__(self):
        """
        Setup the ram, instruction dictionary and register dictionary
        """
        self.ram = dict.fromkeys(xrange(2 ** 12), 0)
        self.instructions = {}

        self.position = 100

        self.registers = {}
        r = self.registers
        r[0] = self.exit
        r[1] = self.output

        self.running = True
        self.status = 0

    # General purpose functions
    def executeNextInstruction(self):
        """
        Retrieves the value from the memory location pointed to
        by the position pointer and executes it
        """
        inst = self.ram[self.position]
        self.instructions[inst]()


    def setInstructionPointer(self, address):
        """
        Sets the instruction pointer to the specified address
        """
        self.position = address

    def next(self):
        """
        Increments program position counter
        """
        self.position += 1

    def getMem(self, addr=-1):
        """
        Retrieve a value from a memory address
        If no address is given then the value from the
        position pointer will be retrieved
        """
        if addr == -1:
            output = self.ram[self.position]
        else:
            output = self.ram[addr]
        return output

    def setMem(self, addr, value):
        """
        Set a value in memory
        If the memory position corresponds to a register then
        the register function will be called instead
        """
        if addr in self.registers:
            self.registers[addr](value)
        else:
            self.ram[addr] = value

    def exit(self, value):
        """
        Set the running flag to false so the external program loop stops
        Also sets the exit status of the VM
        """
        self.running = False
        self.status = value

    def output(self, c):
        """
        Print a char to stdout
        """
        sys.stdout.write(chr(c))


    def loadProgram(self, filename):
        """
        Loads a program into memory
        """
        fp = open(filename)
        for line in fp:
            value = int(line)
            self.setMem(self.position, value)
{'fraction_non_alphanumeric': 0.08296576349506406, 'fraction_numerical': 0.00630119722747322, 'mean_word_length': 1.8808227465214762, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1810142', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1229, 'n_words': 459}