Exemplo n.º 1
0
 def p_stmt_for1(self, p):
     """stmt : FOR LRB exp SEMICOLON exp SEMICOLON exp RRB stmt"""
     p[0] = NonTerminal()
     p[0].begin = self.new_label()
     # pMiddle = self.new_label()
     p[0].after = self.new_label()
     # s = p[5].code.split("\n")
     # pAgain = s[1].split(":")
     p[0].code = p[3].code + p[0].begin + ": ;\n" + p[5].code + "if (" + str(
         p[5].get_value()) + " == 0" + "){\ngoto " + p[0].after + ";\n}\n"
     p[0].code += p[9].code
     p[0].code += p[7].code + "\ngoto " + p[0].begin + ";\n"
     p[0].code += p[0].after + ": ;\n"
Exemplo n.º 2
0
 def p_stmt_while(self, p):
     """stmt : WHILE LRB exp RRB stmt"""
     p[0] = NonTerminal()
     p[0].begin = self.new_label()
     p[0].after = self.new_label()
     p[0].code = p[0].begin + ": ;\n" + p[3].code + "if (" + str(
         p[3].get_value()) + " == 0" + "){\ngoto " + p[0].after + ";\n}\n"
     p[0].code += p[5].code + "\n"
     if p[3].isRel:
         p[0].code += "goto " + "L" + str(self.labelCount - 6) + ";\n"
     else:
         p[0].code += "goto " + p[0].begin + ";\n"
     p[0].code += p[0].after + ": ;\n"
Exemplo n.º 3
0
 def p_stmt_for2(self, p):
     """stmt : FOR LRB ID IN ID RRB stmt"""
     p[0] = NonTerminal()
     p[0].begin = self.new_label()
     # pMiddle = self.new_label()
     p[0].after = self.new_label()
     counter = self.new_temp()
     p[0].code = "int " + counter + " = 0;\n"
     p[0].code += p[0].begin + ": ;\n" + p[
         3] + " = array[" + counter + "];\nif (" + counter + " == arr_pointer){\ngoto " + p[
             0].after + ";\n}\n"
     # p[0].code += "goto " + p[0].after + ";\n"
     p[0].code += p[7].code + counter + " += 1;\ngoto " + "L" + str(
         self.labelCount - 2) + ";\n"
     p[0].code += p[0].after + ": ;\n"
Exemplo n.º 4
0
 def p_stmt_if2(self, p):
     """stmt : IF LRB exp RRB stmt elseiflist ELSE stmt %prec IF"""
     p[0] = NonTerminal()
     # p[0].begin = self.new_label()
     pElse = self.new_label()
     p[0].after = self.new_label()
     if p[6].code != "":
         p[0].code = p[3].code + "if (" + str(p[3].get_value(
         )) + " == 0" + "){\ngoto " + pElse + ";\n}\n" + p[5].code
         p[0].code += "goto " + p[0].after + ";\n"
         p[0].code += pElse + ": ;\n" + p[6].code
         p[0].code += p[0].after + ": ;\n"
     else:
         p[0].code = p[3].code + "if (" + str(p[3].get_value(
         )) + " == 0" + "){\ngoto " + pElse + ";\n}\n" + p[5].code
         p[0].code += "goto " + p[0].after + ";\n"
         p[0].code += pElse + ": ;\n" + p[8].code
         p[0].code += p[0].after + ": ;\n"