def p_cte(p): ''' CTE : cte_i | cte_f | cte_c | null ''' global cteValue global cteType global operandsList global dirAddresses global funcName cteValue = p[1] if isinstance(p[1], int): cteType = "int" elif isinstance(p[1], float): cteType = "float" elif p[1] == "null": cteType = "string" elif isinstance(p[1], str): cteType = "char" addressTableKey = determineTypeAddressTable(None, cteType, cteValue, None) vAddress = dirAddresses[addressTableKey].getAnAddress() dirAddresses[addressTableKey].saveAddressData(vAddress, cteValue, cteType) #print (str(vAddress) + " : " + str(dirAddresses[addressTableKey].getAddressData(vAddress)["value"])) if p[1] == "null": cteType = "null" consOperand = Operand(None, cteValue, cteType, vAddress) operandsList.append(consOperand)
def p_variable_fix(p): ''' VARIABLE_FIX : id '[' cte_i ']' | id ''' global varName global varType global funcDirec global dirAddresses global funcName scope = "local" if (funcName == "global"): scope = "global" #es un arreglo if len(p) > 2: varName = p[1] arraySize = p[3] addressTableKey = determineTypeAddressTable(scope, varType, None, False) vAddress = dirAddresses[addressTableKey].getAnAdressForArray(arraySize) result = funcDirec.addLocalArrayToFunc(funcName, varName, varType, vAddress, arraySize) if isinstance(result, str): errorQueue.append("Error: " + result) print("Error: ", result) #es un id else: varName = p[1] addressTableKey = determineTypeAddressTable(scope, varType, None, False) vAddress = dirAddresses[addressTableKey].getAnAddress() result = funcDirec.addLocalVariableToFunc(funcName, varName, varType, False, vAddress) if isinstance(result, str): errorQueue.append("Error: " + result) print("Error: ", result)
def addExpressionCuadruple(self, operator, leftOperand, rightOperand, dirAddresses): result_type = SC[leftOperand.type][rightOperand.type][operator] if result_type == "error": return "Failed operation. type missmatch: cannot do " + operator + " operation between " + leftOperand.type + " and " + rightOperand.type else: self.resultsCounter += 1 resultName = "result" + str(self.resultsCounter) addressTableKey = determineTypeAddressTable(None,result_type,None,True) vAddress = dirAddresses[addressTableKey].getAnAddress() resultOperand = Operand(resultName, None, result_type, vAddress) self.quadruples.append( (operator, leftOperand, rightOperand, resultOperand) ) return resultOperand
def p_param_name(p): ''' PARAM_NAME : id ''' global varName global funcDirec global varType global dirAddresses scope = "local" varName = p[1] addressTableKey = determineTypeAddressTable(scope, varType, None, False) vAddress = dirAddresses[addressTableKey].getAnAddress() funcDirec.addLocalVariableToFunc(funcName, varName, varType, True, vAddress) funcDirec.addParamAddressOfFunc(funcName, vAddress)
def p_do_color(p): ''' DO_COLOR : cte_s ''' global dirAddresses global operandsList global quadruples cteValue = p[1] cteType = "string" addressTableKey = determineTypeAddressTable(None, cteType, cteValue, None) vAddress = dirAddresses[addressTableKey].getAnAddress() colorOperand = Operand(None, cteValue, cteType, vAddress) dirAddresses[addressTableKey].saveAddressData(vAddress, cteValue, cteType) quadruples.addColorQuadruple(colorOperand)
def p_sem_add_print_cte_s(p): ''' SEM_ADD_PRINT_CTE_S : cte_s ''' global operandsList global quadruples global dirAddresses global funcName cteValue = p[1] cteType = "string" addressTableKey = determineTypeAddressTable(None, cteType, cteValue, None) vAddress = dirAddresses[addressTableKey].getAnAddress() consOperand = Operand(None, cteValue, cteType, vAddress) dirAddresses[addressTableKey].saveAddressData(vAddress, cteValue, cteType) quadruples.addPrintCuadruple(consOperand)
def p_sem_check_array(p): ''' SEM_CHECK_ARRAY : ''' global idForArray global funcDirec global operandsList global operatorsList global funcDirec global dirAddresses global funcName global quadruples #checa si existe el arreglo retrievedArrayData = funcDirec.getVariableInFunc(funcName, idForArray) if isinstance(retrievedArrayData, str): errorQueue.append("Error: " + retrievedArrayData) print("Error: ", retrievedArrayData) elif not retrievedArrayData["isArray"]: errorQueue.append("Error: Failed operation. " + "variable " + idForArray + " is not an array") print("Error: Failed operation. " + "variable " + idForArray + " is not an array") else: arrayIndexExpression = operandsList.pop() #print (arrayIndexExpression.value) #print (arrayIndexExpression.type) #checa que el index del arreglo sea tipo int if arrayIndexExpression.type != "int": errorQueue.append( "Error: Failed operation. Int type index expected for array " + idForArray + ". " + arrayIndexExpression.type + "type was received instead.") print("Error: ", retrievedVar) #si ya recibi un index que es una constante if isinstance(arrayIndexExpression.value, int): actualAddress = retrievedArrayData[ "vAddress"] + arrayIndexExpression.value #checa que el index tipo int del arreglo se encuentre en el rango de direcciones validas para el arreglo if (actualAddress < retrievedArrayData["vAddress"]) or ( actualAddress > retrievedArrayData["sLimit"]): errorQueue.append("Error: Failed operation. Index " + arrayIndexExpression.value + " is out of bounds for array " + idForArray) print("Error: ", retrievedVar) else: operandsList.append( Operand(idForArray, None, retrievedArrayData["varType"], actualAddress)) #oh sorpresa, el index es una expresion cuyo valor solo se puede conocer en ejecucion, por lo tanto no sabemos el address del arreglo else: #Guarda la direccion base del arreglo en una direccion temporal addressTableKey = determineTypeAddressTable( None, "int", retrievedArrayData["vAddress"], None) vAddress = dirAddresses[addressTableKey].getAnAddress() dirAddresses[addressTableKey].saveAddressData( vAddress, retrievedArrayData["vAddress"], "int") #Crea un operando que represente la direccion base del arreglo baseAddressOperand = Operand(None, retrievedArrayData["vAddress"], "int", vAddress) #Suma el operando de la direccion base con el operando del indicie del arreglo para obtener la verdadera direccion realAdressOperand = quadruples.addExpressionCuadruple( "+", baseAddressOperand, arrayIndexExpression, dirAddresses) funcDirec.addTempVarCountInFunc(funcName, realAdressOperand.type) #baseArrayInfo = Operand(idForArray, None, retrievedArrayData["varType"],retrievedArrayData["vAddress"]) #resultOperand = quadruples.addArrayIndexCuadruple(baseArrayInfo,realAdressOperand) #Crea un operando de resultado con la informacion basica del arreglo (nombre, funcion, direccion base) resultOperand = Operand(idForArray, funcName, retrievedArrayData["varType"], realAdressOperand.vAddress) #y ademas, agregale al operando de resultado la info del operando tipo apuntador que tiene guardada su verdadera direccion (la migaja de pan para encontrar al operando del arreglo en los cuadruplos siguientes y resolver su address correcta en ejecucion) resultOperand.fakeAddress = realAdressOperand.vAddress quadruples.addArrayIndexCuadruple(resultOperand) copyResultOperand = Operand(resultOperand.name, resultOperand.value, resultOperand.type, resultOperand.vAddress) copyResultOperand.fakeAddress = realAdressOperand.vAddress operandsList.append(copyResultOperand)