p.advance() print testC(p,"010","1000111","010") print "A=M;JLT" p.advance() print testC(p,"100","1110000","100") print "D;JGE" p.advance() print testC(p,"000","0001100","011") print "0;JMP" p.advance() print testC(p,"000","0101010","111") print "done testing C=====" print "=====Testing decimalConstantToBinaryString" binary = Code.decimalConstantToBinaryString("33") if binary != "000000000100001": print "Bad conversion. Got:",binary,"Expected: 000000000100001" binary = Code.decimalConstantToBinaryString("267") if binary != "000000100001011": print "Bad conversion. Got:",binary,"Expected: 000000100001011" print "done testing decimalConstantToBinaryString====="
else: i += 1 #pass 1 parser.reset() while parser.hasMoreCommands(): parser.advance() type = parser.commandType() if type == "A": sym = parser.symbol() #sym is a constant if ord(sym[0]) >= ord("0") and ord(sym[0]) <= ord("9"): outFile.write("0" + Code.decimalConstantToBinaryString(sym) + "\n") #sym is in table elif symbolTable.contains(sym): outFile.write("0" + symbolTable.getAddress(sym) + "\n") #sym is a new var else: symbolTable.addVariable(sym) outFile.write("0" + symbolTable.getAddress(sym) + "\n") elif type == "C": d = Code.dest(parser.dest()) c = Code.comp(parser.comp()) j = Code.jump(parser.jump()) outFile.write("111" + c + d + j + "\n") outFile.close()