Пример #1
0
def p_list_point3(p):
    '''list_point3 : '''
    index_address = my_code.operand_list.pop()
    index_type = my_code.type_list.pop()
    dimensioned_variable = my_code.list_variable_list.pop()
    if index_type != 'int':
        print("Array indexes should be of type int")
        sys.exit()
    else:
        quadruple = Quadruple(my_code.quadruple_number, 'VERF_INDEX',
                              index_address,
                              dimensioned_variable['lower_limit'],
                              dimensioned_variable['upper_limit'])
        my_code.quadruple_list.append(quadruple)
        my_code.quadruple_number += 1
        base_address_proxy = my_code.memory_request.get_global_address(
            'int', dimensioned_variable['memory_address'])
        index_address_result = my_code.memory_request.get_global_address('int')
        quadruple = Quadruple(my_code.quadruple_number, '+',
                              base_address_proxy, index_address,
                              index_address_result)
        my_code.quadruple_list.append(quadruple)
        my_code.quadruple_number += 1
        result_proxy = {'index_address': index_address_result}
        my_code.operand_list.append(result_proxy)
        my_code.type_list.append(dimensioned_variable['type'])
Пример #2
0
def p_else_point(p):
    '''else_point : '''
    quadruple = Quadruple(my_code.quadruple_number, 'GOTO', None, None, None)
    my_code.quadruple_list.append(quadruple)

    quadruple_number_to_fill = my_code.jump_list.pop()
    quadruple = my_code.quadruple_list[quadruple_number_to_fill]

    my_code.jump_list.append(my_code.quadruple_number - 1)
    my_code.quadruple_number += 1

    quadruple.fill_quadruple_jump(my_code.quadruple_number)
Пример #3
0
def p_write_point(p):
    '''write_point : '''
    operand = my_code.operand_list.pop()
    quadruple = Quadruple(my_code.quadruple_number, 'PRINT', operand, None,
                          None)
    my_code.quadruple_list.append(quadruple)
    my_code.quadruple_number += 1
Пример #4
0
def p_functions_point2(p):
    '''functions_point2 : '''
    function_type = p[-7]

    if function_type == 'void' and my_code.return_flag:
        print('Function {0} of type void should not have return statement.'.
              format(my_code.current_scope))
        sys.exit()
    elif function_type != 'void' and not my_code.return_flag:
        print('Function {0} of type {1} should have return statement.'.format(
            my_code.current_scope, function_type))
        sys.exit()
    else:
        quadruple = Quadruple(my_code.quadruple_number, 'ENDFUNC', None, None,
                              None)
        my_code.quadruple_list.append(quadruple)

    if my_code.return_flag:
        while my_code.return_list:
            quadruple_number_to_fill = my_code.return_list.pop()
            my_code.quadruple_list[quadruple_number_to_fill -
                                   1].fill_quadruple_jump(
                                       my_code.quadruple_number)
    my_code.quadruple_number += 1
    my_code.return_flag = False

    my_code.current_scope = my_code.global_scope
    my_code.memory_request.restart_memory()
Пример #5
0
def p_loop_point3(p):
    '''loop_point3 : '''
    quadruple_number_to_fill = my_code.jump_list.pop()
    quadruple_number_to_return = my_code.jump_list.pop()

    while_quadruple = Quadruple(my_code.quadruple_number, 'GOTO', None, None,
                                quadruple_number_to_return)
    my_code.quadruple_list.append(while_quadruple)
    my_code.quadruple_number += 1

    conditional_quadruple = my_code.quadruple_list[quadruple_number_to_fill]
    conditional_quadruple.fill_quadruple_jump(my_code.quadruple_number)
Пример #6
0
def p_function_point4(p):
    '''function_point4 : '''
    if not my_code.temporal_arguments_types:
        function = p[-7]
        function_quadruple_number = my_code.function_directory.get_function_quadruple_number(
            function)

        quadruple = Quadruple(my_code.quadruple_number, 'GOSUB', function,
                              None, function_quadruple_number)
        my_code.quadruple_list.append(quadruple)
        my_code.quadruple_number += 1
    else:
        print('Argument number mismatch at {0} line '.format(p.lexer.lineno))
        sys.exit()
Пример #7
0
def p_assignment_point3(p):
    '''assignment_point3 : '''
    received_input_address = my_code.operand_list.pop()
    my_code.type_list.pop()
    variable_type = my_code.type_list[-1]
    assignation_address = my_code.memory_request.get_temporal_address(
        variable_type)

    my_code.operand_list.append(assignation_address)
    my_code.type_list.append(variable_type)
    quadruple = Quadruple(my_code.quadruple_number, 'READ', variable_type,
                          received_input_address, assignation_address)
    my_code.quadruple_list.append(quadruple)
    my_code.quadruple_number += 1
