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):
        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())
                print("EXPRESSÃO")
                print(len(ctx.expression(i).expression()))
                cte_value = None
                if len(ctx.expression(i).expression()) == 0:
                    print("TAMANHO ZERO")
                    if ctx.expression(i).integer() != None:
                        cte_value = int(ctx.expression(i).integer().getText())
                        print(cte_value)

                    elif ctx.expression(i).floating() != None:
                        cte_value = float(
                            ctx.expression(i).floating().getText())
                        print(cte_value)

                expr_type = 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")
            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 = 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))
            array_length = self.visit(ctx.array(i))
            self.ids_defined[name] = tyype, array_length

        return
Beispiel #4
0
    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
Beispiel #5
0
    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()
       # 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
Beispiel #7
0
    def visitVariable_definition(
            self, ctx: GrammarParser.Variable_definitionContext):
        tyype = ctx.tyype().getText()

        #attVarList serve para associar os valores que tem =
        attVarlist = []
        children = list(ctx.getChildren())

        for index in range(len(children)):
            if children[index].getText() == "=":
                attVarlist.append((children[index - 1], children[index + 1]))

        #checando os arrays
        for i in range(len(ctx.array())):
            array = ctx.array(i)
            arrayID = array.identifier().getText()
            token = array.identifier().IDENTIFIER().getPayload()

            self.ids_defined[arrayID] = tyype, None
            for attVar in attVarlist:
                self.visit(attVar[0])
                numberOfEltos = len(attVar[1].expression())
                index = self.ids_defined[arrayID][1]
                if index != None:
                    if numberOfEltos != index:
                        print(
                            "ERROR: incorret number of expressions assigned to array {} (given {}, expected {}) in line {} and column {}"
                            .format(arrayID, numberOfEltos, index, token.line,
                                    token.column))
                for i in range(numberOfEltos):
                    expr = attVar[1].expression(i)
                    exprType = self.visit(expr)
                    if exprType != tyype:
                        if exprType == Type.FLOAT and tyype == Type.INT:
                            print(
                                "WARNING: possible loss of information initializing 'float' expression to 'int' array '{}' at index {} of array literal in line {} and column {}"
                                .format(arrayID, i, token.line, token.column))
                        elif not (tyype == Type.FLOAT
                                  and exprType == Type.INT):
                            print(
                                "ERROR: trying to initialize '{}' expression to '{}' array '{}' at index {} of array literal in line {} and column {}"
                                .format(exprType, tyype, arrayID, i,
                                        token.line, token.column))

        #checando os normais
        for i in range(len(ctx.identifier())):
            name = ctx.identifier(i).getText()
            token = ctx.identifier(i).IDENTIFIER().getPayload()
            self.ids_defined[name] = ((tyype, token))

            for attVar in attVarlist:

                if attVar[0].getText() == name:
                    expr = attVar[1]
                    tyype_expression = self.visitExpression(expr)
                    tyype_variable = tyype

                    #print('expression value: ', expr.getText())
                    #print('expression type: ', tyype_expression)

                    if tyype_expression == None:
                        break

                    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
                          ) and 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)))
                    # elif(tyype_expression == Type.VOID):

                    break
Beispiel #8
0
    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
Beispiel #9
0
    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
Beispiel #10
0
    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
Beispiel #11
0
    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'.")