Beispiel #1
0
def declarations():
    global line_number
    line_number = get_line_number()
    global synch_set
    global memory_counter
    line = tokens[0]

    token = peek_token()
    if token == 'var':
        match('var')
        token = tokens[0]
        match('id')
        match(':')
        the_type = type_()
        if type(the_type) is tuple:
            array_size = the_type[1]
            the_type = the_type[0]
        variable_types.update({token[1]: the_type})
        checking = nodes
        value_counter = 0
        func = set_function_name()
        if nodes:
            green_node = nodes[0]
            while green_node.right_sibling is not None:
                green_node = green_node.right_sibling
            if green_node.right_sibling is None:
                new_blue_node = BlueNode(token[1], the_type)
                memory_counter = 0
                new_blue_node.value = memory_counter
                if new_blue_node.w_type is 'integer':
                    memory_counter += 4
                elif new_blue_node.w_type is 'real':
                    memory_counter += 8
                elif new_blue_node.w_type == "a-real":
                    memory_counter += 8 * array_size
                elif new_blue_node.w_type == "a-integer":
                    memory_counter += 4 * array_size

                green_node.right_sibling = new_blue_node
                nodes.insert(0, green_node.right_sibling)
                write_to_memory_file(func, new_blue_node.data, new_blue_node.value)
        checking = nodes
        match(';')
        declarations_()
    else:
        # synch_set = ['function', 'begin']
        del synch_set[:]
        synch_set.append('var')
        synch_set.append('begin')
        synch_set.append('$')
        handle_sync()
        syntax_error(token, 'var', line_number)
Beispiel #2
0
def declarations_():
    line = tokens[0]
    token = peek_token()
    array_size = 0
    global line_number
    line_number = get_line_number()
    global memory_counter
    global variable_exist
    variable_exist = 0
    if token == 'begin':
        pass
    elif token == 'function':
        pass
    elif token == 'var':
        match('var')
        token = tokens[0]
        match('id')
        match(':')
        the_type = type_()
        if type(the_type) is tuple:
            array_size = the_type[1]
            the_type = the_type[0]
        variable_types.update({token[1]: the_type})
        current_nodes = nodes

        if nodes:
            node = nodes[0]
            while node.right_sibling is not None:
                node = node.right_sibling
            if node.right_sibling is None:
                new_blue_node = BlueNode(token[1], the_type)
                new_blue_node.value = memory_counter
                if new_blue_node.w_type == 'integer':
                    memory_counter += 4
                elif new_blue_node.w_type == 'real':
                    memory_counter += 8
                elif new_blue_node.w_type == "a-real":
                    memory_counter += 8 * array_size
                elif new_blue_node.w_type == "a-integer":
                    memory_counter += 4 * array_size
                node.right_sibling = new_blue_node
                nodes.insert(0, node.right_sibling)
                next_node = nodes[1]
                counter = 0
                current_n = new_blue_node
                while current_n.__class__.__name__ is not 'GreenNode':

                    if new_blue_node.data == next_node.data:
                        variable_exist = 1
                        counter += 1
                        current_n = nodes[counter]
                        next_node = nodes[counter]
                        continue

                    counter += 1
                    current_n = nodes[counter]
                    next_node = nodes[counter]
                write_to_memory_file('', new_blue_node.data, new_blue_node.value)
            if variable_exist == 1:
                duplicated_variable(line_number, new_blue_node.data)
            testing = nodes
        match(';')
        declarations_()
    else:
        # synch_set = ['function', 'begin']
        del synch_set[:]
        synch_set.append('function')
        synch_set.append('begin')
        handle_sync()
        syntax_error(token, 'begin', 'function', 'var')