def p_if_stmt(p): "if_stmt : IF LEFTPAREN cond RIGHTPAREN decl_block_var stmt_list else_part ENDIF" global currentScope, currentBlock, lastBlock currentScope = currentScope.getParent() blockScope = SymbolTable("BLOCK " + str(currentBlock), currentScope) lastBlock = "BLOCK " + str(currentBlock) currentScope.addChild(blockScope) currentBlock += 1 # currentScope = blockScope # currentBlock if len(blockList.getValues()) > 0: for var in blockList.getValues(): blockScope.addVariable(var) blockList.clear() tempRep = IRRep() tempRep.addToEnd(p[3][0].first) tempRep.addToEnd(p[6][0].first) if(p[7] is not None): skipLabel = irlist.nextLabel() tempNode1 = IRNode("JUMP", skipLabel, "", "", None, None) tempNode2 = IRNode("LABEL", p[3][1], "", "", None, None) tempRep.addToEnd(tempNode1) tempRep.addToEnd(tempNode2) tempRep.addToEnd(p[7][0].first) tempNode3 = IRNode("LABEL", skipLabel, "", "", None, None) tempRep.addToEnd(tempNode3) else: tempNode = IRNode("LABEL", p[3][1], "", "", None, None) tempRep.addToEnd(tempNode) p[0] = [tempRep]
def p_if_stmt(p): "if_stmt : IF LEFTPAREN cond RIGHTPAREN decl_block_var stmt_list else_part ENDIF" global currentScope, currentBlock, lastBlock currentScope = currentScope.getParent() blockScope = SymbolTable("BLOCK " + str(currentBlock), currentScope) lastBlock = "BLOCK " + str(currentBlock) currentScope.addChild(blockScope) currentBlock += 1 # currentScope = blockScope # currentBlock if len(blockList.getValues()) > 0: for var in blockList.getValues(): blockScope.addVariable(var) blockList.clear() tempRep = IRRep() tempRep.addToEnd(p[3][0].first) tempRep.addToEnd(p[6][0].first) if (p[7] is not None): skipLabel = irlist.nextLabel() tempNode1 = IRNode("JUMP", skipLabel, "", "", None, None) tempNode2 = IRNode("LABEL", p[3][1], "", "", None, None) tempRep.addToEnd(tempNode1) tempRep.addToEnd(tempNode2) tempRep.addToEnd(p[7][0].first) tempNode3 = IRNode("LABEL", skipLabel, "", "", None, None) tempRep.addToEnd(tempNode3) else: tempNode = IRNode("LABEL", p[3][1], "", "", None, None) tempRep.addToEnd(tempNode) p[0] = [tempRep]
def p_read_stmt(p): '''read_stmt : READ LEFTPAREN id_list2 RIGHTPAREN SEMICOLON''' tempRep = IRRep() for var in p[3]: tstring = "I" if (globalSymbolTable.getType(var) == "FLOAT"): tstring = "F" node = IRNode("READ" + tstring, var, "", "", None, None) tempRep.addToEnd(node) p[0] = [tempRep]
def p_read_stmt(p): '''read_stmt : READ LEFTPAREN id_list2 RIGHTPAREN SEMICOLON''' tempRep = IRRep() for var in p[3]: tstring = "I" if(globalSymbolTable.getType(var) == "FLOAT"): tstring = "F" node = IRNode("READ" + tstring, var, "", "", None, None) tempRep.addToEnd(node) p[0] = [tempRep]
def p_write_stmt(p): "write_stmt : WRITE LEFTPAREN id_list2 RIGHTPAREN SEMICOLON" tempRep = IRRep() for var in p[3]: tstring = "I" if (globalSymbolTable.getType(var) == "FLOAT"): tstring = "F" if (globalSymbolTable.getType(var) == "STRING"): tstring = "S" node = IRNode("WRITE" + tstring, var, "", "", None, None) tempRep.addToEnd(node) p[0] = [tempRep]
def p_write_stmt(p): "write_stmt : WRITE LEFTPAREN id_list2 RIGHTPAREN SEMICOLON" tempRep = IRRep() for var in p[3]: tstring = "I" if(globalSymbolTable.getType(var) == "FLOAT"): tstring = "F" if(globalSymbolTable.getType(var) == "STRING"): tstring = "S" node = IRNode("WRITE" + tstring, var, "", "", None, None) tempRep.addToEnd(node) p[0] = [tempRep]
def p_while_stmt(p): "while_stmt : WHILE LEFTPAREN cond RIGHTPAREN decl_block_var stmt_list ENDWHILE" global currentScope, currentBlock currentScope = currentScope.getParent() blockScope = SymbolTable("BLOCK " + str(currentBlock), currentScope) lastBlock = "BLOCK " + str(currentBlock) currentScope.addChild(blockScope) currentBlock += 1 #currentScope = blockScope if len(blockList.getValues()) > 0: for var in blockList.getValues(): blockScope.addVariable(var) blockList.clear() tempRep = IRRep() tempLabel = irlist.nextLabel() tempNode1 = IRNode("LABEL", tempLabel, "", "", None, None) tempRep.addToEnd(tempNode1) tempRep.addToEnd(p[3][0].first) tempRep.addToEnd(p[6][0].first) tempNode2 = IRNode("JUMP", tempLabel, "", "", None, None) tempRep.addToEnd(tempNode2) tempNode3 = IRNode("LABEL", p[3][1], "", "", None, None) tempRep.addToEnd(tempNode3) p[0] = [tempRep]
def p_primary(p): '''primary : LEFTPAREN expr RIGHTPAREN | IDENTIFIER | INTLITERAL | FLOATLITERAL''' if (p[1] == "("): p[0] = p[2] elif (p[1].isdigit()): node = IRNode("STOREI", p[1], "", irlist.nextTemp(), None, None) globalTempSymbolTable.addVariable(Variable(node.result, "INT")) tempRep = IRRep() tempRep.addToEnd(node) p[0] = [tempRep, node.result] elif (p[1][0].isdigit()): node = IRNode("STOREF", p[1], "", irlist.nextTemp(), None, None) globalTempSymbolTable.addVariable(Variable(node.result, "FLOAT")) tempRep = IRRep() tempRep.addToEnd(node) p[0] = [tempRep, node.result] else: p[0] = [IRRep(), p[1]]
def p_primary(p): '''primary : LEFTPAREN expr RIGHTPAREN | IDENTIFIER | INTLITERAL | FLOATLITERAL''' if(p[1] == "("): p[0] = p[2] elif(p[1].isdigit()): node = IRNode("STOREI", p[1], "", irlist.nextTemp(), None, None) globalTempSymbolTable.addVariable(Variable(node.result, "INT")) tempRep = IRRep() tempRep.addToEnd(node) p[0] = [tempRep, node.result] elif(p[1][0].isdigit()): node = IRNode("STOREF", p[1], "", irlist.nextTemp(), None, None) globalTempSymbolTable.addVariable(Variable(node.result, "FLOAT")) tempRep = IRRep() tempRep.addToEnd(node) p[0] = [tempRep, node.result] else: p[0] = [IRRep(), p[1]]
globalSymbolTable = SymbolTable("GLOBAL", None) globalTempSymbolTable = SymbolTable("GLOBAL", None) currentScope = globalSymbolTable currentScope.parent = currentScope lastType = None index = -1 currentBlock = 1 parameterList = SymbolTable("Params", None) funcList = SymbolTable("Funcs", None) blockList = SymbolTable("Block", None) lastFunc = "" lastBlock = "" yacc.yacc() irlist = IRRep() nodeOne = IRNode("LABEL", "main", "", "", None, None) irlist.addToEnd(nodeOne) nodeTwo = IRNode("LINK", "", "", "", None, None) irlist.addToEnd(nodeTwo) # Reads in the file and processes it with open(sys.argv[1], "r") as myFile: data = myFile.read() yacc.parse(input=data, lexer=littleLexer) nodeLast = IRNode("RET", "", "", "", None, None) irlist.addToEnd(nodeLast) irlist.printTiny(globalSymbolTable)