def simulate(self, max_time):
        # TODO: here you can change the number of register you want to use
        # Prepare the environment for the simulation
        # 3 registers and 256 of memory
        environment = Environment(self.MAX_REG, 256, self.root.labels) 

        start_time = time.time()

        current_instruction = 0
        current_line = self.root[current_instruction].line

        try:
            # Execute the simulation
            while True:
                # While the time has not been exceeded
                elapsed_time = time.time() - start_time
                if elapsed_time > max_time:
                    raise SimulationError("Maximum time allowed for the simulation exceeded!")

                # Check if the simulation is over
                if current_instruction >= len(self.root) - 1:
                    break

                # Do the instruction logic
                current_instruction = visit_parse_tree(self.root[current_instruction], SimulatorVisitor(environment))
                current_line = self.root[current_instruction].line

        # Add the line who crashed so it's easier to debug
        except SimulationError as error:
            message, = error.args
            raise SimulationError(message + " occurred at line " + str(current_line))

        # Print the finale results
        environment.print()
Exemplo n.º 2
0
    def fetch_variable(self, variable, can_create_variable=False):
        # Try to get the memory address

        if not (variable[0].isalpha() and variable.isalnum()):
            raise SimulationError("Variable «" + variable + "» is not a valid name.")

        # Manage the variable creation if needed
        if variable not in self.__variables:
            if not can_create_variable:
                raise SimulationError("The variable «" + variable + "» cannot be found!")
            elif len(self.__variables) == self.__memory_size:
                raise SimulationError("The maximum number of variables has been reached!")
            self.__variables[variable] = len(self.__variables)

        return self.__variables[variable]
Exemplo n.º 3
0
    def visit_instruction(self, node, children):
        # The child of an instruction is a label and an operation
        # The only result from childs are next operations
        next_instruction = node.instruction + 1
        if len(children) == 1:
            next_instruction = children[-1]
        elif len(children) > 1:
            raise SimulationError("Unexpected number of children")

        return next_instruction
 def fetch_label_mapping(self, label):
     if label not in self.__labels:
         raise SimulationError("«" + label +
                               "» has not been found in the program")
     return self.__labels[label]
 def fetch_memory(self, address):
     if address < 0 or address >= self.__memory_size:
         raise SimulationError("«R" + str(address) + "» is out of bound")
     return self.__memory[address]
 def fetch_register(self, index):
     if index < 0 or index >= self.__nb_registers:
         raise SimulationError("«R" + str(index) + "» is out of bound")
     return self.__registers[index]