示例#1
0
def p_print_quad(p):
    'print_quad : '
    print_content = current_program.operand_stack.pop()
    print_type = current_program.type_stack.pop()
    current_program.quad_number += quad_append(
        current_program,
        Quad(current_program.quad_number, 'PRINT', print_content, None, None))
示例#2
0
def p_special_string_quad(p):
    'special_string_quad :'
    temporary_string = current_program.mem.temporary_memory_assign(
        'string', p[-2])
    current_program.quad_number += quad_append(
        current_program,
        Quad(current_program.quad_number, p[-4], temporary_string, None, None))
示例#3
0
def evaluate_binary_operation_array_version(current_program):
    right_operand = current_program.operand_stack.pop()
    left_operand = current_program.operand_stack.pop()
    right_type = current_program.type_stack.pop()
    left_type = current_program.type_stack.pop()
    operator = current_program.operator_stack.pop()
    result_type = current_program.sem_cube.get_type(left_type, right_type,
                                                    operator)
    if result_type != 'error':
        new_temporary_address = current_program.mem.temporary_memory_assign(
            result_type)
        current_program.func_directory.new_temporary_variable(
            current_program.scope_l, result_type)
        #Generate quadruple with operands in the middle and result in the last space
        current_program.quad_number += quad_append(
            current_program,
            Quad(current_program.quad_number, operator, left_operand,
                 right_operand, new_temporary_address))
        new_list = []
        new_list.append(new_temporary_address)
        current_program.operand_stack.append(new_list)
        current_program.type_stack.append((result_type))
        return 1
    else:
        print('Operation Type Mismatch.')
        sys.exit()
示例#4
0
def p_print_string_quad(p):
    'print_string_quad : '
    temporary_string = current_program.mem.temporary_memory_assign(
        'string', p[-1])
    current_program.quad_number += quad_append(
        current_program,
        Quad(current_program.quad_number, 'PRINT', temporary_string, None,
             None))
示例#5
0
def goto_else_function(current_program):
    end = current_program.jump_stack.pop()
    #GOTO's are empty at creation
    current_program.quad_number += quad_append(
        current_program,
        Quad(current_program.quad_number, "GOTO", None, None, None))
    quad = current_program.quad_list[end]
    end = current_program.jump_stack.append(current_program.quad_number - 2)
    quad.set_quad_goto(current_program.quad_number)
示例#6
0
def p_goto_while_fill(p):
    'goto_while_fill :'
    end = current_program.jump_stack.pop()
    ret = current_program.jump_stack.pop()
    current_program.quad_number += quad_append(
        current_program,
        Quad(current_program.quad_number, "GOTO", None, None, ret))
    quad = current_program.quad_list[end]
    quad.set_quad_goto(current_program.quad_number)
def two_argument_string_special_function(current_program, element, string):
	current_type = current_program.type_stack.pop()
	#Only one of the variables is stored, the string is just passing by.
	if current_type == "int":
		operand = current_program.operand_stack.pop()
		current_program.quad_number += quad_append(current_program,
			   Quad(current_program.quad_number, element, operand, string, None))
	else:
		print("Special Functions parameter type mismatch.")
		sys.exit()
示例#8
0
def one_argument_special_function(current_program, element):
    #This will pop the expression for the argument and verify it is an int
    current_type = current_program.type_stack.pop()
    if current_type == "int":
        operand = current_program.operand_stack.pop()
        current_program.quad_number += quad_append(
            current_program,
            Quad(current_program.quad_number, element, operand, None, None))
    else:
        print("Special functions parameter type mismatch.")
        sys.exit()
示例#9
0
def gotof_quad_function(current_program):
	exp_type = current_program.type_stack.pop()
	#It is very important that this verification is here, otherwise it would just be a goto
	if exp_type == 'bool':
		result = current_program.operand_stack.pop()
		current_program.quad_number += quad_append(current_program,
			Quad(current_program.quad_number, 'GOTOF', result, None, None))
		current_program.jump_stack.append(current_program.quad_number - 2)
	else:
		print("Type mismatch at condition.")
		sys.exit()
示例#10
0
def gosub_quad(current_program, target):

    if not current_program.temporary_arg_types:
        current_program.quad_number += quad_append(
            current_program,
            Quad(current_program.quad_number, 'GOSUB', target, None,
                 current_program.func_directory.get_quad_number(target)))
        return 1
    else:
        print('Argument number mismatch.')
        sys.exit()
示例#11
0
def input_quad_function(current_program, element):
    #Although we do it often, it's important to note that these two functions will usually act as a way
    #to check both local and global context
    identifier = current_program.func_directory.get_function_variable(
        current_program.scope_l, element)
    if identifier is None:
        identifier = current_program.func_directory.get_function_variable(
            current_program.scope_g, element)
        if identifier is not None:
            #Type is a literal value in this quadruple
            current_program.quad_number += quad_append(
                current_program,
                Quad(current_program.quad_number, "READINPUT",
                     identifier['type'], None, identifier['address']))
        else:
            print("The variable " + element + " has not been declared.")
            sys.exit()
    else:
        current_program.quad_number += quad_append(
            current_program,
            Quad(current_program.quad_number, "READINPUT", identifier['type'],
                 None, identifier['address']))
