def p_expressions_function_call(p): """expressions : ID LPAR arguments_list RPAR""" procedure = p[1] symbol_table.check_procedure_declaration(procedure, p.slice[1]) if len(p[3]) != len(procedure["parameters"]): raise CompilationException( "function call didn't match with function \'" + procedure["place"] + "\' declaration!", p.slice[1]) code_array.save_context() code_array.emit( "store_top", symbol_table.get_current_scope_symbol_table().top_stack_variable, None, None) code_array.emit("push", None, {"value": 0, "type": "int"}, None) return_address_variable = symbol_table.get_new_temp_variable("void*") code_array.emit("&&", return_address_variable, None, None) temp_index = code_array.get_current_quad_index() code_array.emit("push", None, return_address_variable, None) for argument in p[3]: code_array.emit("push", None, argument, None) code_array.emit("call", None, procedure["quad_index"], None) p[0] = symbol_table.get_new_temp_variable("int") code_array.emit("pop", p[0], None, None) code_array.backpatch_e_list([temp_index], code_array.get_current_quad_index()) code_array.restore_context() return
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
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
def p_bool_expressions_or_else(p): """bool_expressions : OR ELSE pair""" first_arg = p[3]["first_arg"] second_arg = p[3]["second_arg"] if "t_list" not in first_arg: code_array.create_simple_if_check(first_arg) if "t_list" not in second_arg: p[3]["second_quad_index"] = code_array.create_simple_if_check( second_arg) code_array.backpatch_e_list(first_arg["f_list"], p[3]["second_quad_index"]) p[0] = { "f_list": second_arg["f_list"], "t_list": code_array.merge_e_lists(first_arg["t_list"], second_arg["t_list"]), "type": "bool" } return
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
def p_program(p): """program : PROGRAM psc ID declarations_list qis_1 procedure_list MAIN block | PROGRAM psc ID qis_1 procedure_list MAIN block | PROGRAM psc ID declarations_list MAIN block | PROGRAM psc ID MAIN block""" if p[len(p) - 1] and "exit_when_quad_index" in p[len(p) - 1]: raise CompilationException( "exit when just allowed in loops!!! function: main", p.slice[1]) symbol_table.set_root() if len(p) > 7: if len(p) == 9: main_goto_quad_index = p[5] main_quad_index = p[8] else: main_goto_quad_index = p[4] main_quad_index = p[7] code_array.backpatch_e_list(main_goto_quad_index["goto_quad_index"], main_quad_index["starting_quad_index"]) return
def p_bool_expressions_or(p): """bool_expressions : OR pair""" first_arg = p[2]["first_arg"] second_arg = p[2]["second_arg"] if "t_list" not in first_arg: code_array.create_simple_if_check(first_arg) if "t_list" not in second_arg: p[2]["second_quad_index"] = code_array.create_simple_if_check( second_arg) temp_var = symbol_table.get_new_temp_variable("bool") code_array.emit('=', temp_var, {"value": 1, "type": "bool"}, None) code_array.backpatch_e_list(first_arg["t_list"], code_array.get_current_quad_index()) code_array.emit('goto', None, p[2]["second_quad_index"], None) code_array.emit('=', temp_var, {"value": 0, "type": "bool"}, None) code_array.backpatch_e_list(first_arg["f_list"], code_array.get_current_quad_index()) code_array.emit('goto', None, p[2]["second_quad_index"], None) code_array.create_simple_if_check(temp_var) code_array.backpatch_e_list(second_arg["f_list"], code_array.get_current_quad_index() - 2) p[0] = { "t_list": code_array.merge_e_lists(temp_var["t_list"], second_arg["t_list"]), "f_list": temp_var["f_list"], "type": "bool" } return
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
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
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