def visitVariable_definition( self, ctx: GrammarParser.Variable_definitionContext): for i in range(len(ctx.array())): text = ctx.array(i).identifier().getText() self.ids_defined[text] = ctx.tyype().getText() #print("Array Definition: ({},{})".format(text, self.ids_defined[text])) for i in range(len(ctx.identifier())): text = ctx.identifier(i).getText() token = ctx.identifier(i).IDENTIFIER().getPayload() self.ids_defined[text] = ctx.tyype().getText() #print("Variable Definition: ({},{})".format(text, self.ids_defined[text])) if ctx.expression(i) != None: expr_type = self.visitExpression(ctx.expression(i)) var_type = self.ids_defined.get(text, Type.VOID) if expr_type != var_type: if var_type == Type.FLOAT and expr_type == Type.INT: continue elif var_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))) else: print( "[ERROR]::[Do you know how to define a variable? You can't assign <{}> to <{}>, FYI.] ({},{})" .format(expr_type, var_type, str(token.line), str(token.column))) return self.visitChildren(ctx)
def visitVariable_definition( self, ctx: GrammarParser.Variable_definitionContext): tyype = ctx.tyype().getText() is_global = False cte_value = None for i in range(len(ctx.identifier())): name = ctx.identifier(i).getText() token = ctx.identifier(i).IDENTIFIER().getPayload() if self.inside_what_function == "": self.global_variables.append(name) print(self.global_variables) is_global = True if ctx.expression(i) != None: if is_global: expr_type = self.visit(ctx.expression(i)) else: expr_type, cte_value = self.visit(ctx.expression(i)) 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)) else: cte_value = None if is_global: self.ids_defined[name] = tyype, -1 else: self.ids_defined[ name] = tyype, -1, cte_value # -1 means not a array, therefore no length here (vide 15 lines below) for i in range(len(ctx.array())): name = ctx.array(i).identifier().getText() token = ctx.array(i).identifier().IDENTIFIER().getPayload() if ctx.array_literal(i) != None: expr_types, cte_values_array = self.visit(ctx.array_literal(i)) for j in range(len(expr_types)): if expr_types[j] == Type.VOID: print( "ERROR: trying to initialize void expression to array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column)) elif expr_types[j] == Type.FLOAT and tyype == Type.INT: print( "WARNING: possible loss of information initializing float expression to int array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column)) else: cte_values_array = None array_length = self.visit(ctx.array(i)) self.ids_defined[name] = tyype, array_length, cte_values_array return
def visitVariable_definition( self, ctx: GrammarParser.Variable_definitionContext): tyype = ctx.tyype().getText() ir_register = None # identifiers for i in range(len(ctx.identifier())): name = ctx.identifier(i).getText() token = ctx.identifier(i).IDENTIFIER().getPayload() if ctx.expression(i) != None: expr_type, cte_value, ir_register = self.visit( ctx.expression(i)) 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") else: # unitialized variables now get value 0 cte_value = 0 ir_register = None self.ids_defined[ name] = tyype, -1, cte_value, ir_register # -1 means not a array, therefore no length here (vide 15 lines below) # arrays for i in range(len(ctx.array())): name = ctx.array(i).identifier().getText() token = ctx.array(i).identifier().IDENTIFIER().getPayload() array_length, _ = self.visit(ctx.array(i)) if ctx.array_literal(i) != None: expr_types, cte_values_array, ir_registers_array = self.visit( ctx.array_literal(i)) for j in range(len(expr_types)): if expr_types[j] == Type.VOID: err("ERROR: trying to initialize void expression to array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) elif expr_types[j] == Type.FLOAT and tyype == Type.INT: err("WARNING: possible loss of information initializing float expression to int array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column) + "\n") else: # unitialized variables now get value 0 cte_values_array = [0] * array_length ir_registers_array = [None] * array_length self.ids_defined[ name] = tyype, array_length, cte_values_array, ir_registers_array return
def visitVariable_definition( self, ctx: GrammarParser.Variable_definitionContext): tyype = ctx.tyype().getText() for i in range(len(ctx.identifier())): name = ctx.identifier(i).getText() token = ctx.identifier(i).IDENTIFIER().getPayload() if ctx.expression(i) != None: # print(ctx.expression(i).getText()) expr_type, const_value = self.visit(ctx.expression(i)) 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)) # print("noExpr") else: const_value = None self.ids_defined[ name] = tyype, -1, const_value # -1 means not a array, therefore no length here (vide 15 lines below) for i in range(len(ctx.array())): name = ctx.array(i).identifier().getText() token = ctx.array(i).identifier().IDENTIFIER().getPayload() if ctx.array_literal(i) != None: expr_types, const_array_values = self.visit( ctx.array_literal(i)) for j in range(len(expr_types)): if expr_types[j] == Type.VOID or expr_types[ j] == Type.STRING: print("ERROR: trying to initialize '" + expr_types[j] + "' expression to '" + tyype + "' array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column)) elif expr_types[j] == Type.FLOAT and tyype == Type.INT: print( "WARNING: possible loss of information initializing float expression to int array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column)) else: const_array_values = None array_length = self.visit(ctx.array(i)) self.ids_defined[name] = tyype, array_length, const_array_values return
def visitVariable_definition(self, ctx:GrammarParser.Variable_definitionContext): tyype = ctx.tyype().getText() # print("VD") for i in range(len(ctx.identifier())): # ver quantas atribuicoes tem na linha daquele valor para n variaveis (identifiers) name = ctx.identifier(i).getText() #guarda o nome da variavel n token = ctx.identifier(i).IDENTIFIER().getPayload() #guarda a linha e coluna daquela variavel if ctx.expression(i) != None: #enquanto eu tiver uma definicao de variavel expr_type, cte_value = self.visit(ctx.expression(i)) #self retorna o tipo e NONE??? cte_value = ctx.expression(i).getText() 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)) else: cte_value = None self.ids_defined[name] = tyype, -1, self.checkDic(name, cte_value) # -1 means not an array, therefore no length here (vide 15 lines below) for i in range(len(ctx.array())): #talvez aqui eu so mexa aqui name = ctx.array(i).identifier().getText() token = ctx.array(i).identifier().IDENTIFIER().getPayload() cte_value = ctx.array(i).getText() if ctx.array_literal(i) != None: expr_types, cte_values_array = self.visit(ctx.array_literal(i)) for j in range(len(expr_types)): if expr_types[j] == Type.VOID: print("ERROR: trying to initialize void expression to array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column)) elif expr_types[j] == Type.FLOAT and tyype == Type.INT: print("WARNING: possible loss of information initializing float expression to int array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column)) else: cte_values_array = None array_length = self.visit(ctx.array(i)) self.ids_defined[name] = tyype, array_length, cte_values_array # print(cte_values_array) #CORRIGIDO return
def visitVariable_definition( self, ctx: GrammarParser.Variable_definitionContext): for i in range(len(ctx.identifier())): variable_name = ctx.identifier(i).getText() variable_type = ctx.tyype().getText() expression_type = self.visitExpression(ctx.expression(i)) self.ids_defined[variable_name] = ctx.tyype().getText() token = ctx.identifier(i).IDENTIFIER().getPayload() if (variable_type == Type.INT and expression_type == Type.FLOAT): print( f"WARNING: possible loss of information assigning float expression to int variable '{variable_name}' in line {token.line} and column {token.column}" ) elif (expression_type == Type.STRING and variable_type != Type.STRING): print( f"ERROR: trying to assign '{expression_type}' expression to variable '{variable_name}' in line {token.line} and column {token.column}" ) elif (expression_type == Type.VOID and variable_type != Type.VOID): print( f"ERROR: trying to assign '{expression_type}' expression to variable '{variable_name}' in line {token.line} and column {token.column}" ) for i in range(len(ctx.array())): variable_name = ctx.array(i).identifier().getText() variable_type = ctx.tyype().getText() self.ids_defined[variable_name] = variable_type token = ctx.array(i).identifier().IDENTIFIER().getPayload() if (len(ctx.array_literal()) != 0): for j in range(len(ctx.array_literal(i).expression())): expression_type = self.visitExpression( ctx.array_literal(i).expression(j)) if (variable_type != expression_type): if (expression_type == Type.STRING): print( f"ERROR: trying to initialize '{expression_type}' expression to '{variable_type}' array '{variable_name}' at index {j} of array literal in line {token.line} and column {token.column}" ) elif (variable_type == Type.INT and expression_type == Type.FLOAT): print( f"WARNING: possible loss of information initializing {expression_type} expression to {variable_type} array '{variable_name}' at index {j} of array literal in line {token.line} and column {token.column}" ) elif (expression_type == Type.VOID): print( f"ERROR: trying to assign '{variable_type}' expression to array variable '{variable_name}' at index {j} in line {token.line} and column {token.column}" ) return
def visitVariable_definition( self, ctx: GrammarParser.Variable_definitionContext): tyype = ctx.tyype().getText() for i in range(len(ctx.identifier())): name = ctx.identifier(i).getText() token = ctx.identifier(i).IDENTIFIER().getPayload() if ctx.expression(i) != None: #print(ctx.expression(i).getText()) expr_type, expr_value, expr_is_constant = self.visit( ctx.expression(i)) 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)) #se ele nao estiver em nenhuma função a variavel nao é constante if self.inside_what_function == "": expr_is_constant = False self.ids_defined[ name] = tyype, -1, expr_value, expr_is_constant else: self.ids_defined[ name] = tyype, -1, None, None # -1 means not a array, therefore no length here (vide 15 lines below) for i in range(len(ctx.array())): name = ctx.array(i).identifier().getText() token = ctx.array(i).identifier().IDENTIFIER().getPayload() expr_eltos = [] array_length = self.visit(ctx.array(i)) if ctx.array_literal(i) != None: expr_eltos = self.visit(ctx.array_literal(i)) for j in range(len(expr_eltos)): if expr_eltos[j] == Type.VOID or expr_eltos[ j] == Type.STRING: print("ERROR: trying to initialize '" + expr_eltos[j] + "' expression to '" + tyype + "' array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column)) elif expr_eltos[j] == Type.FLOAT and tyype == Type.INT: print( "WARNING: possible loss of information initializing float expression to int array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column)) else: for i in range(array_length): expr_eltos.append(('', None, True)) self.ids_defined[name] = tyype, array_length, expr_eltos #print(self.ids_defined[name]) #print(self.ids_defined) return
def visitVariable_definition( self, ctx: GrammarParser.Variable_definitionContext): tyype = ctx.tyype().getText() ir_register = None # identifiers for i in range(len(ctx.identifier())): name = ctx.identifier(i).getText() token = ctx.identifier(i).IDENTIFIER().getPayload() if ctx.expression(i) != None: if self.inside_what_function: f.write("\t%" + name + " = alloca " + llvm_type(tyype) + ", align 4\n") expr_type, cte_value, ir_register = self.visit( ctx.expression(i)) if tyype == Type.FLOAT and cte_value: f.write("\tstore " + llvm_type(tyype) + " " + float_to_hex(cte_value) + ", " + llvm_type(tyype) + "* %" + name + ", align 4\n") elif cte_value: f.write("\tstore " + llvm_type(tyype) + " " + str(int(cte_value)) + ", " + llvm_type(tyype) + "* %" + name + ", align 4\n") elif ir_register: f.write("\tstore " + llvm_type(tyype) + " %" + str(ir_register) + ", " + llvm_type(tyype) + "* %" + name + ", align 4\n") 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") else: expr_type, cte_value, ir_register = self.visit( ctx.expression(i)) self.global_vars.append(name) if expr_type == Type.FLOAT: f.write("@" + name + " = global " + llvm_type(expr_type) + " " + float_to_hex(cte_value) + "\n") else: f.write("@" + name + " = global " + llvm_type(expr_type) + " " + str(cte_value) + "\n") pass else: # unitialized variables now get value 0 cte_value = 0 ir_register = self.next_ir_register self.next_ir_register += 1 self.ids_defined[ name] = tyype, -1, cte_value, ir_register # -1 means not a array, therefore no length here (vide 15 lines below) # arrays for i in range(len(ctx.array())): name = ctx.array(i).identifier().getText() token = ctx.array(i).identifier().IDENTIFIER().getPayload() array_length, _ = self.visit(ctx.array(i)) if ctx.array_literal(i) != None: expr_types, cte_values_array, ir_registers_array = self.visit( ctx.array_literal(i)) for j in range(len(expr_types)): if expr_types[j] == Type.VOID: err("ERROR: trying to initialize void expression to array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) elif expr_types[j] == Type.FLOAT and tyype == Type.INT: err("WARNING: possible loss of information initializing float expression to int array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column) + "\n") else: # unitialized variables now get value 0 cte_values_array = [0] * array_length ir_registers_array = [None] * array_length self.ids_defined[ name] = tyype, array_length, cte_values_array, ir_registers_array return
def visitVariable_definition( self, ctx: GrammarParser.Variable_definitionContext): tyype = ctx.tyype().getText() llvm_tyype = llvm_type(tyype) ir_register = None is_inside = self.inside_what_function != "" # identifiers for i in range(len(ctx.identifier())): name = ctx.identifier(i).getText() token = ctx.identifier(i).IDENTIFIER().getPayload() if is_inside: printAlloca(name, llvm_tyype, 4, is_inside) if ctx.expression(i) != None: expr_type, cte_value, ir_register = self.visit( ctx.expression(i)) llvm_expr_type = llvm_type(expr_type) 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 not is_inside: cte_value_str = str(cte_value) if expr_type == Type.FLOAT: cte_value_str = float_to_hex(float(cte_value)) global_vars.append(name) arquivo.write("@" + name + " = global " + llvm_type(tyype) + " " + cte_value_str + "\n") elif cte_value != None: cte_value_str = str(cte_value) if expr_type == Type.FLOAT or tyype == Type.FLOAT: cte_value_str = float_to_hex(float(cte_value)) printStore(cte_value_str, llvm_tyype, name, llvm_tyype, 4, is_inside, True) else: printStore(ir_register, tyype, name, expr_type, 4, is_inside, False) else: # unitialized variables now get value 0 cte_value = 0 ir_register = None if not is_inside: cte_value = None self.ids_defined[ name] = tyype, -1, cte_value, ir_register # -1 means not a array, therefore no length here (vide 15 lines below) # arrays for i in range(len(ctx.array())): name = ctx.array(i).identifier().getText() token = ctx.array(i).identifier().IDENTIFIER().getPayload() array_length, _ = self.visit(ctx.array(i)) if ctx.array_literal(i) != None: expr_types, cte_values_array, ir_registers_array = self.visit( ctx.array_literal(i)) for j in range(len(expr_types)): if expr_types[j] == Type.VOID: err("ERROR: trying to initialize void expression to array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) elif expr_types[j] == Type.FLOAT and tyype == Type.INT: err("WARNING: possible loss of information initializing float expression to int array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column) + "\n") else: # unitialized variables now get value 0 cte_values_array = [0] * array_length ir_registers_array = [None] * array_length self.ids_defined[ name] = tyype, array_length, cte_values_array, ir_registers_array return
def visitVariable_definition( self, ctx: GrammarParser.Variable_definitionContext): function_type = ctx.tyype().getText() for i in range(len(ctx.identifier())): name = ctx.identifier(i).getText() token = ctx.identifier(i).IDENTIFIER().getPayload() if ctx.expression(i) != None: expr_type, cte_value = self.visit(ctx.expression(i)) 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 function_type == 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)) elif expr_type == Type.STRING and (function_type == Type.INT or function_type == Type.FLOAT): print( "ERROR: trying to initialize 'char *' expression to '" + name + "' array in line " + str(token.line) + " and column " + str(token.column)) else: cte_value = None self.ids_defined[name] = function_type, -1, cte_value for i in range(len(ctx.array())): name = ctx.array(i).identifier().getText() token = ctx.array(i).identifier().IDENTIFIER().getPayload() #array_length, _ = self.visit(ctx.array(i)) if ctx.array_literal(i) != None: expr_types, cte_values_array = self.visit(ctx.array_literal(i)) for j in range(len(expr_types)): if expr_types[j] == Type.VOID: print( "ERROR: trying to initialize void expression to array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column)) elif expr_types[ j] == Type.FLOAT and function_type == Type.INT: print( "WARNING: possible loss of information initializing float expression to int array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column)) elif expr_types[j] == Type.STRING and ( function_type == Type.INT or function_type == Type.FLOAT): print( "ERROR: trying to initialize 'char *' expression to '" + name + "' array at index '" + str(j) + "' of array literal in line " + str(token.line) + " and column " + str(token.column)) else: cte_values_array = None array_length = self.visit(ctx.array(i)) self.ids_defined[ name] = function_type, array_length, cte_values_array return
def visitVariable_definition(self, ctx:GrammarParser.Variable_definitionContext): tyype = ctx.tyype().getText() ir_register = None # identifiers is_global = self.inside_what_function=='' for i in range(len(ctx.identifier())): name = ctx.identifier(i).getText() token = ctx.identifier(i).IDENTIFIER().getPayload() if is_global: #print(name) self.global_var += [name] #%a = alloca i32, align 4 if ctx.expression(i) != None: if not is_global: print(' ' +'%'+name+ ' = alloca ' + str(llvm_type(tyype)) + ', align 4') expr_type, cte_value, ir_register = self.visit(ctx.expression(i)) if is_global: print('@'+name+'= global '+ str(llvm_type(expr_type))+' '+str(cte_value)) if expr_type == Type.FLOAT: if(cte_value != None): if not is_global: print(' ' +'store ' + str(llvm_type(expr_type)) + ' ' +str(float_to_hex(cte_value)) + ', '+ str(llvm_type(expr_type)) + '* %' + name+ ', align 4') else: if (self.func_count[self.inside_what_function] == None): self.func_count[self.inside_what_function] = 1 if not is_global: print(' ' +'store ' + str(llvm_type(expr_type)) + ' %' + str(self.func_count[self.inside_what_function]) + ', '+ str(llvm_type(expr_type)) + '* %' + name+ ', align 4') #store float %1, float* %k, align 4 else: if not is_global: print(' ' +'store ' + str(llvm_type(expr_type)) + ' ' + str(cte_value) + ', '+ str(llvm_type(expr_type)) + '* %' + name+ ', align 4') 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") else: # unitialized variables now get value 0 cte_value = 0 ir_register = None self.ids_defined[name] = tyype, -1, cte_value, ir_register # -1 means not a array, therefore no length here (vide 15 lines below) # arrays for i in range(len(ctx.array())): name = ctx.array(i).identifier().getText() token = ctx.array(i).identifier().IDENTIFIER().getPayload() array_length, _ = self.visit(ctx.array(i)) if ctx.array_literal(i) != None: expr_types, cte_values_array, ir_registers_array = self.visit(ctx.array_literal(i)) for j in range(len(expr_types)): if expr_types[j] == Type.VOID: err("ERROR: trying to initialize void expression to array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column) + "\n") exit(-1) elif expr_types[j] == Type.FLOAT and tyype == Type.INT: err("WARNING: possible loss of information initializing float expression to int array '" + name + "' at index " + str(j) + " of array literal in line " + str(token.line) + " and column " + str(token.column) + "\n") else: # unitialized variables now get value 0 cte_values_array = [0] * array_length ir_registers_array = [None] * array_length self.ids_defined[name] = tyype, array_length, cte_values_array, ir_registers_array print(" ") return
def visitVariable_definition(self, ctx:GrammarParser.Variable_definitionContext): for i in range(len(ctx.identifier())): if ctx.expression(i) != None: expr_type = self.visit(ctx.expression(i)) tyype = ctx.tyype().getText() text = ctx.identifier(i).getText() token = ctx.identifier(i).IDENTIFIER().getPayload() if tyype == expr_type: if (text in GrammarCheckerVisitor.ids_defined) == False: GrammarCheckerVisitor.ids_defined[text] = tyype else: raise NameError("identifier '" + text + "' linha: "+ str(token.line) + " ,já foi definido anteriormente.") elif (tyype == Type.INT and expr_type == Type.FLOAT) or (tyype == Type.FLOAT and expr_type == Type.INT): print("WARNING: identifier '" + text + "' linha: "+ str(token.line) + " ,pode perder informações, por está recebendo um tipo diferente do tipo do identificador.") if (text in GrammarCheckerVisitor.ids_defined) == False: GrammarCheckerVisitor.ids_defined[text] = tyype else: raise NameError("identifier '" + text + "' linha: "+ str(token.line) + " ,já foi definido anteriormente.") else: raise NameError("identifier '" + text + "' linha: "+ str(token.line) + " ,está recebendo um que tipo que não é aceito") else: text = ctx.identifier(i).getText() tyype = ctx.tyype().getText() token = ctx.identifier(i).IDENTIFIER().getPayload() if (text in GrammarCheckerVisitor.ids_defined) == False: GrammarCheckerVisitor.ids_defined[text] = Type.VOID GrammarCheckerVisitor.ids_undefined[text] = tyype else: raise NameError("identifier '" + text + "' linha: "+ str(token.line) + " ,já foi definido anteriormente.") for i in range(len(ctx.array())): if ctx.array_literal(i) != None: index_type = self.visit(ctx.array(i).expression()) array_tyype = ctx.tyype().getText() text = ctx.array(i).identifier().getText() token = ctx.array(i).identifier().IDENTIFIER().getPayload() if index_type == Type.INT: key = 0 for j in range(len(ctx.array_literal(i).expression())): expr_type = self.visit(ctx.array_literal(i).expression(j)) if expr_type != array_tyype: key = 1 if expr_type != Type.INT and expr_type != Type.FLOAT: key = 2 if key == 0: if (text in GrammarCheckerVisitor.ids_defined) == False: GrammarCheckerVisitor.ids_defined[text] = array_tyype else: raise NameError("array '" + text + "'[] linha: "+ str(token.line) + " ,já definido anteriormente.") elif key == 1: print("WARNING: identifier '" + text + "'[] linha: "+ str(token.line) + " ,pode perder informações, por está recebendo um tipo diferente do tipo do array.") if (text in GrammarCheckerVisitor.ids_defined) == False: GrammarCheckerVisitor.ids_defined[text] = array_tyype else: raise NameError("array '" + text + "'[] linha: "+ str(token.line) + " ,já definido anteriormente.") else: raise NameError("array '" + text + "'[] linha: "+ str(token.line) + " ,está recebendo um tipo diferente do tipo do array") else: raise NameError("array '" + text + "'[] linha: "+ str(token.line) + " ,não possui índice do tipo 'int'.") else: index_type = self.visit(ctx.array(i).expression()) text = ctx.array(i).identifier().getText() array_tyype = ctx.tyype().getText() token = ctx.array(i).identifier().IDENTIFIER().getPayload() if index_type == Type.INT: if (text in GrammarCheckerVisitor.ids_defined) == False: GrammarCheckerVisitor.ids_defined[text] = Type.VOID GrammarCheckerVisitor.ids_undefined[text] = array_tyype else: raise NameError("array '" + text + "'[] linha: "+ str(token.line) + " ,já definido anteriormente.") else: raise NameError("array '" + text + "'[] linha: "+ str(token.line) + " ,não possui índice do tipo 'int'.")