def parse(parser, codeWriter, fileName): import stackParser from stackParser import Parser codeWriter.setFileName(fileName) while parser.hasMoreCommands(): parser.advance() print("Command is " + parser.current() + " type is " + str(parser.commandType())) if parser.commandType() == Parser.C_ARITHMETIC: codeWriter.writeArithmetic(parser.arg1()) elif (parser.commandType() == Parser.C_PUSH) | (parser.commandType() == Parser.C_POP): codeWriter.writePushPop(parser.commandType(), parser.arg1(), parser.arg2()) elif parser.commandType() == Parser.C_GOTO: codeWriter.writeGoto(parser.arg1()) elif parser.commandType() == Parser.C_LABEL: codeWriter.writeLabel(parser.arg1()) elif parser.commandType() == Parser.C_IF: codeWriter.writeIf(parser.arg1()) elif parser.commandType() == Parser.C_CALL: codeWriter.writeCall(parser.arg1(), int(parser.arg2())) elif parser.commandType() == Parser.C_RETURN: codeWriter.writeReturn() elif parser.commandType() == Parser.C_FUNCTION: codeWriter.writeFunction(parser.arg1(), int(parser.arg2())) else: print("Unhandled command type, type is " + str(parser.commandType())) print("\tCommand is " + parser.current())
def parse(parser, codeWriter, fileName): import stackParser from stackParser import Parser codeWriter.setFileName(fileName) while parser.hasMoreCommands(): parser.advance() if parser.commandType() == Parser.C_ARITHMETIC: codeWriter.writeArithmetic(parser.arg1()) elif (parser.commandType() == Parser.C_PUSH) | (parser.commandType() == Parser.C_POP): codeWriter.writePushPop(parser.commandType(), parser.arg1(), parser.arg2()) else: print("Unhandled command type, type is " + str(parser.commandType()))
def commandType(): global data global arith global push global pop arith = False push = False pop = False global components components = data[currentLine].split(" ") if data[currentLine] in arithmetic: codeWriter.writeArithmetic() elif "push" in data[currentLine] or "pop" in data[currentLine]: codeWriter.writePushPop()