def p_if(self, p): r'''if : IF LP exp RP block elseif_blocks else_block''' p[3].code = p[3].ifexp if p[3].ifexp else p[3].code p[0] = NoneTerminal(p) p[0].true = Label() p[0].next = Label() falselabel = None #back patch true block back_patch_true(p[3], p[0].true) elsifblock = "" elseblock = "" elselabel = None #create label for else block and set next for it if p[7].code: elselabel = Label() elseblock = elselabel.label + ": //else\n" + p[7].code elseiflabel = None #check wheter there is elseif block if p[6].code: elseiflabel = Label() back_patch_false(p[3], elseiflabel) falselabel = elseiflabel back_patch_next(p[6], p[0].next) #check for else if p[7].code: back_patch_false(p[6], elselabel) else: back_patch_false(p[6], p[0].next) elsifblock = elseiflabel.label + ": //elseifs\n" + p[6].code elif p[7].code: back_patch_false(p[3], elselabel) falselabel = elselabel else: back_patch_false(p[3], p[0].next) falselabel = None true_block = p[0].true.label + ": " + p[5].code + "goto " + p[0].next.label + "; //next label\n\n" false_block = "" if falselabel: false_block = elsifblock + elseblock next_block = p[0].next.label + ":; //end of if statement - next\n" p[0].code = "// if statement\n//new\n" + p[3].code + true_block + false_block + next_block
def p_while(self, p): r'''while : WHILE LP exp RP block''' p[0] = NoneTerminal(p) begin = Label() code_begin = Label() after = Label() back_patch_false(p[3], after) back_patch_true(p[3], code_begin) p[0].code = begin.label + ": // while begin\n\n" + p[3].code + code_begin.label + ": // while code begin\n" + p[5].code \ + "goto " + begin.label + "; //back to while begin\n\n" + after.label+ ": // end of while\n\n"
def p_logical_operation_10(self, p): r'''logical_operation : exp OR exp''' p[0] = NoneTerminal(p) false_label = Label() back_patch_false(p[1], false_label) p[0].code = p[1].code + false_label.label + ": // logical calculation (OR)\n" + p[3].code
def p_logical_operation_9(self, p): r'''logical_operation : exp AND exp''' p[0] = NoneTerminal(p) true_label = Label() back_patch_true(p[1], true_label) p[0].code = p[1].code + true_label.label + ": // logical calculation (AND)\n" + p[3].code
def p_elseif(self, p): r'''elseif : ELSEIF LP exp RP block %prec PREC1''' p[0] = NoneTerminal(p) truelabel = Label() back_patch_true(p[3], truelabel) p[0].code = p[3].code + truelabel.label + ": //elseif epression\n" + p[5].code + "goto " + NEXT_LABEL + "; // next label\n\n"
def p_elseifs_1(self, p): r'''elseifs : elseifs elseif %prec PREC2''' p[0] = NoneTerminal(p) elseif_label = Label() back_patch_false(p[1], elseif_label) p[0].code = p[1].code + elseif_label.label + ": //elseif \n" + p[2].code + "\n"
def p_for(self, p): r'''for : FOR LP ID IN exp TO exp STEPS exp RP block ''' p[0] = NoneTerminal(p) begin = Label() code_begin = Label() after = Label() initialization = "int " + p[3] + ";\n" + p[3] + " = " + p[5].get_value() + "; // FOR initialization\n" check_bundry = "if ( " + p[3] + " <= " + p[7].get_value() + " ) goto " + code_begin.label + "; // FOR check\n" check_bundry += "goto " + after.label + ";\n\n" iteration = p[3] + " = " + p[3] + " + " + p[9].get_value() + "; // FOR iteration\n" p[0].code = "// FOR BEGIN\n\n" + p[5].code + p[9].code + initialization \ + begin.label + ": // for begin\n\n" + p[7].code + check_bundry \ + code_begin.label + ": // for code begin\n" + p[11].code + iteration \ + "goto " + begin.label + "; //back to for begin\n\n" + after.label+ ": // end of for\n\n"
def p_function_call_1(self, p): r'''function_call : ID function_call_body''' global VARIABLES p[0] = NoneTerminal(p) return_label = Label() registers = Register.Registers.copy() variables = VARIABLES.copy() p[0].code = store_all_registers(registers) + store_all_variables(variables) + "// store return label\n" + push_address(return_label.label) p[0].code += "// calc and store function arguments\n"+ p[2].code + store_args(p[2].args) p[0].code += "// call function\ngoto " + p[1] + ";\n\n" p[0].code += "// return label:\n" + return_label.label + ":;\n" p[0].reg = Register("double") p[0].code += "// load return value\n" + pop_variable(p[0].reg.place) p[0].code += "// load regs and vars\n" + load_all_variables(variables) + load_all_registers(registers) p[0].place = p[0].reg.place
def p_logical_operation_and(self, p): 'logical_operation : exp TOKEN_AND exp' p[0] = NoneTerminal(p) true_label = Label() print(p[1].code) print('=====+') self.back_patch_true(p[1], true_label) p[0].code = p[ 1].code + true_label.label + ": // logical calculation (AND)\n " + p[ 3].code # m = p[1].m # self.backpatch(p[1].true_list, m) # p[0].false_list = p[1].false_list + p[3].false_list # p[0].true_list = p[3].true_list print('logical_operation : exp TOKEN_AND exp')