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()))