def visitVariable_assignment( self, ctx: GrammarParser.Variable_assignmentContext): if (ctx.identifier() != None): variable_name = ctx.identifier().getText() token = ctx.identifier().IDENTIFIER().getPayload() else: variable_name = ctx.array().identifier().getText() token = ctx.array().identifier().IDENTIFIER().getPayload() if (self.inside_what_function): params = self.ids_defined[self.inside_what_function][1] defined_ids = list(map(lambda x: x[0], params)) if (variable_name in defined_ids): if (ctx.expression() != None): self._process_variable_assignment_error( ctx, variable_name, token) return if (self.ids_defined.get(variable_name) == None): print( f"ERROR: undefined variable '{variable_name}' in line {token.line} and column {token.column}" ) if (ctx.expression() != None): self._process_variable_assignment_error(ctx, variable_name, token) return return self.visitChildren(ctx)
def visitVariable_assignment( self, ctx: GrammarParser.Variable_assignmentContext): if ctx.expression() != None: expr_type = self.visitExpression(ctx.expression()) if ctx.identifier() != None: # Identifier token = ctx.identifier().IDENTIFIER().getPayload() text = ctx.identifier().getText() id_type = self.ids_defined.get(text, Type.VOID) else: # Array token = ctx.array().identifier().IDENTIFIER().getPayload() text = ctx.array().identifier().getText() id_type = self.ids_defined.get(text, Type.VOID) # Check the types of the variable that is receiving the assignment and the value being assigned. if not (id_type == expr_type or (id_type == Type.INT and expr_type == Type.FLOAT)): #print(str(id_type == Type.INT and expr_type == Type.FLOAT)) print( "[ERROR]::[Oh. My. God. You just tried to assign <{}> to <{}>. Wow.] ({},{})" .format(expr_type, id_type, str(token.line), str(token.column))) elif id_type == Type.INT and expr_type == Type.FLOAT: print( "[WARNING]::[This assignment of FLOAT to INT can cause loss of information. But apparently that is not the only thing that is lost here.] ({},{})" .format(str(token.line), str(token.column))) return self.visitChildren(ctx)
def visitVariable_assignment( self, ctx: GrammarParser.Variable_assignmentContext): op = ctx.OP.text if ctx.identifier() != None: name = ctx.identifier().getText() token = ctx.identifier().IDENTIFIER().getPayload() try: tyype, _ = self.ids_defined[name] except: print("ERROR: undefined variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) return else: name = ctx.array().identifier().getText() token = ctx.array().identifier().IDENTIFIER().getPayload() try: tyype, array_length = self.ids_defined[name] except: print("ERROR: undefined array '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) return self.visit(ctx.array()) if ctx.expression() != None: print("EXPRESSÃO") print(len(ctx.expression().expression())) if len(ctx.expression().expression()) == 0: print("TAMANHO ZERO") if ctx.expression().expression().integer() != None: cte_value = int( ctx.expression().expression().integer().getText()) print(cte_value) elif ctx.expression().expression().floating() != None: cte_value = float( ctx.expression().expression().floating().getText()) print(cte_value) expr_type = self.visit(ctx.expression()) if expr_type == Type.VOID or expr_type == Type.STRING: print("ERROR: trying to assign '" + expr_type + "' expression to variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) elif expr_type == Type.FLOAT and tyype == Type.INT: print( "WARNING: possible loss of information assigning float expression to int variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) if ctx.identifier() != None: self.ids_defined[name] = tyype, -1 else: # array self.ids_defined[name] = tyype, array_length return
def visitVariable_assignment(self, ctx:GrammarParser.Variable_assignmentContext): # print('visitVariable_assignment') if ctx.identifier() != None: name = ctx.identifier().getText() token = ctx.identifier().IDENTIFIER().getPayload() try: tyype, _, cte_value = self.ids_defined[name] except: print("ERROR: undefined variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) return else: # array name = ctx.array().identifier().getText() token = ctx.array().identifier().IDENTIFIER().getPayload() try: tyype, array_length, cte_values_array = self.ids_defined[name] except: print("ERROR: undefined array '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) array_index = self.visit(ctx.array()) if cte_values_array != None: cte_value = cte_values_array[array_index] # print("CTE_VALUE", cte_value) else: cte_value = None # ATUALIZAR O VALOR DA VARIAVEL OU DO VALOR NAQUELA POSICAO DO ARRAY POR MEIO DE CTE_VALUE op = ctx.OP.text if op == '++' or op == '--': # print('PRINT 1') if op == '++': # print(self.ids_defined) cte_value = cte_value + 1 elif op == '--': cte_value = cte_value - 1 else: print('Erro - Operadores do For-Step') else: # print("PRINT 2") expr_type, expr_cte_value = self.visit(ctx.expression()) if expr_type == Type.VOID: print("ERROR: trying to assign void expression to variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) elif expr_type == Type.FLOAT and tyype == Type.INT: print("WARNING: possible loss of information assigning float expression to int variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) if ctx.identifier() != None: # print("PRINT 3") # print(name, cte_value) self.ids_defined[name] = tyype, -1, cte_value # print(self.ids_defined) else: # array # print("PRINT 4") if cte_values_array != None: cte_values_array[array_index] = cte_value self.ids_defined[name] = tyype, array_length, cte_values_array return
def visitVariable_assignment( self, ctx: GrammarParser.Variable_assignmentContext): op = ctx.OP.text #print("assignment: "+ ctx.getText()) tyype_variable = Type.VOID if ctx.identifier() != None: name = ctx.identifier().getText() #print(name) token = ctx.identifier().IDENTIFIER().getPayload() def_identifier = self.ids_defined.get(name) if def_identifier == None: print( "ERROR: trying to assign to a non-defined variable '{}' in line {} and column {}" .format(name, str(token.line), str(token.column))) elif ctx.expression() == None: tyype = def_identifier[0] if tyype != Type.INT and tyype != Type.FLOAT: print( "ERROR: binary operator '{}' used to variable of type '{}' in line {} and column {}" .format(op, tyype, str(token.line), str(token.column))) else: tyype_expression = self.visitExpression(ctx.expression()) tyype_variable = def_identifier[0] if ctx.expression().array() != None: self.visit(ctx.expression().array()) if tyype_expression == None: return None if tyype_variable == Type.INT and tyype_expression == Type.FLOAT: print( "WARNING: possible loss of information assigning float expression to int variable '{}' in line {} and column {}" .format(str(name), str(token.line), str(token.column))) elif tyype_variable == tyype_expression: if tyype_variable != Type.FLOAT and tyype_variable != Type.INT: print( "ERROR: binary operator '{}' used to variable of type '{}' in line {} and column {}" .format(op, tyype_variable, str(token.line), str(token.column))) elif not (tyype_variable == Type.FLOAT and tyype_expression == Type.INT): print( "ERROR: trying to assign '{}' expression to variable '{}' in line {} and column {}" .format(tyype_expression, name, str(token.line), str(token.column)))
def visitVariable_assignment( self, ctx: GrammarParser.Variable_assignmentContext): op = ctx.OP.text # identifier assignment if ctx.identifier() != None: name = ctx.identifier().getText() token = ctx.identifier().IDENTIFIER().getPayload() try: tyype, _, cte_value, ir_register = self.ids_defined[name] except: err("ERROR: undefined variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) return # array assignment else: name = ctx.array().identifier().getText() token = ctx.array().identifier().IDENTIFIER().getPayload() try: tyype, array_length, cte_values_array, ir_registers_array = self.ids_defined[ name] except: err("ERROR: undefined array '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) array_index_cte, array_index_ir = self.visit(ctx.array()) if array_index_cte == None: cte_value = None else: if array_index_cte < 0 or array_index_cte >= array_length: err("ERROR: array '" + name + "' index out of range in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) else: cte_value = cte_values_array[array_index_cte] ir_register = ir_registers_array[array_index_cte] if op == '++' or op == '--': if cte_value != None: if op == '++': cte_value += 1 elif op == '--': cte_value -= 1 else: cte_value = None if ir_register != None: out = " %" + str(self.next_ir_register) + " = " self.filee.write(out) if op == '++': out = "add nsw " + llvm_type(tyype) + " %" + str( ir_register) + ", 1\n" self.filee.write(out) elif op == '--': out = "sub nsw " + llvm_type(tyype) + " %" + str( ir_register) + ", 1\n" self.filee.write(out) ir_register = self.next_ir_register self.next_ir_register += 1 else: expr_type, expr_cte_value, expr_ir_register = self.visit( ctx.expression()) if expr_type == Type.VOID: err("ERROR: trying to assign void expression to variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) elif expr_type == Type.FLOAT and tyype == Type.INT: err("WARNING: possible loss of information assigning float expression to int variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") if op == '=': cte_value = expr_cte_value if cte_value != None: if op == '+=': cte_value += expr_cte_value elif op == '-=': cte_value -= expr_cte_value elif op == '*=': cte_value *= expr_cte_value elif op == '/=': cte_value /= expr_cte_value if ir_register != None: out = " %" + str(self.next_ir_register) + " = " self.filee.write(out) if op == '+=': if expr_ir_register != None: out = "add nsw " + llvm_type(tyype) + " %" + str( ir_register) + ", %" + str(expr_ir_register) + "\n" self.filee.write(out) else: out = "add nsw " + llvm_type(tyype) + " %" + str( ir_register) + ", " + str(expr_cte_value) + "\n" self.filee.write(out) elif op == '-=': if expr_ir_register != None: out = "sub nsw " + llvm_type(tyype) + " %" + str( ir_register) + ", %" + str(expr_ir_register) + "\n" self.filee.write(out) else: out = "sub nsw " + llvm_type(tyype) + " %" + str( ir_register) + ", " + str(expr_cte_value) + "\n" self.filee.write(out) elif op == '*=': if expr_ir_register != None: out = "mul nsw " + llvm_type(tyype) + " %" + str( ir_register) + ", %" + str(expr_ir_register) + "\n" self.filee.write(out) else: out = "mul nsw " + llvm_type(tyype) + " %" + str( ir_register) + ", " + str(expr_cte_value) + "\n" self.filee.write(out) elif op == '/=': if expr_ir_register != None: out = "sdiv nsw " + llvm_type(tyype) + " %" + str( ir_register) + ", %" + str(expr_ir_register) + "\n" self.filee.write(out) else: out = "sdiv nsw " + llvm_type(tyype) + " %" + str( ir_register) + ", " + str(expr_cte_value) + "\n" self.filee.write(out) ir_register = self.next_ir_register self.next_ir_register += 1 elif ir_register == None and expr_ir_register != None: ir_register = self.next_ir_register - 1 if ctx.identifier() != None: self.ids_defined[name] = tyype, -1, cte_value, ir_register else: # array if array_index_cte != None: cte_values_array[array_index_cte] = cte_value ir_registers_array[array_index_cte] = ir_register self.ids_defined[ name] = tyype, array_length, cte_values_array, ir_registers_array return
def visitVariable_assignment( self, ctx: GrammarParser.Variable_assignmentContext): op = ctx.OP.text name = None tyype = None value = None is_constant = None array_index = None params = [] #vamos pegar os parametros da funcao, que foram declarados nos argumentos if self.inside_what_function != "": function_type, params = self.ids_defined.get( self.inside_what_function) if ctx.identifier() != None: name = ctx.identifier().getText() token = ctx.identifier().IDENTIFIER().getPayload() try: #checando no ids_defined tyype, _, value, is_constant = self.ids_defined[name] except: try: #se nao tiver no ids_defined é pra ta nos parametros ne tyype = params[name] is_constant = False except: print("ERROR: undefined variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) return else: #array #print(ctx.getText()) name = ctx.array().identifier().getText() token = ctx.array().identifier().IDENTIFIER().getPayload() #print(self.ids_defined[name]) try: tyype, array_length, values = self.ids_defined[name] except: print("ERROR: undefined array '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) return array_index = self.visit(ctx.array()) if array_index < 0 or array_index >= array_length: print("ERROR: array '" + name + "' index out of range in line " + str(token.line) + " and column " + str(token.column)) return _, value, is_constant = values[array_index] if ctx.expression() != None: expr_type, expr_value, expr_is_constant = self.visit( ctx.expression()) #print(expr_is_constant) #aqui é como se a gente visse a pilha dos ifs e pegava os nomes das variaveis que podem deixar de ser constantes ao serem usadas detro de uma condicao prior_variables = [] try: prior_variables = self.connected_to_condition[-1] except: prior_variables = [] #se tiver, xau xau constante if name in prior_variables: expr_value = None expr_is_constant = False if ctx.identifier() != None: if not is_constant or not expr_is_constant: self.ids_defined[name] = tyype, -1, None, False else: if op == '/=': expr_value = value / expr_value elif op == '*=': expr_value = value * expr_value elif op == '+=': expr_value = value + expr_value elif op == '-=': expr_value = value - expr_value self.ids_defined[ name] = tyype, -1, expr_value, expr_is_constant else: # array if not is_constant or not expr_is_constant: values[array_index] = tyype, None, False else: if op == '/=': expr_value = value / expr_value elif op == '*=': expr_value = value * expr_value elif op == '+=': expr_value = value + expr_value elif op == '-=': expr_value = value - expr_value values[array_index] = tyype, expr_value, expr_is_constant #print(self.ids_defined[name]) self.ids_defined[name] = tyype, array_length, values #print(self.ids_defined[name]) if expr_type == Type.VOID or expr_type == Type.STRING: print("ERROR: trying to assign '" + expr_type + "' expression to variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) elif expr_type == Type.FLOAT and tyype == Type.INT: print( "WARNING: possible loss of information assigning float expression to int variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) else: #aqui é a mesma coisa so que pra var++ e var-- prior_variables = [] try: prior_variables = self.connected_to_condition[-1] except: prior_variables = [] #print("nothing in prior") if name in prior_variables: value = None is_constant = False if ctx.identifier() != None: if not is_constant: self.ids_defined[name] = tyype, -1, None, False else: if op == '++': value += 1 elif op == '--': value -= 1 self.ids_defined[name] = tyype, -1, value, is_constant else: # array if not is_constant: values[array_index] = tyype, None, False else: if op == '++': value += 1 elif op == '--': value -= 1 values[array_index] = tyype, value, is_constant self.ids_defined[name] = tyype, array_length, values return
def visitVariable_assignment( self, ctx: GrammarParser.Variable_assignmentContext): if ctx.identifier() != None: name = ctx.identifier().getText() token = ctx.identifier().IDENTIFIER().getPayload() try: tyype, _, cte_value = self.ids_defined[name] except: print("ERROR: undefined variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) return else: # array name = ctx.array().identifier().getText() token = ctx.array().identifier().IDENTIFIER().getPayload() try: tyype, array_length, cte_values_array = self.ids_defined[name] except: print("ERROR: undefined array '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) array_index = self.visit(ctx.array()) if cte_values_array != None: cte_value = cte_values_array[array_index] else: cte_value = None # ATUALIZAR O VALOR DA VARIAVEL OU DO VALOR NAQUELA POSICAO DO ARRAY POR MEIO DE CTE_VALUE op = ctx.OP.text if op == '++' or op == '--': if cte_value is None: cte_value = None elif op == '++': cte_value = eval("{} {} {}".format(cte_value, '+', 1)) elif op == '--': cte_value = eval("{} {} {}".format(cte_value, '-', 1)) else: expr_type, expr_cte_value = self.visit(ctx.expression()) if expr_type == Type.VOID: print("ERROR: trying to assign void expression to variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) elif expr_type == Type.FLOAT and tyype == Type.INT: print( "WARNING: possible loss of information assigning float expression to int variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) if op == '=': cte_value = expr_cte_value elif expr_cte_value == None or cte_value == None: cte_value = None elif op == '/=': cte_value = eval("{} {} {}".format(cte_value, '/', expr_cte_value)) elif op == '*=': cte_value = eval("{} {} {}".format(cte_value, '*', expr_cte_value)) elif op == '+=': cte_value = eval("{} {} {}".format(cte_value, '+', expr_cte_value)) elif op == '-=': cte_value = eval("{} {} {}".format(cte_value, '-', expr_cte_value)) else: cte_value = eval("{} {} {}".format(cte_value, op, expr_cte_value)) if ctx.identifier() != None: self.ids_defined[name] = tyype, -1, cte_value else: # array if cte_values_array != None: cte_values_array[array_index] = cte_value self.ids_defined[name] = tyype, array_length, cte_values_array return
def visitVariable_assignment( self, ctx: GrammarParser.Variable_assignmentContext): op = ctx.OP.text # identifier assignment if ctx.identifier() != None: name = ctx.identifier().getText() token = ctx.identifier().IDENTIFIER().getPayload() try: tyype, _, cte_value, ir_register = self.ids_defined[name] except: err("ERROR: undefined variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) return # array assignment else: name = ctx.array().identifier().getText() token = ctx.array().identifier().IDENTIFIER().getPayload() try: tyype, array_length, cte_values_array, ir_registers_array = self.ids_defined[ name] except: err("ERROR: undefined array '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) array_index_cte, array_index_ir = self.visit(ctx.array()) if array_index_cte == None: cte_value = None else: if array_index_cte < 0 or array_index_cte >= array_length: err("ERROR: array '" + name + "' index out of range in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) else: cte_value = cte_values_array[array_index_cte] ir_register = ir_registers_array[array_index_cte] if op == "++" or op == "--": tyype, _, _, _ = self.ids_defined[name] ir_register = self.next_ir_register self.next_ir_register += 1 f.write("\t%{} = load {}, {}* %{}, align 4".format( ir_register, llvm_type(tyype), llvm_type(tyype), name) + "\n") if cte_value != None: if op == "++": cte_value += 1 process_operands(self, "add", ir_register, 1, None, Type.INT, tyype, name) elif op == "--": cte_value -= 1 process_operands(self, "sub", ir_register, 1, None, Type.INT, tyype, name) else: if op == "++": process_operands(self, "add", ir_register, 1, None, Type.INT, tyype, name) elif op == "--": process_operands(self, "sub", ir_register, 1, None, Type.INT, tyype, name) cte_value = None else: expr_type, expr_cte_value, expr_ir_register = self.visit( ctx.expression()) if expr_type == Type.VOID: err("ERROR: trying to assign void expression to variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) elif expr_type == Type.FLOAT and tyype == Type.INT: err("WARNING: possible loss of information assigning float expression to int variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") tyype, _, _, _ = self.ids_defined[name] if not self.ids_defined[name][3]: ir_register = self.next_ir_register self.next_ir_register += 1 f.write("\t%{} = load {}, {}* %{}, align 4".format( ir_register, llvm_type(tyype), llvm_type(tyype), name) + "\n") self.ids_defined[name] = self.ids_defined[name][ 0], self.ids_defined[name][1], self.ids_defined[name][ 2], ir_register if op == "=": cte_value = expr_cte_value f.write("\tstore {} %{}, {}* %{}, align 4".format( llvm_type(tyype), ir_register, llvm_type(tyype), name) + "\n") if cte_value != None: if op == "+=": cte_value += expr_cte_value process_operands(self, "add", ir_register, expr_cte_value, expr_ir_register, expr_type, tyype, name) elif op == "-=": cte_value -= expr_cte_value process_operands(self, "sub", ir_register, expr_cte_value, expr_ir_register, expr_type, tyype, name) elif op == "*=": cte_value *= expr_cte_value process_operands(self, "mul", ir_register, expr_cte_value, expr_ir_register, expr_type, tyype, name) elif op == "/=": cte_value /= expr_cte_value process_operands(self, "sdiv", ir_register, expr_cte_value, expr_ir_register, expr_type, tyype, name) if ctx.identifier() != None: self.ids_defined[name] = tyype, -1, cte_value, ir_register else: # array if array_index_cte != None: cte_values_array[array_index_cte] = cte_value ir_registers_array[array_index_cte] = ir_register self.ids_defined[ name] = tyype, array_length, cte_values_array, ir_registers_array return
def visitVariable_assignment( self, ctx: GrammarParser.Variable_assignmentContext): op = ctx.OP.text llvm_op = "" name = None tyype = None cte_value = None ir_register = None is_inside = self.inside_what_function != "" # identifier assignment if ctx.identifier() != None: name = ctx.identifier().getText() token = ctx.identifier().IDENTIFIER().getPayload() try: tyype, _, cte_value, ir_register = self.ids_defined[name] except: err("ERROR: undefined variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) return # array assignment else: name = ctx.array().identifier().getText() token = ctx.array().identifier().IDENTIFIER().getPayload() try: tyype, array_length, cte_values_array, ir_registers_array = self.ids_defined[ name] except: err("ERROR: undefined array '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) array_index_cte, array_index_ir = self.visit(ctx.array()) if array_index_cte == None: cte_value = None else: if array_index_cte < 0 or array_index_cte >= array_length: err("ERROR: array '" + name + "' index out of range in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) else: cte_value = cte_values_array[array_index_cte] ir_register = ir_registers_array[array_index_cte] if op == '++' or op == '--': printLoad(self.next_ir_register, tyype, name, tyype, 4, is_inside) self.next_ir_register += 1 if cte_value != None: if op == '++': cte_value += 1 elif op == '--': cte_value -= 1 else: cte_value = None if op == '++': if (tyype == Type.FLOAT): llvm_op = "fadd" else: llvm_op = "add" elif op == '--': if (tyype == Type.FLOAT): llvm_op = "fsub" else: llvm_op = "sub" one_value = 1 if tyype == Type.FLOAT: one_value = float_to_hex(float(one_value)) llvm_tyype = llvm_type(tyype) printOper(llvm_op, self.next_ir_register, llvm_tyype, self.next_ir_register - 1, one_value, is_inside, False, True) printStore(self.next_ir_register, llvm_tyype, name, llvm_tyype, 4, is_inside, False) self.next_ir_register += 1 else: if op != '=' and self.ids_defined[name][3] == None: temp = list(self.ids_defined[name]) temp[3] = self.next_ir_register self.ids_defined[name] = tuple(temp) printLoad(self.next_ir_register, tyype, name, tyype, 4, is_inside) self.next_ir_register += 1 expr_type, expr_cte_value, expr_ir_register = self.visit( ctx.expression()) llvm_expr_type = llvm_type(expr_type) llvm_tyype = llvm_type(tyype) if expr_type == Type.INT and tyype == Type.FLOAT and expr_cte_value == None: printIntToFloat(self.next_ir_register, expr_ir_register, has_tab=True) expr_ir_register = self.next_ir_register self.next_ir_register += 1 if expr_type == Type.VOID: err("ERROR: trying to assign void expression to variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) elif expr_type == Type.FLOAT and tyype == Type.INT: err("WARNING: possible loss of information assigning float expression to int variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") if op == '=': cte_value = expr_cte_value if cte_value != None: if op == '+=': cte_value += expr_cte_value elif op == '-=': cte_value -= expr_cte_value elif op == '*=': cte_value *= expr_cte_value elif op == '/=': cte_value /= expr_cte_value if op == '+=': if (tyype == Type.FLOAT): llvm_op = "fadd" else: llvm_op = "add" elif op == '-=': if (tyype == Type.FLOAT): llvm_op = "fsub" else: llvm_op = "sub" elif op == '*=': if (tyype == Type.FLOAT): llvm_op = "fmul" else: llvm_op = "mul" elif op == '/=': if (tyype == Type.INT): llvm_op = "sdiv" elif (tyype == Type.FLOAT): llvm_op = "fdiv" else: llvm_op = "udiv" expr_cte_value_str = str(expr_cte_value) if (tyype == Type.FLOAT): expr_cte_value_str = float_to_hex(float(expr_cte_value)) if op != '=': if expr_cte_value != None: printOper(llvm_op, self.next_ir_register, llvm_tyype, self.ids_defined[name][3], expr_cte_value, is_inside, False, True) else: printOper(llvm_op, self.next_ir_register, llvm_tyype, self.ids_defined[name][3], self.next_ir_register - 1, is_inside, False, False) printStore(self.next_ir_register, llvm_expr_type, name, llvm_tyype, 4, is_inside, False) self.next_ir_register += 1 else: if expr_cte_value != None: printStore(expr_cte_value_str, llvm_expr_type, name, llvm_tyype, 4, is_inside, True) else: printStore(expr_ir_register, llvm_expr_type, name, llvm_tyype, 4, is_inside, False) if ctx.identifier() != None: self.ids_defined[name] = tyype, -1, cte_value, ir_register else: # array if array_index_cte != None: cte_values_array[array_index_cte] = cte_value ir_registers_array[array_index_cte] = ir_register self.ids_defined[ name] = tyype, array_length, cte_values_array, ir_registers_array return
def visitVariable_assignment( self, ctx: GrammarParser.Variable_assignmentContext): op = ctx.OP.text # identifier assignment if ctx.identifier() != None: name = ctx.identifier().getText() token = ctx.identifier().IDENTIFIER().getPayload() try: tyype, _, cte_value, ir_register = self.ids_defined[name] except: err("ERROR: undefined variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) return # array assignment else: name = ctx.array().identifier().getText() token = ctx.array().identifier().IDENTIFIER().getPayload() try: tyype, array_length, cte_values_array, ir_registers_array = self.ids_defined[ name] except: err("ERROR: undefined array '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) array_index_cte, array_index_ir = self.visit(ctx.array()) if array_index_cte == None: cte_value = None else: if array_index_cte < 0 or array_index_cte >= array_length: err("ERROR: array '" + name + "' index out of range in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) else: cte_value = cte_values_array[array_index_cte] ir_register = ir_registers_array[array_index_cte] if op == '++' or op == '--': if cte_value != None: if op == '++': cte_value += 1 elif op == '--': cte_value -= 1 else: ir_register_before_operation = ir_register ir_register = self.next_ir_register self.next_ir_register += 1 if op == '++': printf(" %%%s = add nsw %s %%%s, %s\n", ir_register, llvm_type(tyype), ir_register_before_operation, 1) elif op == '--': printf(" %%%s = sub nsw %s %%%s, %s\n", ir_register, llvm_type(tyype), ir_register_before_operation, -1) else: print("We have a bug on operations ++ and --") cte_value = None else: expr_type, expr_cte_value, expr_ir_register = self.visit( ctx.expression()) if expr_type == Type.VOID: err("ERROR: trying to assign void expression to variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) elif expr_type == Type.FLOAT and tyype == Type.INT: err("WARNING: possible loss of information assigning float expression to int variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") if op == '=': cte_value = expr_cte_value if cte_value != None: if op == '+=': cte_value += expr_cte_value elif op == '-=': cte_value -= expr_cte_value elif op == '*=': cte_value *= expr_cte_value elif op == '/=': cte_value /= expr_cte_value else: ir_register_before_operation = ir_register ir_register = self.next_ir_register self.next_ir_register += 1 if op == '*=': printf( " %%%s = mul nsw %s %%%s, %%%s\n", ir_register, llvm_type(tyype), ir_register_before_operation, ir_register - 1 ) #doubt verificar esse ultimo argumento se ta pegando certo elif op == '/=': printf(" %%%s = sdiv nsw %s %%%s, %%%s\n", ir_register, llvm_type(tyype), ir_register_before_operation, ir_register - 1) elif op == '+=': printf(" %%%s = add nsw %s %%%s, %%%s\n", ir_register, llvm_type(tyype), ir_register_before_operation, ir_register - 1) elif op == '-=': printf(" %%%s = sub nsw %s %%%s, %%%s\n", ir_register, llvm_type(tyype), ir_register_before_operation, ir_register - 1) elif op == '=': ir_register = expr_ir_register cte_value = None if ctx.identifier() != None: self.ids_defined[name] = tyype, -1, cte_value, ir_register else: # array if array_index_cte != None: cte_values_array[array_index_cte] = cte_value ir_registers_array[array_index_cte] = ir_register self.ids_defined[ name] = tyype, array_length, cte_values_array, ir_registers_array return
def visitVariable_assignment( self, ctx: GrammarParser.Variable_assignmentContext): op = ctx.OP.text if ctx.identifier() != None: name = ctx.identifier().getText() token = ctx.identifier().IDENTIFIER().getPayload() try: tyype, _, const_value = self.ids_defined[name] except: print("ERROR: undefined variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) return else: name = ctx.array().identifier().getText() token = ctx.array().identifier().IDENTIFIER().getPayload() try: tyype, array_length, const_array_values = self.ids_defined[ name] except: print("ERROR: undefined array '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) # return array index index = self.visit(ctx.array()) if const_array_values: const_value = const_array_values[index] else: const_value = None op = ctx.OP.text if op in ['++', '--']: if not const_value: const_value = None else: const_value = eval(f"const_value {op[0]} 1") else: expr_type, expr_const_value = self.visit(ctx.expression()) if expr_type == Type.VOID or expr_type == Type.STRING: print("ERROR: trying to assign '" + expr_type + "' expression to variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) elif expr_type == Type.FLOAT and tyype == Type.INT: print( "WARNING: possible loss of information assigning float expression to int variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) if op == '=': const_value = expr_const_value elif not (expr_const_value and const_value): const_value = None # compound operators elif op in ['+=', '-=', '*=', '/=']: const_value = eval(f"const_value {op[0]} expr_const_value") # commons operators else: const_value = eval(f"const_value {op} expr_const_value") if ctx.identifier() != None: self.ids_defined[name] = tyype, -1, const_value else: # array if const_array_values: const_array_values[index] = const_value self.ids_defined[name] = tyype, array_length, const_array_values return
def visitVariable_assignment(self, ctx:GrammarParser.Variable_assignmentContext): op = ctx.OP.text # identifier assignment if ctx.identifier() != None: name = ctx.identifier().getText() token = ctx.identifier().IDENTIFIER().getPayload() #print('name',name) try: tyype, _, cte_value, ir_register = self.ids_defined[name] except: err("ERROR: undefined variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) return # array assignment else: name = ctx.array().identifier().getText() token = ctx.array().identifier().IDENTIFIER().getPayload() try: tyype, array_length, cte_values_array, ir_registers_array = self.ids_defined[name] except: err("ERROR: undefined array '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) array_index_cte, array_index_ir = self.visit(ctx.array()) if array_index_cte == None: cte_value = None else: if array_index_cte < 0 or array_index_cte >= array_length: err("ERROR: array '" + name + "' index out of range in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) else: cte_value = cte_values_array[array_index_cte] ir_register = ir_registers_array[array_index_cte] if op == '++' or op == '--': if cte_value != None: if op == '++': cte_value += 1 elif op == '--': cte_value -= 1 else: cte_value = 1 if op == '++': self.func_count[self.inside_what_function] += 1 ir_register = self.func_count[self.inside_what_function] print(' ' +"%"+str(ir_register)+" = load "+str(llvm_type(tyype))+", "+str(llvm_type(tyype))+"* %"+str(name)+", align 4") if tyype == Type.INT or tyype == Type.FLOAT: ir_register += 1 print(' ' +"%"+str(ir_register)+" = add "+str(llvm_type(tyype))+" %"+str(self.func_count[self.inside_what_function])+", "+ str(cte_value)) print(' ' +'store ' + str(llvm_type(tyype)) + ' %' + str(ir_register) + ', '+ str(llvm_type(tyype)) + '* %' + name+ ', align 4') cte_value += 1 self.func_count[self.inside_what_function] = ir_register else: expr_type, expr_cte_value, expr_ir_register = self.visit(ctx.expression()) self.in_expr = [] if expr_type == Type.VOID: err("ERROR: trying to assign void expression to variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) elif expr_type == Type.FLOAT and tyype == Type.INT: err("WARNING: possible loss of information assigning float expression to int variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column) + "\n") if op == '=': cte_value = expr_cte_value elif op == '+=': self.func_count[self.inside_what_function] += 1 ir_register = self.func_count[self.inside_what_function] print(' ' +"%"+str(ir_register)+" = load "+str(llvm_type(tyype))+", "+str(llvm_type(tyype))+"* %"+str(name)+", align 4") if tyype == Type.INT: ir_register += 1 print(' ' +"%"+str(ir_register)+" = add "+str(llvm_type(tyype))+" %"+str(ir_register)+", "+('%'+str(expr_ir_register) if expr_ir_register != None else str(expr_cte_value))) elif tyype == Type.FLOAT: ir_register += 1 print(' ' +"%"+str(ir_register)+" = fadd "+str(llvm_type(tyype))+" %"+str(ir_register)+", "+('%'+str(expr_ir_register) if expr_ir_register != None else str(expr_cte_value))) print(' ' +'store ' + str(llvm_type(expr_type)) + ' %' + str(ir_register) + ', '+ str(llvm_type(tyype)) + '* %' + name+ ', align 4') if cte_value != None and expr_cte_value != None: cte_value += expr_cte_value elif op == '-=': self.func_count[self.inside_what_function] += 1 ir_register = self.func_count[self.inside_what_function] print(' ' +"%"+str(ir_register)+" = load "+str(llvm_type(tyype))+", "+str(llvm_type(tyype))+"* %"+str(name)+", align 4") if tyype == Type.INT: ir_register += 1 print(' ' +"%"+str(ir_register)+" = sub "+str(llvm_type(tyype))+" %"+str(ir_register)+", "+('%'+str(expr_ir_register) if expr_ir_register != None else str(expr_cte_value))) elif tyype == Type.FLOAT: ir_register += 1 print(' ' +"%"+str(ir_register)+" = fsub "+str(llvm_type(tyype))+" %"+str(ir_register)+", "+('%'+str(expr_ir_register) if expr_ir_register != None else str(expr_cte_value))) print(' ' +'store ' + str(llvm_type(expr_type)) + ' %' + str(ir_register) + ', '+ str(llvm_type(tyype)) + '* %' + name+ ', align 4') if cte_value != None and expr_cte_value != None: cte_value -= expr_cte_value elif op == '*=': self.func_count[self.inside_what_function] += 1 ir_register = self.func_count[self.inside_what_function] print(' ' +"%"+str(ir_register)+" = load "+str(llvm_type(tyype))+", "+str(llvm_type(tyype))+"* %"+str(name)+", align 4") if tyype == Type.INT: ir_register += 1 print(' ' +"%"+str(ir_register)+" = mul "+str(llvm_type(tyype))+" %"+str(ir_register)+", "+('%'+str(expr_ir_register) if expr_ir_register != None else str(expr_cte_value))) elif tyype == Type.FLOAT: ir_register += 1 print(' ' +"%"+str(ir_register)+" = fmul "+str(llvm_type(tyype))+" %"+str(ir_register)+", "+('%'+str(expr_ir_register) if expr_ir_register != None else str(expr_cte_value))) print(' ' +'store ' + str(llvm_type(expr_type)) + ' %' + str(ir_register) + ', '+ str(llvm_type(tyype)) + '* %' + name+ ', align 4') if cte_value != None and expr_cte_value != None: cte_value *= expr_cte_value elif op == '/=': self.func_count[self.inside_what_function] += 1 ir_register = self.func_count[self.inside_what_function] print(' ' +"%"+str(ir_register)+" = load "+str(llvm_type(tyype))+", "+str(llvm_type(tyype))+"* %"+str(name)+", align 4") if tyype == Type.INT: ir_register += 1 print(' ' +"%"+str(ir_register)+" = sdiv "+str(llvm_type(tyype))+" %"+str(ir_register)+", "+('%'+str(expr_ir_register) if expr_ir_register != None else str(expr_cte_value))) elif tyype == Type.FLOAT: ir_register += 1 print(' ' +"%"+str(ir_register)+" = fdiv "+str(llvm_type(tyype))+" %"+str(ir_register)+", "+('%'+str(expr_ir_register) if expr_ir_register != None else str(expr_cte_value))) print(' ' +'store ' + str(llvm_type(expr_type)) + ' %' + str(ir_register) + ', '+ str(llvm_type(tyype)) + '* %' + name+ ', align 4') if cte_value != None and expr_cte_value != None: cte_value /= expr_cte_value self.func_count[self.inside_what_function] = ir_register if ctx.identifier() != None: self.ids_defined[name] = tyype, -1, cte_value, ir_register #print(self.ids_defined[name]) # print(self.visit(ctx.identifier())) else: # array if array_index_cte != None: cte_values_array[array_index_cte] = cte_value ir_registers_array[array_index_cte] = ir_register self.ids_defined[name] = tyype, array_length, cte_values_array, ir_registers_array print(" ") return
def visitVariable_assignment( self, ctx: GrammarParser.Variable_assignmentContext): if ctx.identifier() != None: name = ctx.identifier().getText() token = ctx.identifier().IDENTIFIER().getPayload() try: tyype, _, cte_value = self.ids_defined[name] except: print("ERROR: undefined variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) return else: # array name = ctx.array().identifier().getText() token = ctx.array().identifier().IDENTIFIER().getPayload() try: tyype, array_length, cte_values_array = self.ids_defined[name] except: print("ERROR: undefined array '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) array_index = self.visit(ctx.array()) if cte_values_array != None: if array_index >= 0 and array_index < array_length: cte_value = cte_values_array[array_index] else: cte_value = None # ATUALIZAR O VALOR DA VARIAVEL OU DO VALOR NAQUELA POSICAO DO ARRAY POR MEIO DE CTE_VALUE op = ctx.OP.text if op == '++' or op == '--': pass else: expr_type, expr_cte_value = self.visit(ctx.expression()) if expr_type == Type.VOID: print("ERROR: trying to assign void expression to variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) elif expr_type == Type.FLOAT and tyype == Type.INT: print( "WARNING: possible loss of information assigning float expression to int variable '" + name + "' in line " + str(token.line) + " and column " + str(token.column)) if ctx.identifier() != None: if op == '=' and expr_cte_value != None: cte_value = expr_cte_value elif op == '+=' and cte_value != None and expr_cte_value != None: cte_value += expr_cte_value elif op == '-=' and cte_value != None and expr_cte_value != None: cte_value -= expr_cte_value elif op == '*=' and cte_value != None and expr_cte_value != None: cte_value -= expr_cte_value elif op == '/=' and cte_value != None and expr_cte_value != None: cte_value /= expr_cte_value elif op == '++' and cte_value != None: cte_value += 1 elif op == '--' and cte_value != None: cte_value -= 1 if tyype == Type.INT and cte_value != None: self.ids_defined[name] = tyype, -1, int(cte_value) else: self.ids_defined[name] = tyype, -1, cte_value else: # array if array_index >= 0 and array_index < array_length: if cte_values_array == None: cte_values_array = [] for i in range(array_length): cte_values_array += [None] #lixo cte_value = cte_values_array[array_index] if op == '=' and expr_cte_value != None: if tyype == Type.INT: cte_values_array[array_index] = int(expr_cte_value) else: cte_values_array[array_index] = expr_cte_value elif op == '+=' and cte_value != None and expr_cte_value != None: if tyype == Type.INT: cte_values_array[array_index] = int(cte_value + expr_cte_value) else: cte_values_array[array_index] += expr_cte_value elif op == '-=' and cte_value != None and expr_cte_value != None: if tyype == Type.INT: cte_values_array[array_index] = int(cte_value - expr_cte_value) else: cte_values_array[array_index] -= expr_cte_value elif op == '*=' and cte_value != None and expr_cte_value != None: if tyype == Type.INT: cte_values_array[array_index] = int(cte_value * expr_cte_value) else: cte_values_array[array_index] *= expr_cte_value elif op == '/=' and cte_value != None and expr_cte_value != None: if tyype == Type.INT: cte_values_array[array_index] = int(cte_value / expr_cte_value) else: cte_values_array[array_index] /= expr_cte_value elif op == '++' and cte_value != None: cte_values_array[array_index] += 1 elif op == '--' and cte_value != None: cte_values_array[array_index] -= 1 self.ids_defined[name] = tyype, array_length, cte_values_array return
def visitVariable_assignment(self, ctx:GrammarParser.Variable_assignmentContext): if ctx.expression() != None: key = 0 if ctx.identifier() != None: key = 1 text = ctx.identifier().getText() token = ctx.identifier().IDENTIFIER().getPayload() if (text in GrammarCheckerVisitor.ids_defined) == True: tyype = GrammarCheckerVisitor.ids_defined[text] else: raise NameError("identifier '" + text + "' linha: "+ str(token.line) + " ,não foi definido ainda.") else: text = ctx.array().identifier().getText() token = ctx.array().identifier().IDENTIFIER().getPayload() if (text in GrammarCheckerVisitor.ids_defined) == True: tyype = GrammarCheckerVisitor.ids_defined[text] else: raise NameError("array '" + text + "'[] linha: "+ str(token.line) + " ,não foi definido ainda.") expr_type = self.visit(ctx.expression()) if (tyype == Type.INT and expr_type == Type.FLOAT) or (tyype == Type.FLOAT and expr_type == Type.INT): if key == 1: print("WARNING: identifier '" + text + "' linha: "+ str(token.line) + " ,pode perder informações, por está recebendo um tipo diferente do tipo do identificador.") else: print("WARNING: array '" + text + "'[] linha: "+ str(token.line) + " ,pode perder informações, por está recebendo um tipo diferente do tipo do array.") elif tyype == Type.VOID and (expr_type == Type.INT or expr_type == Type.FLOAT): GrammarCheckerVisitor.ids_defined[text] = GrammarCheckerVisitor.ids_undefined[text] tyype = GrammarCheckerVisitor.ids_defined[text] del GrammarCheckerVisitor.ids_undefined[text] if (tyype == Type.INT and expr_type == Type.FLOAT) or (tyype == Type.FLOAT and expr_type == Type.INT): if key == 1: print("WARNING: identifier '" + text + "' linha: "+ str(token.line) + " ,pode perder informações, por está recebendo um tipo diferente do tipo do identificador.") else: print("WARNING: array '" + text + "'[] linha: "+ str(token.line) + " ,pode perder informações, por está recebendo um tipo diferente do tipo do array.") else: if tyype != expr_type: if key == 1: raise NameError("identifier '" + text + "' linha: "+ str(token.line) + " ,está recebendo um tipo diferente do aceito") else: raise NameError("array '" + text + "' linha: "+ str(token.line) + " ,está recebendo um tipo diferente do aceito") elif ctx.expression() == None: if ctx.identifier != None: text = ctx.identifier().getText() token = ctx.identifier().IDENTIFIER().getPayload() if (text in GrammarCheckerVisitor.ids_defined) == True: tyype = GrammarCheckerVisitor.ids_defined[text] if tyype != Type.INT and tyype != Type.float: raise NameError("identifier '" + text + "' linha: "+ str(token.line) + " ,não possui tipo 'int' ou 'float'.") else: raise NameError("identifier '" + text + "' linha: "+ str(token.line) + " ,não foi definido ainda.") else: text = ctx.array().identifier().getText() token = ctx.array().identifier().IDENTIFIER().getPayload() if (text in GrammarCheckerVisitor.ids_defined) == True: tyype = GrammarCheckerVisitor.ids_defined[text] if tyype != Type.INT and tyype != Type.float: raise NameError("array '" + text + "'[] linha: "+ str(token.line) + " ,não possui tipo 'int' ou 'float'.") else: raise NameError("array '" + text + "'[] linha: "+ str(token.line) + " ,não foi definido ainda.")