def test_overloaded_casting(): grade = 0 try: ten = Hexadecimal(10) x = int(ten) if type(x) is int and x == 10: grade += 3 else: print("Can't cast Hexadecimal objects as ints correctly.") except Exception as ex: print("Crashed when casting Hexadecimal objects as ints.") try: ten = Hexadecimal(10) x = float(ten) if type(x) is float and x == 10.0: grade += 2 else: print("Can't cast Hexadecimal objects as floats correctly.") except Exception as ex: print("Crashed when casting Hexadecimal object as ints.") return grade
def __init__(self): self.index = 0 self.list_n = [] self.conv = Convert() self.reg = Register("X") self.hexa = Hexadecimal() self.last_code = 0
def relative_base(self,arg): hex = Hexadecimal() c = Convert() res = hex.subs(arg,self.base) res_dec = c.to_decimal(res) if res_dec >= 0 and res_dec <= 4095: return res return None
def gen_table(self): h = Hexadecimal() dir_c = self.bloques[0].get_load_dir() for x in self.bloques: x.set_load_dir(dir_c) if x.name == "por omision": x.length = h.subs(x.last_cp, x.get_load_dir()) else: x.length = x.last_cp dir_c = h.plus(dir_c, x.length)
def __init__(self, window): self.window = window self.tabse = Tabse() self.lonsc = "0H" self.dirsc = "0H" self.dirprog = "0H" self.direj = "0H" self.hexa = Hexadecimal() self.convert = Convert() self.register = Register("A") self.error_indefinido = False self.error_duplicado = False
def test_str(): #As long as there is a constructor and an overloaded str() function, #the try block shouldn't be necessary, but I included it, just in case. grade = 0 try: x = Hexadecimal('AF') if str(x) == "AF": grade += 5 else: print("Overloaded str:", str(x), 'should be AF.') print( "Check the overloaded str(), but also could be in constructor." ) x = Hexadecimal('0') if str(x) == "0": grade += 5 else: print("Overloaded str:", str(x), 'should be 0.') except Exception as ex: print("Crashed when testing str().") return grade
def is_relative_cp(self,cp,arg): hex = Hexadecimal() c = Convert() res_hex = hex.subs_minus(arg,cp) sign = res_hex[1] res_hex = res_hex[0] res = int(c.to_decimal(res_hex)) if sign == "-": res = (res ^ 4095)+1 res = res * -1 if res <= 2047 and res >= -2048: return c.exp_to_hexadecimal(res) else: return None
def get_m_register_op(self): list_ret = [] hexa = Hexadecimal() self.elimina_repetidos(self.list_op_m) for it in self.list_op_m: reg = "M" val = hexa.plus(it[3],"1H") reg += self.current_register.adjust_bytes(val,6,False) reg += "05" reg += it[2] if it[1] == "_": name = self.current_register.adjust_name(it[0]) else: name = self.current_register.adjust_name(self.h_name) reg += name list_ret.append(reg) return list_ret
def make_register_m(self,obj_list,cp_list,num_bloque,bloques): r = Register("M") hx = Hexadecimal() r.name = self.h_name it = 0 while it < len(self.m_register): index = self.m_register[it] # print "normal",index load_dir = bloques.get_load_dir_at(num_bloque[index-1]) cp = hx.plus(cp_list[index-1],load_dir) register = r.make_M(obj_list[index-1],cp) self.list_registers_m.append(register) it += 1 it = 0 while it < len(self.m_modif_register): index = self.m_modif_register[it] # print "modifi",index,len(cp_list) load_dir = bloques.get_load_dir_at(num_bloque[index-1]) cp = hx.plus(cp_list[index-1],load_dir) register = r.make_M_modificado(obj_list[index-1],cp) self.list_registers_m.append(register) it += 1
# @brief Practica 2 Paso 1 del ensamblador para SIC estandar # clase para manejar los archivos y checar las extensiones de estos # @date 29 de Agosto del 2013 # Modolu donde se definen las reglas gramaticales de # el lenguaje sic estandar y se manejan los errores sintacticos from scanner import tokens from scanner import scann import ply.yacc as yacc from convert import Convert from displacement import Displacement from hexadecimal import Hexadecimal from register import Register from segment import Segment seg = Segment() hex = Hexadecimal() conv = Convert() disp = Displacement() reg = Register("R") extension = "" registers = { "A": 0, "X": 1, "L": 2, "CP": 8, "SW": 9, "B": 3, "S": 4, "T": 5, "F": 6 }
def __init__(self, parent=None): QtGui.QWidget.__init__(self) self.window = Ui_DockWidget() self.window.setupUi(self) self.add_registers() self.cargador = None self.hexa = Hexadecimal() self.convert = Convert() self.register = Register("A") self.init_registers() self.end_program = "0H" self.cc_val = "=" self.window.btnSimular.clicked.connect(self.simular) self.operations_3 = { "18": "ADD", "00": "LDA", "40": "AND", "28": "COMP", "24": "DIV", "3C": "J", "30": "JEQ", "34": "JGT", "38": "JLT", "48": "JSUB", "50": "LDCH", "08": "LDL", "04": "LDX", "20": "MUL", "4C": "RSUB", "0C": "STA", "54": "STCH", "14": "STL", "E8": "STSW", "10": "STX", "1C": "SUB", "2C": "TIX", "58": "ADDF", "88": "COMPF", "64": "DIVF", "68": "LDB", "70": "LDF", "6C": "LDS", "74": "LDT", "D0": "LPS", "60": "MULF", "EC": "SSK", "78": "STB", "80": "STF", "D4": "STI", "7C": "STS", "E8": "STSW", "84": "STT", "5C": "SUBF", "E0": "TD", "DC": "WD", "44": "OR" } self.operations_1 = { "C4": "FIX", "C0": "FLOAT", "F4": "HIO", "C8": "NORM", "F0": "SIO", "F8": "TIO" } self.operations_2 = { "90": "ADDR", "B4": "CLEAR", "A0": "COMPR", "9C": "DIVR", "98": "MULR", "AC": "RMO", "A4": "SHIFTL", "A8": "SHIFTR", "94": "SUBR", "B0": "SVC", "B8": "TIXR" } self.operations = { "18": self.add, "00": self.lda, "40": self.and_op, "28": self.comp, "24": self.div, "3C": self.j_op, "30": self.jeq, "34": self.jgt, "38": self.jlt, "48": self.jsub, "50": self.ldch, "08": self.ldl, "04": self.ldx, "20": self.mul, "4C": self.rsub, "0C": self.sta, "54": self.stch, "14": self.stl, "E8": self.stsw, "10": self.stx, "1C": self.sub, "2C": self.tix, "58": self.float_operations, "88": self.float_operations, "64": self.float_operations, "68": self.ldb, "70": self.float_operations, "6C": self.lds, "74": self.ldt, "D0": self.system_operations, "60": self.float_operations, "EC": self.system_operations, "78": self.stb, "80": self.float_operations, "D4": self.system_operations, "7C": self.sts, "E8": self.system_operations, "84": self.stt, "5C": self.float_operations, "E0": self.system_operations, "DC": self.system_operations, "C4": self.float_operations, "C0": self.float_operations, "F4": self.system_operations, "C8": self.float_operations, "F0": self.system_operations, "44": self.or_op, "F8": self.system_operations, "90": self.addr, "B4": self.clear, "A0": self.compr, "9C": self.divr, "98": self.mulr, "AC": self.rmo, "A4": "SHIFTL", "A8": "SHIFTR", "94": self.subr, "B0": "SVC", "B8": self.tixr } self.registers = { "0": [self.REG_A, "A"], "1": [self.REG_X, "X"], "2": [self.REG_L, "L"], "8": [self.REG_CP, "CP"], "9": [self.REG_SW, "SW"], "3": [self.REG_B, "B"], "4": [self.REG_S, "S"], "5": [self.REG_T, "T"], "6": [self.REG_F, "F"] } self.operations_m = [ "J", "JLT", "JEQ", "JGT", "JSUB", "STA", "STB", "STCH", "STL", "STS", "STSW", "STT", "STX", "LDCH" ]
def __init__(self, parent=None): QtGui.QWidget.__init__(self) self.window = Ui_DockWidget() self.window.setupUi(self) self.file_name = None self.init = None self.header = None self.registers = None self.end = None self.rows_count = None self.end_program = None self.cc = "=" self.hex = Hexadecimal() self.reg = Register("T") self.window.btnSimular.clicked.connect(self.simular) self.operations = { "18": self.add, "00": self.lda, "40": self.andop, "28": self.cmp_op, "24": self.div, "3C": self.j_op, "30": self.jeq, "34": self.jgt, "38": self.jlt, "48": self.jsub, "50": self.ldch, "08": self.ldl, "04": self.ldx, "20": self.mul, "4C": self.rsub, "0C": self.sta, "54": self.stch, "14": self.stl, "E8": self.stsw, "10": self.stx, "1C": self.sub, "2C": self.tix } self.operations_text = { "18": "ADD", "00": "LDA", "40": "AND", "28": "COMP", "24": "DIV", "3C": "J", "30": "JEQ", "34": "JGT", "38": "JLT", "48": "JSUB", "50": "LDCH", "08": "LDL", "04": "LDX", "20": "MUL", "4C": "RSUB", "0C": "STA", "54": "STCH", "14": "STL", "E8": "STSW", "10": "STX", "1C": "SUB", "2C": "TIX" }
def test_combo_ops(): grade = 0 try: one = Hexadecimal(1) two = Hexadecimal(2) three = Hexadecimal(3) four = Hexadecimal(4) #Testing += try: one += two if str(one) == str(three): grade += 3 else: print("Trouble with +=") #resetting one one = Hexadecimal(1) except Exception as ex: print("Crashed when testing +=") #Testing -= try: one -= two except Exception as ex: grade += 1 else: print( "Your class stored a negative in an object as the result of -=." ) print("It should have raised an exception and didn't.") one = Hexadecimal(1) try: two -= one if str(two) == str(one): grade += 3 else: print("Trouble when testing -=") print(two, one) two = Hexadecimal(2) except Exception as ex: print("Crashed when testing -=") #Testing *= try: two *= two if str(two) == str(four): grade += 3 else: print("Trouble when testing *=") two = Hexadecimal(2) except Exception as ex: print("Crashed when testing *=") #Testing /= try: four /= two if str(four) == str(two): grade += 3 else: print("Trouble when testing /=") four = Hexadecimal(4) except Exception as ex: print("Crashed when testing /=") try: zero = Hexadecimal(0) four /= zero except ZeroDivisionError as ex: grade += 1 else: print( "Division by 0 is undefined, you should have raised an exception." ) #Testing **= try: two **= two if str(four) == str(two): grade += 3 else: print("Trouble when testing **=") two = Hexadecimal(2) except Exception as ex: print("Crashed when testing **=") #Testing %= try: four %= three if str(four) == str(one): grade += 2 else: print("Trouble when testing %=") four = Hexadecimal(4) except Exception as ex: print("Crashed when testing %=") try: four %= zero except Exception as ex: grade += 1 else: print( "Your class should have caused a crash when trying to % by 0, but didn't." ) four = Hexadecimal(4) except Exception as ex: print("Crashed when testing combination operators") return grade
def test_constructor(): grade = 0 #These should work try: a = Hexadecimal() except Exception as ex: print("Constructor doesn't have default arguments.") else: grade += 2 try: b = Hexadecimal(10) except Exception as ex: print("Constructor doesn't accept positive ints.") else: grade += 2 try: c = Hexadecimal(10.1) except Exception as ex: print("Constructor doesn't accept positive floats.") else: grade += 2 try: d = Hexadecimal('10') except Exception as ex: print("Constructor doesn't accept string version of ints") else: grade += 2.5 #These shouldn't work try: e = Hexadecimal([1, 2, 3]) except Exception as ex: grade += 1 else: print("Constructor shouldn't accept lists") try: e = Hexadecimal('Q') except Exception as ex: grade += 1 else: print("Constructor shouldn't accept a 'Q'") try: f = Hexadecimal(-1) except Exception as ex: grade += 1 else: print("Constructor shouldn't accept negative integers.") try: g = Hexadecimal(-20.1) except Exception as ex: grade += 1 else: print("Constructor shouldn't accept negative floats.") #Decided that constructor should be worth 20 points, so multiplied by 2 grade *= 2 return grade
def test_relational_ops(): grade = 0 try: one = Hexadecimal(1) two = Hexadecimal(2) #Testing < try: if one < two: grade += 2 else: print("Trouble with <") if two < one: print("Trouble with <") else: grade += 1 except Exception as ex: print("Crashed when testing <") #Testing <= try: if one <= two: grade += 2 else: print("Trouble with <=") if one <= one: grade += 1 else: print("Trouble with <=") if two <= one: print("Trouble with <=") else: grade += 1 except Exception as ex: print("Crashed when testing <=") #Testing > try: if one > two: print("Trouble with >") else: grade += 2 if two > one: grade += 1 else: print("Trouble with >") except Exception as ex: print("Crashed when testing >") #Testing >= try: if two >= one: grade += 2 else: print("Trouble with >=") if two >= two: grade += 1 else: print("Trouble with >=") if one >= two: print("Trouble with >=") else: grade += 1 except Exception as ex: print("Crashed when testing >=") #Testing == try: if one == one: grade += 2 else: print("Trouble with ==") if one == two: print("Trouble with ==") else: grade += 1 except Exception as ex: print("Crashed when testing ==") #Testing != try: if one != two: grade += 2 else: print("Trouble with !=") if one != one: print("Trouble with !=") else: grade += 1 except Exception as ex: print("Crashed when testing !=") except Exception as ex: print("Crashed when testing Relational operators") return grade
def test_math_ops(): grade = 0 try: two = Hexadecimal(2) ten = Hexadecimal(10) zero = Hexadecimal(0) five = Hexadecimal(5) fifteen = Hexadecimal(15) fifty = Hexadecimal(50) #Add try: total = ten + zero if str(total) == str(ten): grade += 3 else: print("Overloaded + not working correctly.") total = ten + five if str(total) == str(fifteen): grade += 2 else: print("Overloaded + not working correctly.") except Exception as ex: print("Crashed when trying to add Hexadecimal objects.") #Subtract try: difference = fifteen - five if str(difference) == str(ten): grade += 3 else: print("Overloaded - not working correctly.") except Exception as ex: print("Crashed when trying to subtract Hexadecimal objects.") try: difference = ten - fifteen except Exception as ex: grade += 2 else: print( "Your class stored a negative in an object as the result of subtracting." ) print("It should have raised an exception and didn't.") #Multiply try: product = five * ten if str(product) == str(fifty): grade += 2 else: print("Overloaded * not working correctly.") product = five * zero if str(product) == str(zero): grade += 1 else: print("Overloaded * not working correctly.") except Exception as ex: print("Crashed when trying to multiply Hexadecimal objects.") #Divide try: quotient = fifty / five if str(quotient) == str(ten): grade += 2 else: print("Overloaded / not working correctly.") except Exception as ex: print("Crashed when trying to divide Hexadecimal objects.") try: quotient = fifty / zero except Exception as ex: grade += 1 else: print( "Should have raised an exception when trying to divide by zero." ) #Power try: answer = five**two if str(answer) == str(Hexadecimal(25)): grade += 2 else: print("Overloaded ** not working correctly.") except Exception as ex: print("Crashed when trying to raise a hexadecimal to a power") #Mod try: answer = ten % five if str(answer) == str(zero): grade += 1 else: print("Overloaded % not working correctly.") except Exception as ex: print("Crashed when trying to calculate remainder.") try: answer = ten % zero except Exception as ex: grade += 1 else: print( "Should have raised an exception when trying to divide by zero -- which is used with % operations." ) except Exception as ex: print("Crashed when testing math operations.") return grade