Beispiel #1
0
    def generate_boolean_code(self, p, code_list):
        p[0] = Nonterminal()
        next_quad = len(code_list)
        p[0].true_list = [next_quad]
        p[0].false_list = [next_quad + 1]

        if p[1].place != "" and p[3].place != "":
            code = "L" + str(next_quad) + ": " + "if " + p[1].place + p[2] + p[
                3].place + " goto _" + ";"
        elif p[1].place != "" and p[3].place == "":
            code = "L" + str(next_quad) + ": " + "if " + p[1].place + p[2] + p[
                3].value + " goto _" + ";"
        elif p[1].place == "" and p[3].place != "":
            code = "L" + str(next_quad) + ": " + "if " + p[1].value + p[2] + p[
                3].place + " goto _" + ";"
        else:
            code = "L" + str(next_quad) + ": " + "if " + p[1].value + p[2] + p[
                3].value + " goto _" + ";"

        code_list.append(code)

        code = "L" + str(next_quad + 1) + ": " + 'goto _' + ";"
        code_list.append(code)

        p[0].m = next_quad + 2
        p[0].code = code_list
        p[0].type = "bool"

        print("comparison_operation -> exp " + p[2] + " exp")
        print(code_list)
Beispiel #2
0
 def p_exp_3(self, p):
     """exp : FALSE"""
     # print("""exp -> FALSE""")
     p[0] = Nonterminal()
     next_quad = len(self.code_list)
     p[0].false_list = [next_quad]
     p[0].m = next_quad + 1
     code = "L" + str(next_quad) + ": " + "goto _" + ";"
     self.code_list.append(code)
Beispiel #3
0
    def p_logical_operation_1(self, p):
        """logical_operation : exp OR exp"""
        print("""logical_operation -> exp OR exp""")
        p[0] = Nonterminal()
        m = p[1].m
        self.backpatch(p[1].false_list, m)
        p[0].true_list = p[1].true_list + p[3].true_list
        p[0].false_list = p[3].false_list

        p[0].code = self.code_list
        print(self.code_list)
        self.produce_output()