def two_argument_special_function(current_program, element):
    current_type = current_program.type_stack.pop()
    current_type2 = current_program.type_stack.pop()
    #Since we are verifying two int exps, two verificaitions are in check
    if current_type == "int":
        if current_type2 == "int":
            operand = current_program.operand_stack.pop()
            operand2 = current_program.operand_stack.pop()
            current_program.quad_number += quad_append(
                current_program,
                Quad(current_program.quad_number, element, operand, operand2,
                     None))
        else:
            print("Special functions paramter type mismatch.")
            sys.exit()
示例#13
0
def quad_ver_function(current_program):
    a = 1
    argument_type = current_program.type_stack.pop()
    if argument_type == 'int':
        argument = current_program.operand_stack.pop()
        if current_program.scope_l == current_program.scope_g:
            current_variable = current_program.func_directory.get_function_variable(
                current_program.scope_g, current_program.current_array_id)
        else:
            current_variable = current_program.func_directory.get_function_variable(
                current_program.scope_l, current_program.current_array_id)
        if current_variable is not None:
            if current_variable['dimension_one'] != -1 and current_variable[
                    'dimension_two'] == -1:
                current_program.quad_number += quad_append(
                    current_program,
                    Quad(current_program.quad_number, 'VER', argument, 0,
                         current_variable['dimension_one']))
                current_program.operand_stack.append(argument)
                k_value = current_program.mem.constant_memory_assign('int', 0)
                current_program.operand_stack.append(k_value)
                current_program.type_stack.append('int')
                current_program.type_stack.append('int')
                current_program.operator_stack.append(
                    '+'
                )  #This command and the last 4 are for adding -k (0 in this case)
                current_program.quad_number += evaluate_binary_operation(
                    current_program)
                memory_sum = current_program.operand_stack.pop()
                base = current_program.operand_stack.pop()
                dir_base = current_program.mem.constant_memory_assign(
                    'int',
                    base)  #We store the base address in a variable to add it
                current_program.operand_stack.append(memory_sum)
                current_program.operand_stack.append(dir_base)
                current_program.operator_stack.append('+')
                current_program.quad_number += evaluate_binary_operation_array_version(
                    current_program)
                #Calls for a list insert into operand stack
            else:
                print("Variable " + current_program.current_id +
                      " is not an array.")
                sys.exit()

        else:
            print("Variable " + current_program.current_id +
                  " is not defined.")
            sys.exit()
示例#14
0
def return_from_function(current_program):
    return_value = current_program.operand_stack.pop()
    return_type = current_program.type_stack.pop()
    function = current_program.func_directory.get_function(
        current_program.scope_l)
    function_type = function['return_type']
    function_ret_address = function['return_address']
    if function_type == return_type:
        #This line should be properly providing the return address for the function (but it doesn't work at the moment)
        current_program.quad_number += quad_append(
            current_program,
            Quad(current_program.quad_number, 'RETURN', return_value, None,
                 function_ret_address))
    else:
        print("Function return type mismatch.")
        sys.exit()
示例#15
0
def evaluate_assignment(current_program):
    #Evaluates the assignment
    operator = current_program.operator_stack.pop()
    if operator == '=':
        right_operand = current_program.operand_stack.pop()
        right_type = current_program.type_stack.pop()
        left_operand = current_program.operand_stack.pop()
        left_type = current_program.type_stack.pop()
        result_type = current_program.sem_cube.get_type(
            left_type, right_type, operator)
        if result_type != 'error':
            #From this format, the value stored is the right_operand and the place it is stored is the left_operand
            quad = Quad(current_program.quad_number, operator, right_operand,
                        None, left_operand)
            current_program.quad_list.append(quad)
        else:
            print('Operation Type Mismatch.')
            sys.exit()
示例#16
0
def era_quad(current_program, element):

    if current_program.func_directory.get_function(element) is not None:
        return_address = current_program.func_directory.get_return_address(
            element)
        function_type = current_program.func_directory.get_function_type(
            element)
        current_program.operand_stack.append(
            return_address)  #These two lines were not in the first version
        current_program.type_stack.append(
            function_type)  # They insert the return line into the stack
        current_program.quad_number += quad_append(
            current_program,
            Quad(current_program.quad_number, 'ERA', element, None, None))
        function_parameters = current_program.func_directory.get_function_parameters(
            element)
        #This will help us verify that the sent types are of the correct type
        current_program.temporary_arg_types = list(
            function_parameters['parameter_types'])
        return 1
    else:
        print("Function " + element + " is not defined.")
        sys.exit()
