def check_path_exists_BFS(G, u, v): stack = Stack() stack.push(u) G.nodes[u]['weight'] = 1 while not stack.isEmpty(): current = stack.pop() for n in G.neighbors(current.data): if n == v: return True if G.nodes[n]['weight'] == 0: stack.push(n) G.nodes[n]['weight'] = 1 return False
def executeQuadruples(self): instructionPointer = 0 valuesStack = Stack() functionsStack = Stack() savedIPs = Stack() speed = 8 while instructionPointer < self.quadQueue.size(): quadLocation = self.quadQueue.size() - instructionPointer - 1 quad = self.quadQueue.get(quadLocation) leftOperand = quad.left_operand rightOperand = quad.right_operand resultAddress = quad.result if isinstance(quad.left_operand, list): if len(quad.left_operand) == 1: leftOperand = self.memory.getValueByAddress(quad.left_operand[0]) if isinstance(quad.right_operand, list): if len(quad.right_operand) == 1: rightOperand = self.memory.getValueByAddress(quad.right_operand[0]) if isinstance(quad.result, list): if len(quad.result) == 1: resultAddress = self.memory.getValueByAddress(quad.result[0]) if quad.operator == '+': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) #print ('Left Operand Value', leftOperandValue) #print ('Right Operand Value', rightOperandValue) resultValue = leftOperandValue + rightOperandValue #print ('Result Value', resultValue, ' on address ', resultAddress) self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '-': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) #print ('Left Operand Value', leftOperandValue) #print ('Right Operand Value', rightOperandValue) resultValue = leftOperandValue - rightOperandValue #print ('Result Value', resultValue, ' on address ', resultAddress) self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '*': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) #print ('Left Operand Value', leftOperandValue) #print ('Right Operand Value', rightOperandValue) resultValue = leftOperandValue * rightOperandValue #print ('Result Value', resultValue, ' on address ', resultAddress) self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '/': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) #print ('Left Operand Value', leftOperandValue) #print ('Right Operand Value', rightOperandValue) resultValue = leftOperandValue / rightOperandValue #print ('Result Value', resultValue, ' on address ', resultAddress) self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '!': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) #print ('Left Operand Value', leftOperandValue) result = not(leftOperandValue) #print ('Result Value', result, ' on address ', resultAddress) self.memory.modifyValueByAddress(resultAddress, result) elif quad.operator == '=': # print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) #print ('Left Operand Value', leftOperandValue) #print ('Result Value', leftOperandValue, ' on address', resultAddress) self.memory.modifyValueByAddress(resultAddress, leftOperandValue) elif quad.operator == '<': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) #print ('Left Operand Value', leftOperandValue) #print ('Right Operand Value', rightOperandValue) resultValue = leftOperandValue < rightOperandValue #print ('Result Value', resultValue, ' on address ', resultAddress) self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '>': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) #print ('Left Operand Value', leftOperandValue) #print ('Right Operand Value', rightOperandValue) resultValue = leftOperandValue > rightOperandValue #print ('Result Value', resultValue, ' on address ', resultAddress) self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '<=': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) #print ('Left Operand Value', leftOperandValue) # print ('Right Operand Value', rightOperandValue) resultValue = leftOperandValue <= rightOperandValue # print ('Result Value', resultValue, ' on address ', resultAddress) self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '>=': # print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) # print ('Left Operand Value', leftOperandValue) # print ('Right Operand Value', rightOperandValue) resultValue = leftOperandValue >= rightOperandValue # print ('Result Value', resultValue, ' on address ', resultAddress) self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '==': # print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) #print ('Left Operand Value', leftOperandValue) #print ('Right Operand Value', rightOperandValue) resultValue = leftOperandValue == rightOperandValue #print ('Result Value', resultValue, ' on address ', resultAddress) self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '||': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) #print ('Left Operand Value', leftOperandValue) #print ('Right Operand Value', rightOperandValue) resultValue = leftOperandValue or rightOperandValue #print ('Result Value', resultValue, ' on address ', resultAddress) self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '&&': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) #print ('Left Operand Value', leftOperandValue) #print ('Right Operand Value', rightOperandValue) resultValue = leftOperandValue and rightOperandValue #print ('Result Value', resultValue, ' on address ', resultAddress) self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '!=': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) #print ('Left Operand Value', leftOperandValue) #print ('Right Operand Value', rightOperandValue) resultValue = leftOperandValue != rightOperandValue #print ('Result Value', resultValue, ' on address ', resultAddress) self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == 'WRITE': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) #print ('Left Operand Value', leftOperandValue) print leftOperandValue elif quad.operator == 'READ': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) input = raw_input() if self.isInt(input): if leftOperand == 'int': self.memory.modifyValueByAddress(resultAddress, int(input)) else: print('Error: Input type mismatch, expecting ' + str(leftOperand)) sys.exit() elif self.isFloat(input): if leftOperand == 'float': self.memory.modifyValueByAddress(resultAddress, float(input)) else: print('Error: Input type mismatch, expecting ' + str(leftOperand)) sys.exit() elif input == 'true': if leftOperand == 'bool': self.memory.modifyValueByAddress(resultAddress, True) else: print('Error: Input type mismatch, expecting ' + str(leftOperand)) sys.exit() elif input == 'false': if leftOperand == 'bool': self.memory.modifyValueByAddress(resultAddress, False) else: print('Error: Input type mismatch, expecting ' + str(leftOperand)) sys.exit() else: if leftOperand == 'string': self.memory.modifyValueByAddress(resultAddress, str(input)) else: print('Error: Input type mismatch, expecting ' + str(leftOperand)) sys.exit() elif quad.operator == 'goto': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) instructionPointer = resultAddress - 2 elif quad.operator == 'gotof': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) if leftOperandValue == False: instructionPointer = resultAddress - 2 elif quad.operator == 'gosub': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) savedIPs.push(quad.quad_number) instructionPointer = resultAddress - 2 elif quad.operator == 'RETURN': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) #print ('leftoperand',self.memory.getValueByAddress(resultAddress)) self.memory.modifyValueByAddress(rightOperand, leftOperandValue) instructionPointer = resultAddress - 2 #print ('resultAddress',self.memory.getValueByAddress(resultAddress)) elif quad.operator == 'ENDPROC': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) if not functionsStack.isEmpty(): functionName = functionsStack.pop() function = self.functionsDirectory.functions[functionName] varTable = function['variables'] variables = varTable.variables for variable in variables: variableInfo = variables[variable] variableVirtualAddress = variableInfo[1] self.memory.modifyValueByAddress(variableVirtualAddress, valuesStack.pop()) instructionPointer = savedIPs.pop() - 1 elif quad.operator == 'ERA': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, #quad.result) function = self.functionsDirectory.functions[leftOperand] varTable = function['variables'] variables = varTable.variables for variable in variables: variableInfo = variables[variable] variableVirtualAddress = variableInfo[1] #print('return variableVirtualAddress ', variableVirtualAddress) variableValue = self.memory.getValueByAddress(variableVirtualAddress) #print('return variableValue ', variableValue) valuesStack.push(variableValue) #print('valuesStack ', valuesStack.items) functionsStack.push(leftOperand) elif quad.operator == 'VER': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) index = self.memory.getValueByAddress(leftOperand) #print ('index', index) #print ('inf lim', rightOperand) #print ('sup lim', resultAddress) if not (index >= rightOperand and index <= resultAddress): print("ERROR: Array index is out of bounds.") sys.exit() elif quad.operator == 'PARAM': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) leftOperandValue = self.memory.getValueByAddress(leftOperand) #print ('Left Operand Value', leftOperandValue) self.memory.modifyValueByAddress(resultAddress, leftOperandValue) #self.memory.modifyValueByAddress(resultAddress, result) elif quad.operator == 'DRAWLINE': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) color = rightOperand.lower() self.pen.pencolor(color) self.pen.speed(speed) self.pen.up() self.pen.setposition(self.memory.getValueByAddress(leftOperand[0]), self.memory.getValueByAddress(leftOperand[1])) #self.pen.speed(1) self.pen.down() self.pen.setposition(self.memory.getValueByAddress(leftOperand[2]), self.memory.getValueByAddress(leftOperand[3])) elif quad.operator == 'DRAWCIRCLE': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) #print ('Right Operand a', rightOperand) color = rightOperand.lower() self.pen.pencolor(color) self.pen.speed(speed) self.pen.up() self.pen.setposition(self.memory.getValueByAddress(leftOperand[0]), self.memory.getValueByAddress(leftOperand[1])) if self.memory.getValueByAddress(leftOperand[3]) == True: self.pen.color(color) self.pen.begin_fill() #self.pen.speed(1) self.pen.down() self.pen.circle(self.memory.getValueByAddress(leftOperand[2])) if self.memory.getValueByAddress(leftOperand[3]) == True: self.pen.end_fill() elif quad.operator == 'DRAWSQUARE': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) color = rightOperand.lower() sidelength = self.memory.getValueByAddress(leftOperand[2]) self.pen.pencolor(color) self.pen.speed(speed) self.pen.up() self.pen.setposition(self.memory.getValueByAddress(leftOperand[0]), self.memory.getValueByAddress(leftOperand[1])) if self.memory.getValueByAddress(leftOperand[3]) == True: self.pen.color(color) self.pen.begin_fill() #self.pen.speed(1) self.pen.down() for i in range(0, 4): self.pen.forward(sidelength) self.pen.left(90) if self.memory.getValueByAddress(leftOperand[3]) == True: self.pen.end_fill() elif quad.operator == 'DRAWTRIANGLE': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) color = rightOperand.lower() sidelength = self.memory.getValueByAddress(leftOperand[2]) if self.memory.getValueByAddress(leftOperand[4]) != 1 and self.memory.getValueByAddress(leftOperand[4]) != 2: print("ERROR: Parameter must be 1 or 2.") sys.exit() self.pen.pencolor(color) self.pen.speed(speed) self.pen.up() self.pen.setposition(self.memory.getValueByAddress(leftOperand[0]), self.memory.getValueByAddress(leftOperand[1])) if self.memory.getValueByAddress(leftOperand[3]) == True: self.pen.color(color) self.pen.begin_fill() #self.pen.speed(1) self.pen.down() for i in range(0, 3): self.pen.forward(sidelength) if self.memory.getValueByAddress(leftOperand[4]) != 1: self.pen.left(120) if self.memory.getValueByAddress(leftOperand[4]) != 2: self.pen.right(120) if self.memory.getValueByAddress(leftOperand[3]) == True: self.pen.end_fill() elif quad.operator == 'DRAWPOLYGON': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand, # quad.result) sides = self.memory.getValueByAddress(leftOperand[2]) if not (sides >= 5 and sides <= 10): print("ERROR: Polygon sides must be between 5 and 10.") sys.exit() else: color = rightOperand.lower() sidelength = self.memory.getValueByAddress(leftOperand[3]) self.pen.pencolor(color) self.pen.speed(speed) self.pen.up() self.pen.setposition(self.memory.getValueByAddress(leftOperand[0]), self.memory.getValueByAddress(leftOperand[1])) #self.pen.speed(1) self.pen.down() if self.memory.getValueByAddress(leftOperand[4]) == True: self.pen.color(color) self.pen.begin_fill() if sides == 5: for i in range(0, 5): self.pen.forward(sidelength) self.pen.left(72) elif sides == 6: for i in range(0, 6): self.pen.forward(sidelength) self.pen.left(60) elif sides == 7: for i in range(0, 7): self.pen.forward(sidelength) self.pen.left(51.43) elif sides == 8: for i in range(0, 8): self.pen.forward(sidelength) self.pen.left(45) elif sides == 9: for i in range(0, 9): self.pen.forward(sidelength) self.pen.left(40) elif sides == 10: for i in range(0, 10): self.pen.forward(sidelength) self.pen.left(36) if self.memory.getValueByAddress(leftOperand[4]) == True: self.pen.end_fill() elif quad.operator == 'DRAWITC': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand,quad.result) color = rightOperand.lower() self.pen.pencolor(color) self.pen.speed(speed) self.pen.up() self.pen.setposition(-300, 100) #self.pen.speed(1) self.pen.down() self.pen.setposition(-100, 100) self.pen.up() self.pen.setposition(-200, 100) self.pen.down() self.pen.setposition(-200, -100) self.pen.up() self.pen.setposition(-300, -100) self.pen.down() self.pen.setposition(-100, -100) self.pen.up() self.pen.setposition(-80, 100) self.pen.down() self.pen.setposition(120, 100) self.pen.up() self.pen.setposition(20, 100) self.pen.down() self.pen.setposition(20, -100) self.pen.up() self.pen.setposition(140, 100) self.pen.down() self.pen.setposition(340, 100) self.pen.up() self.pen.setposition(140, 100) self.pen.down() self.pen.setposition(140, -100) self.pen.setposition(340, -100) elif quad.operator == 'END': #print("Quad " + str(quad.quad_number), quad.operator, quad.left_operand, quad.right_operand,quad.result) self.pen.hideturtle() done() instructionPointer += 1
def executeQuadruples(self): #Apuntador a los quadruplos instructionPointer = 0 valuesStack = Stack() functionsStack = Stack() savedIPs = Stack() #While que recorre todos los quadruplos while instructionPointer < self.quadQueue.size(): quadLocation = self.quadQueue.size() - instructionPointer - 1 quad = self.quadQueue.get(quadLocation) leftOperand = quad.left_operand rightOperand = quad.right_operand resultAddress = quad.result #print('Este es el operator ' + quad.operator) if isinstance(quad.left_operand, list): if len(quad.left_operand) == 1: leftOperand = self.memory.getValueByAddress( quad.left_operand[0]) #print('Este es el left operand ' + str(leftOperand)) if isinstance(quad.right_operand, list): if len(quad.right_operand) == 1: rightOperand = self.memory.getValueByAddress( quad.right_operand[0]) if isinstance(quad.result, list): if len(quad.result) == 1: resultAddress = self.memory.getValueByAddress( quad.result[0]) if quad.operator == '+': leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) resultValue = leftOperandValue + rightOperandValue self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '-': leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) resultValue = leftOperandValue - rightOperandValue self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '*': leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) resultValue = leftOperandValue * rightOperandValue self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '/': leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) resultValue = leftOperandValue / rightOperandValue self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '!': leftOperandValue = self.memory.getValueByAddress(leftOperand) result = not (leftOperandValue) self.memory.modifyValueByAddress(resultAddress, result) elif quad.operator == '=': leftOperandValue = self.memory.getValueByAddress(leftOperand) self.memory.modifyValueByAddress(resultAddress, leftOperandValue) elif quad.operator == '<': leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) resultValue = leftOperandValue < rightOperandValue self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '>': leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) resultValue = leftOperandValue > rightOperandValue self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '<=': leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) resultValue = leftOperandValue <= rightOperandValue self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '>=': leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) resultValue = leftOperandValue >= rightOperandValue self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '==': leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) resultValue = leftOperandValue == rightOperandValue self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '||': leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) resultValue = leftOperandValue or rightOperandValue self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '&&': leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) resultValue = leftOperandValue and rightOperandValue self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == '!=': leftOperandValue = self.memory.getValueByAddress(leftOperand) rightOperandValue = self.memory.getValueByAddress(rightOperand) resultValue = leftOperandValue != rightOperandValue self.memory.modifyValueByAddress(resultAddress, resultValue) elif quad.operator == 'WRITE': leftOperandValue = self.memory.getValueByAddress(leftOperand) print leftOperandValue elif quad.operator == 'READ': input = raw_input() if self.isInt(input): if leftOperand == 'int': self.memory.modifyValueByAddress( resultAddress, int(input)) else: print('Error: Input type mismatch, expecting ' + str(leftOperand)) sys.exit() elif self.isFloat(input): if leftOperand == 'float': self.memory.modifyValueByAddress( resultAddress, float(input)) else: print('Error: Input type mismatch, expecting ' + str(leftOperand)) sys.exit() elif input == 'true': if leftOperand == 'bool': self.memory.modifyValueByAddress(resultAddress, True) else: print('Error: Input type mismatch, expecting ' + str(leftOperand)) sys.exit() elif input == 'false': if leftOperand == 'bool': self.memory.modifyValueByAddress(resultAddress, False) else: print('Error: Input type mismatch, expecting ' + str(leftOperand)) sys.exit() else: if leftOperand == 'string': self.memory.modifyValueByAddress( resultAddress, str(input)) else: print('Error: Input type mismatch, expecting ' + str(leftOperand)) sys.exit() elif quad.operator == 'goto': instructionPointer = resultAddress - 2 elif quad.operator == 'gotof': leftOperandValue = self.memory.getValueByAddress(leftOperand) if leftOperandValue == False: instructionPointer = resultAddress - 2 elif quad.operator == 'gosub': savedIPs.push(quad.quad_number) instructionPointer = resultAddress - 2 elif quad.operator == 'RETURN': leftOperandValue = self.memory.getValueByAddress(leftOperand) self.memory.modifyValueByAddress(rightOperand, leftOperandValue) instructionPointer = resultAddress - 2 elif quad.operator == 'ENDPROC': if not functionsStack.isEmpty(): functionName = functionsStack.pop() function = self.functionsDirectory.functions[functionName] varTable = function['variables'] variables = varTable.variables for variable in variables: variableInfo = variables[variable] variableVirtualAddress = variableInfo[1] self.memory.modifyValueByAddress( variableVirtualAddress, valuesStack.pop()) instructionPointer = savedIPs.pop() - 1 elif quad.operator == 'ERA': function = self.functionsDirectory.functions[leftOperand] varTable = function['variables'] variables = varTable.variables for variable in variables: variableInfo = variables[variable] variableVirtualAddress = variableInfo[1] variableValue = self.memory.getValueByAddress( variableVirtualAddress) valuesStack.push(variableValue) functionsStack.push(leftOperand) elif quad.operator == 'VER': index = self.memory.getValueByAddress(leftOperand) if not (index >= rightOperand and index <= resultAddress): print("ERROR: Array index is out of bounds.") sys.exit() elif quad.operator == 'PARAM': leftOperandValue = self.memory.getValueByAddress(leftOperand) self.memory.modifyValueByAddress(resultAddress, leftOperandValue) elif quad.operator == 'DRAWBARCHART': plt.figure() plt.title("Bar Chart") plt.xlabel("X") plt.ylabel("Y") plt.bar(self.memory.getValueByAddress(leftOperand[0]), self.memory.getValueByAddress(leftOperand[1]), align='center') plt.bar(self.memory.getValueByAddress(leftOperand[2]), self.memory.getValueByAddress(leftOperand[3]), align='center') plt.bar(self.memory.getValueByAddress(leftOperand[4]), self.memory.getValueByAddress(leftOperand[5]), align='center') plt.bar(self.memory.getValueByAddress(leftOperand[6]), self.memory.getValueByAddress(leftOperand[7]), align='center') plt.plot() elif quad.operator == 'DRAWDOTCHART': plt.figure() plt.title("Dot Chart") plt.xlabel("X") plt.ylabel("Y") plt.plot([ self.memory.getValueByAddress(leftOperand[0]), self.memory.getValueByAddress(leftOperand[2]), self.memory.getValueByAddress(leftOperand[4]), self.memory.getValueByAddress(leftOperand[6]) ], [ self.memory.getValueByAddress(leftOperand[1]), self.memory.getValueByAddress(leftOperand[3]), self.memory.getValueByAddress(leftOperand[5]), self.memory.getValueByAddress(leftOperand[7]) ], 'ro') plt.axis([0, 6, 0, 20]) elif quad.operator == 'DRAWLINECHART': plt.figure() plt.title("Line Chart") plt.xlabel("X") plt.ylabel("Y") plt.plot([ self.memory.getValueByAddress(leftOperand[0]), self.memory.getValueByAddress(leftOperand[1]), self.memory.getValueByAddress(leftOperand[2]), self.memory.getValueByAddress(leftOperand[3]), self.memory.getValueByAddress(leftOperand[4]), self.memory.getValueByAddress(leftOperand[5]), self.memory.getValueByAddress(leftOperand[6]), self.memory.getValueByAddress(leftOperand[7]) ]) plt.axis([0, 6, 0, 20]) elif quad.operator == 'DRAWHISTCHART': plt.figure() # Crea semilla random np.random.seed(19680801) mu, sigma = self.memory.getValueByAddress( leftOperand[0]), self.memory.getValueByAddress( leftOperand[1]) x = mu + sigma * np.random.randn(10000) # Datos para histograma n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75) plt.xlabel('X') plt.ylabel('Y') plt.title('Histogram Chart') plt.axis([40, 160, 0, 0.03]) plt.grid(True) elif quad.operator == 'DRAWPOLYCHART': plt.figure() plt.title("Polygons Chart") t = np.arange(self.memory.getValueByAddress(leftOperand[0]), self.memory.getValueByAddress(leftOperand[1]), self.memory.getValueByAddress(leftOperand[2])) plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') elif quad.operator == 'END': #print("Quadruplo END") #print self.functionsDirectory.functions plt.show() sys.exit() instructionPointer += 1