def p_write_stmt(p): '''write_stmt : WRITE LPAREN id_list RPAREN SEMICOLON ''' for i in p[3]: if (symboltable.checkType(i) == 'INT'): print(";WRITEI " + i) ll.insert("WRITEI", i, "", "") elif (symboltable.checkType(i) == 'STRING'): print(";WRITES " + i) ll.insert("WRITES", i, "", "") else: print(";WRITEF " + i) ll.insert("WRITEF", i, "", "")
def p_assign_expr(p): '''assign_expr : id ASSIGN expr ''' global registerNum if (" " in p[3]): temp = str(p[1]) + " " + str(p[2]) + " " + str(p[3]) # print(str(p[3])) irExpBuilder(temp) elif (symboltable.checkType(p[1]) == 'INT' and "." not in p[3]): print(";STOREI " + str(p[3]) + " $T" + str(registerNum)) ll.insert("STOREI", str(p[3]), "$T" + str(registerNum), "") print(";STOREI $T" + str(registerNum) + " " + p[1]) ll.insert("STOREI", "$T" + str(registerNum), p[1], "") registerNum = registerNum + 1 elif (symboltable.checkType(p[1]) == 'FLOAT'): print(";STOREF " + str(p[3]) + " $T" + str(registerNum)) ll.insert("STOREF", str(p[3]), "$T" + str(registerNum), "") print(";STOREF $T" + str(registerNum) + " " + p[1]) ll.insert("STOREF", "$T" + str(registerNum), p[1], "") registerNum = registerNum + 1
def innerExp(start, end, listor): global registerNum global typeExp iterator = start ops = ['*','/','+','-'] opNum = 0; for i in listor: if (symboltable.checkType(i) == 'INT'): typeExp = "I" break; elif (symboltable.checkType(i) == 'FLOAT'): typeExp = "F" break; while iterator < end: if (ops[0] in listor[iterator] or ops[1] in listor[iterator]): if (ops[0] in listor[iterator]): print(";MULT" + typeExp + " " + listor[iterator-1] + " " + listor[iterator + 1] + " $T" + str(registerNum)) ll.insert("MULT" + typeExp, listor[iterator-1] , listor[iterator + 1], "$T" + str(registerNum)) listor.insert(iterator-1, "$T" + str(registerNum)) listor.pop(iterator) listor.pop(iterator) listor.pop(iterator) registerNum = registerNum + 1 end = end - 2 elif (ops[1] in listor[iterator] ): print(";DIV" + typeExp + " " + listor[iterator-1] + " " + listor[iterator + 1] + " $T" + str(registerNum)) ll.insert("DIV" + typeExp, listor[iterator-1] , listor[iterator + 1], "$T" + str(registerNum)) listor.insert(iterator-1, "$T" + str(registerNum)) listor.pop(iterator) listor.pop(iterator) listor.pop(iterator) registerNum = registerNum + 1 end = end - 2 iterator = start + 1 else: iterator = iterator + 1 iterator = start while iterator < end: if (ops[2] in listor[iterator] or ops[3] in listor[iterator]): if (ops[2] in listor[iterator]): print(";ADD" + typeExp + " " + listor[iterator-1] + " " + listor[iterator + 1] + " $T" + str(registerNum)) ll.insert("ADD" + typeExp, listor[iterator-1] , listor[iterator + 1], "$T" + str(registerNum)) listor.insert(iterator-1, "$T" + str(registerNum)) listor.pop(iterator) listor.pop(iterator) listor.pop(iterator) registerNum = registerNum + 1 end = end - 2 elif (ops[3] in listor[iterator]): print(";SUB" + typeExp + " " + listor[iterator-1] + " " + listor[iterator + 1] + " $T" + str(registerNum)) ll.insert("SUB" + typeExp, listor[iterator-1] , listor[iterator + 1], "$T" + str(registerNum)) listor.insert(iterator-1, "$T" + str(registerNum)) listor.pop(iterator) listor.pop(iterator) listor.pop(iterator) registerNum = registerNum + 1 end = end - 2 else: iterator = iterator + 1 if (len(listor) >= 2): registerNum = registerNum + 1 return end else: listor.pop(end+1) return end
def p_cond(p): '''cond : expr compop expr''' global labelNum global registerNum counter = 0; if (" " in p[3]): for i in p[3]: ops = ["*","/","-","+"] if (i in ops): counter = counter + 1 counter = counter + registerNum + 2 temp = "$T" + str(counter) + " := " + p[3] irExpBuilder(temp) else: if ("." not in p[3]): print(";STOREI " + str(p[3]) + " $T" + str(registerNum)) ll.insert("STOREI", str(p[3]), "$T" + str(registerNum), "") else: print(";STOREF " + str(p[3]) + " $T" + str(registerNum)) ll.insert("STOREF", str(p[3]), "$T" + str(registerNum), "") # print(labelNum) if (symboltable.checkType(p[1]) == 'INT'): typeExp = "I" elif (symboltable.checkType(p[1]) == 'FLOAT'): typeExp = "F" if (p[2] == '!='): print(";EQ" + typeExp + " " + str(p[1]) + " $T" + str(registerNum) + " label" + str(labelNum)) ll.insert("EQ" + typeExp , str(p[1]), "$T" + str(registerNum), "label" + str(labelNum)) q.put(labelNum) stack.append(labelNum) registerNum = registerNum + 1 labelNum = labelNum + 1 if (p[2] == '>'): print(";LE" + typeExp + " " + str(p[1]) + " $T" + str(registerNum) + " label" + str(labelNum)) ll.insert("LE" + typeExp , str(p[1]), "$T" + str(registerNum), "label" + str(labelNum)) q.put(labelNum) stack.append(labelNum) registerNum = registerNum + 1 labelNum = labelNum + 1 if (p[2] == '<'): print(";GE" + typeExp + " " + str(p[1]) + " $T" + str(registerNum) + " label" + str(labelNum)) ll.insert("GE" + typeExp , str(p[1]), "$T" + str(registerNum), "label" + str(labelNum)) q.put(labelNum) stack.append(labelNum) registerNum = registerNum + 1 labelNum = labelNum + 1 if (p[2] == '='): print(";NE" + typeExp + " " + str(p[1]) + " $T" + str(registerNum) + " label" + str(labelNum)) ll.insert("NE" + typeExp , str(p[1]), "$T" + str(registerNum), "label" + str(labelNum)) q.put(labelNum) stack.append(labelNum) registerNum = registerNum + 1 labelNum = labelNum + 1 if (p[2] == '<='): print(";GT" + typeExp + " " + str(p[1]) + " $T" + str(registerNum) + " label" + str(labelNum)) ll.insert("GT" + typeExp , str(p[1]), "$T" + str(registerNum), "label" + str(labelNum)) q.put(labelNum) stack.append(labelNum) registerNum = registerNum + 1 labelNum = labelNum + 1 if (p[2] == '>='): print(";LT" + typeExp + " " + str(p[1]) + " $T" + str(registerNum) + " label" + str(labelNum)) ll.insert("LT" + typeExp , str(p[1]), "$T" + str(registerNum), "label" + str(labelNum)) q.put(labelNum) stack.append(labelNum) registerNum = registerNum + 1 labelNum = labelNum + 1
def innerExp(start, end, listor): global registerNum global typeExp iterator = start ops = ['*', '/', '+', '-'] opNum = 0 for i in listor: if (symboltable.checkType(i) == 'INT'): typeExp = "I" break elif (symboltable.checkType(i) == 'FLOAT'): typeExp = "F" break while iterator < end: if (ops[0] in listor[iterator] or ops[1] in listor[iterator]): if (ops[0] in listor[iterator]): print(";MULT" + typeExp + " " + listor[iterator - 1] + " " + listor[iterator + 1] + " $T" + str(registerNum)) ll.insert("MULT" + typeExp, listor[iterator - 1], listor[iterator + 1], "$T" + str(registerNum)) listor.insert(iterator - 1, "$T" + str(registerNum)) listor.pop(iterator) listor.pop(iterator) listor.pop(iterator) registerNum = registerNum + 1 end = end - 2 elif (ops[1] in listor[iterator]): print(";DIV" + typeExp + " " + listor[iterator - 1] + " " + listor[iterator + 1] + " $T" + str(registerNum)) ll.insert("DIV" + typeExp, listor[iterator - 1], listor[iterator + 1], "$T" + str(registerNum)) listor.insert(iterator - 1, "$T" + str(registerNum)) listor.pop(iterator) listor.pop(iterator) listor.pop(iterator) registerNum = registerNum + 1 end = end - 2 iterator = start + 1 else: iterator = iterator + 1 iterator = start while iterator < end: if (ops[2] in listor[iterator] or ops[3] in listor[iterator]): if (ops[2] in listor[iterator]): print(";ADD" + typeExp + " " + listor[iterator - 1] + " " + listor[iterator + 1] + " $T" + str(registerNum)) ll.insert("ADD" + typeExp, listor[iterator - 1], listor[iterator + 1], "$T" + str(registerNum)) listor.insert(iterator - 1, "$T" + str(registerNum)) listor.pop(iterator) listor.pop(iterator) listor.pop(iterator) registerNum = registerNum + 1 end = end - 2 elif (ops[3] in listor[iterator]): print(";SUB" + typeExp + " " + listor[iterator - 1] + " " + listor[iterator + 1] + " $T" + str(registerNum)) ll.insert("SUB" + typeExp, listor[iterator - 1], listor[iterator + 1], "$T" + str(registerNum)) listor.insert(iterator - 1, "$T" + str(registerNum)) listor.pop(iterator) listor.pop(iterator) listor.pop(iterator) registerNum = registerNum + 1 end = end - 2 else: iterator = iterator + 1 if (len(listor) >= 2): registerNum = registerNum + 1 return end else: listor.pop(end + 1) return end
def p_cond(p): '''cond : expr compop expr''' global labelNum global registerNum counter = 0 if (" " in p[3]): for i in p[3]: ops = ["*", "/", "-", "+"] if (i in ops): counter = counter + 1 counter = counter + registerNum + 2 temp = "$T" + str(counter) + " := " + p[3] irExpBuilder(temp) else: if ("." not in p[3]): print(";STOREI " + str(p[3]) + " $T" + str(registerNum)) ll.insert("STOREI", str(p[3]), "$T" + str(registerNum), "") else: print(";STOREF " + str(p[3]) + " $T" + str(registerNum)) ll.insert("STOREF", str(p[3]), "$T" + str(registerNum), "") # print(labelNum) if (symboltable.checkType(p[1]) == 'INT'): typeExp = "I" elif (symboltable.checkType(p[1]) == 'FLOAT'): typeExp = "F" if (p[2] == '!='): print(";EQ" + typeExp + " " + str(p[1]) + " $T" + str(registerNum) + " label" + str(labelNum)) ll.insert("EQ" + typeExp, str(p[1]), "$T" + str(registerNum), "label" + str(labelNum)) q.put(labelNum) stack.append(labelNum) registerNum = registerNum + 1 labelNum = labelNum + 1 if (p[2] == '>'): print(";LE" + typeExp + " " + str(p[1]) + " $T" + str(registerNum) + " label" + str(labelNum)) ll.insert("LE" + typeExp, str(p[1]), "$T" + str(registerNum), "label" + str(labelNum)) q.put(labelNum) stack.append(labelNum) registerNum = registerNum + 1 labelNum = labelNum + 1 if (p[2] == '<'): print(";GE" + typeExp + " " + str(p[1]) + " $T" + str(registerNum) + " label" + str(labelNum)) ll.insert("GE" + typeExp, str(p[1]), "$T" + str(registerNum), "label" + str(labelNum)) q.put(labelNum) stack.append(labelNum) registerNum = registerNum + 1 labelNum = labelNum + 1 if (p[2] == '='): print(";NE" + typeExp + " " + str(p[1]) + " $T" + str(registerNum) + " label" + str(labelNum)) ll.insert("NE" + typeExp, str(p[1]), "$T" + str(registerNum), "label" + str(labelNum)) q.put(labelNum) stack.append(labelNum) registerNum = registerNum + 1 labelNum = labelNum + 1 if (p[2] == '<='): print(";GT" + typeExp + " " + str(p[1]) + " $T" + str(registerNum) + " label" + str(labelNum)) ll.insert("GT" + typeExp, str(p[1]), "$T" + str(registerNum), "label" + str(labelNum)) q.put(labelNum) stack.append(labelNum) registerNum = registerNum + 1 labelNum = labelNum + 1 if (p[2] == '>='): print(";LT" + typeExp + " " + str(p[1]) + " $T" + str(registerNum) + " label" + str(labelNum)) ll.insert("LT" + typeExp, str(p[1]), "$T" + str(registerNum), "label" + str(labelNum)) q.put(labelNum) stack.append(labelNum) registerNum = registerNum + 1 labelNum = labelNum + 1