示例#17
0
def evaluate_operation(current_program):
    right_operand = current_program.operand_stack.pop()
    right_type = current_program.type_stack.pop()
    left_operand = current_program.operand_stack.pop()
    left_type = current_program.type_stack.pop()
    operator = current_program.operator_stack.pop()
    result_type = current_program.semantic_cube.get_type(
        left_type, right_type, operator)
    if result_type != 'error':
        new_temporary = current_program.memory.request_temporal_address(
            result_type)
        current_program.function_directory.new_temporary_variable(
            current_program.scope_l, result_type)
        #Results in a quadruple that will solve operations with value in middle and store in the right
        quad = Quad(current_program.quad_number, operator, left_operand,
                    right_operand, new_temporary)
        current_program.quad_list.append(quad)
        print_to_output_file(quad)
        current_program.operand_stack.append(new_temporary)
        current_program.type_stack.append(result_type)
    else:
        print('Operation type mismatch.')
        sys.exit()
示例#18
0
def p_endproc_quad(p):
    'endproc_quad : '
    current_program.quad_number += quad_append(
        current_program,
        Quad(current_program.quad_number, "ENDPROC", None, None, None))
示例#19
0
def p_main_quad(p):
    'main_quad : '
    current_program.quad_number += quad_append(
        current_program,
        Quad(current_program.quad_number, "GOTO", "MAIN", None, None))
def no_argument_special_function(current_program, element):
    #No pops since it has no arguments
    current_program.quad_number += quad_append(
        current_program,
        Quad(current_program.quad_number, element, None, None, None))
示例#21
0
def quad_ver_two_function(current_program):
    a = 1
    s2 = 1
    s1 = 1

    s2_type = current_program.type_stack.pop()
    s1_type = current_program.type_stack.pop()
    if s2_type == 'int' and s1_type == 'int':
        s2 = current_program.operand_stack.pop()
        s1 = current_program.operand_stack.pop()
        if current_program.scope_l == current_program.scope_g:
            current_variable = current_program.func_directory.get_function_variable(
                current_program.scope_g, current_program.current_array_id)
        else:
            current_variable = current_program.func_directory.get_function_variable(
                current_program.scope_l, current_program.current_array_id)
        if current_variable is not None:
            if current_variable['dimension_one'] != -1:
                if current_variable['dimension_two'] != -1:
                    current_program.quad_number += quad_append(
                        current_program,
                        Quad(current_program.quad_number, 'VER', s1, 0,
                             current_variable['dimension_one']))
                    current_program.operand_stack.append(s1)
                    current_program.operand_stack.append(
                        current_program.mem.temporary_memory_assign(
                            'int',
                            current_variable['dimension_two']))  #S1 * d2
                    current_program.type_stack.append('int')
                    current_program.type_stack.append('int')
                    current_program.operator_stack.append('*')
                    current_program.quad_number += evaluate_binary_operation(
                        current_program)
                    current_program.quad_number += quad_append(
                        current_program,
                        Quad(current_program.quad_number, 'VER', s2, 0,
                             current_variable['dimension_two']))
                    current_program.operand_stack.append(s2)
                    current_program.type_stack.append('int')
                    current_program.type_stack.append('int')
                    current_program.operator_stack.append(
                        '+')  #Adds the second index (s2)
                    current_program.quad_number += evaluate_binary_operation(
                        current_program)
                    current_program.operand_stack.append(
                        current_program.operand_stack.pop())
                    k_value = current_program.mem.constant_memory_assign(
                        'int', 0)
                    current_program.operand_stack.append(k_value)
                    current_program.type_stack.append('int')
                    current_program.type_stack.append('int')
                    current_program.operator_stack.append(
                        '+'
                    )  #This command and the last 4 are for adding -k (0 in this case)
                    current_program.quad_number += evaluate_binary_operation(
                        current_program)
                    memory_sum = current_program.operand_stack.pop()
                    base = current_program.operand_stack.pop()
                    dir_base = current_program.mem.constant_memory_assign(
                        'int', base)
                    current_program.operand_stack.append(memory_sum)
                    current_program.operand_stack.append(dir_base)
                    current_program.operator_stack.append('+')
                    current_program.quad_number += evaluate_binary_operation_array_version(
                        current_program)
                    #Calls for a list insert into operand stack
                else:
                    print("Variable " + current_program.current_id +
                          " is not a matrix.")
                    sys.exit()
            else:
                print("Variable " + current_program.current_id +
                      " is not dimensioned.")
                sys.exit()

        else:
            print("Variable " + current_program.current_id +
                  " is not defined.")
            sys.exit()
示例#22
0
def goto_main_quad(quad_number, quad_list):
    print_to_output_file("\n\nINTERMEDIATE CODE:\n")
    current_quad = Quad(quad_number, 'GOTO', 'MAIN', None, None)
    quad_list.append(current_quad)
    print_to_output_file(current_quad)