Пример #8
0
def p_return_point(p):
    '''return_point : '''
    my_code.return_flag = True
    operand = my_code.operand_list.pop()
    operand_type = my_code.type_list.pop()
    function = my_code.function_directory.get_function(my_code.current_scope)
    function_type = function['return_type']
    function_return_address = function['return_address']

    if function_type != operand_type:
        print("Return type of function {0} doesn't match function return type".
              format(my_code.current_scope))
        sys.exit()

    quadruple = Quadruple(my_code.quadruple_number, 'RETURN', operand, None,
                          function_return_address)
    my_code.quadruple_list.append(quadruple)
    my_code.quadruple_number += 1

    quadruple = Quadruple(my_code.quadruple_number, 'GOTO', None, None, None)
    my_code.return_list.append(my_code.quadruple_number)
    my_code.quadruple_list.append(quadruple)
    my_code.quadruple_number += 1
Пример #9
0
def p_function_point2(p):
    '''function_point2 : '''
    function = p[-3]
    if my_code.function_directory.search_function(function):
        quadruple = Quadruple(my_code.quadruple_number, 'ERA', function, None,
                              None)
        my_code.quadruple_list.append(quadruple)
        my_code.quadruple_number += 1

        parameters = my_code.function_directory.get_function_parameters(
            function)
        my_code.temporal_arguments_types = list(parameters['types'])
    else:
        print("The function " + function +
              " you are trying to call doesn't exists")
        sys.exit()
Пример #10
0
def p_args_point(p):
    '''args_point : '''
    if my_code.temporal_arguments_types:
        argument = my_code.operand_list.pop()
        argument_type = my_code.type_list.pop()
        parameter_type = my_code.temporal_arguments_types.pop(0)

        if argument_type == parameter_type:
            quadruple = Quadruple(my_code.quadruple_number, 'PARAMETER',
                                  argument, None, None)
            my_code.quadruple_list.append(quadruple)
            my_code.quadruple_number += 1
        else:
            print('Argument type mismatch at {0} line '.format(p.lexer.lineno))
            sys.exit()
    else:
        print('Agument number mismatch at {0} line '.format(p.lexer.lineno))
        sys.exit()
Пример #11
0
def p_assignment_point4(p):
    '''assignment_point4 : '''
    operator = my_code.operator_list.pop()
    if operator == '=':
        right_operand = my_code.operand_list.pop()
        right_type = my_code.type_list.pop()
        left_operand = my_code.operand_list.pop()
        left_type = my_code.type_list.pop()
        result_type = my_code.semantic_cube.get_semantic_type(
            left_type, right_type, operator)
        if result_type != 'error':
            quadruple = Quadruple(my_code.quadruple_number, operator,
                                  right_operand, None, left_operand)

            my_code.quadruple_list.append(quadruple)
            my_code.quadruple_number += 1
        else:
            print('Operation type mismatch at {0}'.format(p.lexer.lineno))
            sys.exit()
Пример #12
0
def p_function_point5(p):
    '''function_point5 : '''
    function_called = p[-8]
    function = my_code.function_directory.get_function(function_called)
    function_return = function['return_address']
    function_type = function['return_type']

    #my_code.temporal_variable_counter += 1

    temporal_variable_address = my_code.memory_request.get_temporal_address(
        function_type)
    my_code.function_directory.add_temporal_to_function(
        my_code.current_scope, function_type)

    quadruple = Quadruple(my_code.quadruple_number, '=', function_return, None,
                          temporal_variable_address)
    my_code.quadruple_list.append(quadruple)
    my_code.quadruple_number += 1

    my_code.operand_list.append(temporal_variable_address)
    my_code.type_list.append(function_type)
Пример #13
0
def solve_operation(p):
    """Solve an operation from the lists"""
    right_operand = my_code.operand_list.pop()
    right_type = my_code.type_list.pop()
    left_operand = my_code.operand_list.pop()
    left_type = my_code.type_list.pop()
    operator = my_code.operator_list.pop()
    result_type = my_code.semantic_cube.get_semantic_type(
        left_type, right_type, operator)
    if result_type != 'error':
        temporal_variable_address = my_code.memory_request.get_temporal_address(
            result_type)
        my_code.function_directory.add_temporal_to_function(
            my_code.current_scope, result_type)
        quadruple = Quadruple(my_code.quadruple_number, operator, left_operand,
                              right_operand, temporal_variable_address)
        my_code.quadruple_list.append(quadruple)
        my_code.quadruple_number += 1
        my_code.operand_list.append(temporal_variable_address)
        my_code.type_list.append(result_type)
    else:
        print('Operation type mismatch at {0}'.format(p.lexer.lineno))
        sys.exit()
Пример #14
0
def create_conditional_quadruple(p):
    quadruple = Quadruple(my_code.quadruple_number, 'GOTOF', None, None, None)
    my_code.quadruple_list.append(quadruple)
    my_code.jump_list.append(my_code.quadruple_number - 1)
    my_code.quadruple_number += 1
Пример #15
0
def p_program_point(p):
    '''program_point : '''
    quadruple = Quadruple(my_code.quadruple_number, 'GOTO', 'MAIN', None, None)
    my_code.quadruple_list.append(quadruple)
    my_code.quadruple_number += 1