Exemple #1
0
def p_procedure(p):
    """procedure        : PROCEDURE function_sign LBRACE qis_1 declarations_list block RBRACE SEMICOLON
                        | PROCEDURE function_sign LBRACE qis_1 block RBRACE SEMICOLON"""
    if p[len(p) - 3] and "exit_when_quad_index" in p[len(p) - 3]:
        raise CompilationException(
            "exit when just allowed in loops!!! function: " + p[2]["place"],
            p.slice[2])
    procedure = p[2]
    p[0] = procedure

    # goto return statements
    exit_procedure_statements_quad_index = code_array.get_next_quad_index()
    return_address_variable = symbol_table.get_new_temp_variable("void*")
    code_array.emit("pop", return_address_variable, None, None)
    code_array.emit("short jump", None, return_address_variable, None)
    begin_procedure_statements_quad_index = code_array.get_next_quad_index()

    # backpatch qis_1 with begin proc statements
    qis_1 = p[4]
    code_array.backpatch_e_list(qis_1["goto_quad_index"],
                                begin_procedure_statements_quad_index)
    # load arguments
    for parameter in procedure["parameters"]:
        code_array.emit("pop", parameter, None, None)
    # goto beginning of proc
    code_array.emit("goto", None, qis_1["quad_index"], None)
    symbol_table.pop_scope()
    return
Exemple #2
0
def p_statement_do_while(p):
    """statement            : DO qis statement WHILE bool_expressions"""
    code_array.backpatch_e_list(p[5]["t_list"], p[2]["quad_index"])
    code_array.backpatch_e_list(p[5]["f_list"],
                                code_array.get_next_quad_index())
    if p[3] and "exit_when_quad_index" in p[3]:
        code_array.backpatch_e_list(p[3]["exit_when_quad_index"],
                                    code_array.get_next_quad_index())
    return
Exemple #3
0
def p_statement_if(p):
    """statement            : IF bool_expressions THEN qis statement
                            | IF bool_expressions THEN qis statement ELSE qis_1 statement"""
    code_array.backpatch_e_list(p[2]["t_list"], p[4]["quad_index"])
    if len(p) == 6:
        code_array.backpatch_e_list(p[2]["f_list"],
                                    code_array.get_next_quad_index())
    else:
        code_array.backpatch_e_list(p[2]["f_list"], p[7]["quad_index"])
        code_array.backpatch_e_list(p[7]["goto_quad_index"],
                                    code_array.get_next_quad_index())
    return
Exemple #4
0
def p_statement_for(p):
    """statement            : FOR ID ASSIGNMENT_SIGN counter DO qis_1 statement"""
    symbol_table.check_variable_declaration(p[2], p.slice[2])
    code_array.check_variable_is_not_array(p[2], p.slice[2])
    code_array.emit(p[4]["opt"], p[2], p[2], {"value": 1, "type": "int"})
    code_array.emit("goto", None, code_array.get_next_quad_index() + 2, None)
    code_array.backpatch_e_list(p[6]["goto_quad_index"],
                                code_array.get_next_quad_index())
    code_array.emit("=", p[2], p[4]["from"], None)
    code_array.emit(">", None, p[2], p[4]["to"])
    code_array.emit("goto", None, code_array.get_next_quad_index() + 2, None)
    code_array.emit("goto", None, p[6]["quad_index"], None)
    if p[7] and "exit_when_quad_index" in p[7]:
        code_array.backpatch_e_list(p[7]["exit_when_quad_index"],
                                    code_array.get_next_quad_index())
    return
Exemple #5
0
def p_qis_1(p):
    """qis_1      : """
    code_array.emit("goto", None, None, None)
    p[0] = {
        "quad_index": code_array.get_next_quad_index(),
        "goto_quad_index": [code_array.get_current_quad_index()]
    }
    return
Exemple #6
0
def p_default(p):
    """default      : DEFAULT COLON qis block"""
    break_quad_index = code_array.get_next_quad_index()
    code_array.emit("goto", None, None, None)
    p[0] = {
        "starting_quad_index": p[3]["quad_index"],
        "break_quad_index": break_quad_index
    }
    return
Exemple #7
0
def p_bool_expressions_comparator(p):
    """bool_expressions     : LT pair 
                            | LE pair 
                            | GT pair 
                            | GE pair 
                            | EQ pair 
                            | NEQ pair"""
    p[0] = {
        "t_list": [code_array.get_next_quad_index() + 1],
        "f_list": [code_array.get_next_quad_index() + 2],
        "type": "bool"
    }
    opt = p.slice[1].type
    if opt == "EQ":
        opt = "=="
    elif opt == "NEQ":
        opt = "!="
    else:
        opt = p[1]
    code_array.emit(opt, None, p[2]["first_arg"], p[2]["second_arg"])
    code_array.emit("goto", None, None, None)
    code_array.emit("goto", None, None, None)
    return
Exemple #8
0
def p_statement_switch(p):
    """statement            : SWITCH expressions qis_1 case_element default END
                            | SWITCH expressions qis_1 case_element END"""
    if p[2]["type"] == "bool" and "place" not in p[2] and "value" not in p[2]:
        p[2] = code_array.store_boolean_expression_in_variable(p[2])
        code_array.emit("goto", None,
                        code_array.get_next_quad_index() + 1, None)
        code_array.backpatch_e_list(p[3]["goto_quad_index"],
                                    code_array.get_next_quad_index())
    else:
        code_array.backpatch_e_list(p[3]["goto_quad_index"],
                                    code_array.get_next_quad_index())
    next_list = []
    for case_element in p[4]:
        code_array.emit("==", None, p[2], case_element["num_constraint"])
        code_array.emit("goto", None, case_element["starting_quad_index"],
                        None)
        next_list.append(case_element["break_quad_index"])
    if len(p) == 7:
        code_array.emit("goto", None, p[5]["starting_quad_index"], None)
        next_list.append(p[5]["break_quad_index"])
    code_array.backpatch_e_list(next_list, code_array.get_next_quad_index())
    return
Exemple #9
0
def p_case_element(p):
    """case_element     : CASE NUMCONST COLON qis block
                        | case_element CASE NUMCONST COLON qis block"""
    break_quad_index = code_array.get_next_quad_index()
    code_array.emit("goto", None, None, None)
    if len(p) == 6:
        p[0] = [{
            "num_constraint": p[2],
            "starting_quad_index": p[4]["quad_index"],
            "break_quad_index": break_quad_index
        }]
    else:
        p[0] = [{
            "num_constraint": p[3],
            "starting_quad_index": p[5]["quad_index"],
            "break_quad_index": break_quad_index
        }] + p[1]
    return
Exemple #10
0
def p_psc(p):  # psc: procedure symbol table creator
    """psc      : """
    symbol_table.create_new_scope_symbol_table("main")
    p[0] = {"quad_index": code_array.get_next_quad_index()}
    return
Exemple #11
0
def p_qis(p):  # qis: quad index saver
    """qis      : """
    p[0] = {"quad_index": code_array.get_next_quad_index()}
    return
Exemple #12
0
def p_statement_exit_when(p):
    """statement            : EXIT WHEN bool_expressions"""
    code_array.backpatch_e_list(p[3]["f_list"],
                                code_array.get_next_quad_index())
    p[0] = {"exit_when_quad_index": p[3]["t_list"]}
    return