Esempio n. 1
0
    def declare_var(self):
        """
        Declare variables
        """
        self.match("TK_VAR")
        variables_initialized = []

        while self.curr_t[1] == "TK_ID":
            if self.curr_t not in variables_initialized:
                variables_initialized.append(self.curr_t)
                self.match("TK_ID")

                if self.curr_t[1] == "TK_COMMA":
                    self.match("TK_COMMA")
            else:
                raise NameError("Variable already declared" + self.curr_t[1])

        self.match("TK_COLON")

        data_type = Aux.get_type(self.curr_t[1])
        self.match(data_type)

        if data_type == "TK_ARRAY":
            self.match("TK_LEFT_BRACKET")
            access_type, lower_bound, upper_bound = self.range_array(self.curr_t)
            self.match("TK_RANGE")
            self.match("TK_RIGHT_BRACKET")
            self.match("TK_OF")

            if self.curr_t[1] == "TK_INTEGER":
                assignment_type = self.curr_t[1]
                self.match("TK_INTEGER")
            elif self.curr_t[1] == "TK_REAL":
                assignment_type = self.curr_t[1]
                self.match("TK_REAL")
            elif self.curr_t[1] == "TK_CHAR":
                assignment_type = self.curr_t[1]
                self.match("TK_CHAR")
            else:
                raise TypeError("Unsupported types: " + self.curr_t[1])
            self.match("TK_SEMICOLON")

            for variable in variables_initialized:
                array_symb = Symbol(variable[0], 'TK_ARRAY', 'ARRAY', self.data_indicator)
                array_symb.access_type = access_type
                array_symb.lower_bound = lower_bound
                array_symb.upper_bound = upper_bound
                array_symb.assignment_type = assignment_type
                self.symbol_table.append(array_symb)
                self.data_indicator += 4 * int(upper_bound) - int(lower_bound)

        else:
            self.match("TK_SEMICOLON")
            for variable in variables_initialized:
                variable_symb = Symbol(variable[0], data_type, "VARIABLE", self.data_indicator)
                self.data_indicator += 1
                self.symbol_table.append(variable_symb)
            self.data_indicator += 1

        if self.curr_t[1] == "TK_VAR":
            self.declare_var()
        else:
            self.begin()