From 8eab267645cf49fb7e25827ff6eb2a0368b0d0f8 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Thu, 18 May 2017 13:20:17 +0200 Subject: [PATCH] Polishing. Uso di next() --- python/assembler.py | 36 ++++++------------------------------ 1 file changed, 6 insertions(+), 30 deletions(-) diff --git a/python/assembler.py b/python/assembler.py index 378a38d..9d6928c 100644 --- a/python/assembler.py +++ b/python/assembler.py @@ -65,8 +65,8 @@ class VMAssembler: def process_code_line(self, line): sys.stdout.write("CODE: ") components = [x for x in re.split('\W', line) if x] - instruction = VMInstruction(components[0], components[1:]) + sys.stdout.write(str(instruction) + "\n") self.parse(instruction) def imm2reg(self, instruction): @@ -225,16 +225,14 @@ class VMInstruction: """ def __init__(self, opcode, instr_list): - # TODO EXCEPTION SE REGISTRO / IMM / OPCODE NON VALIDO immediate_regexp = re.compile("^(0x*|[0-9]*$)") - opc_name, opc_value = value_from_list(ops, opcode) - self.opcode = VMComponent(opc_name, opc_value) + self.opcode = next((x for x in ops if x.name == opcode), None) self.args = [] for el in instr_list: if not immediate_regexp.match(el): # create a VM component for a register - reg_name, reg_value = value_from_list(regs, el) - self.args.append(VMComponent(reg_name, reg_value)) + reg_comp = next((x for x in regs if x.name == el), None) + self.args.append(reg_comp) else: # directly append the immediate self.args.append(VMComponent(el, el)) @@ -250,7 +248,8 @@ op_names = ["MOVI", "ADDR", "SUBI", "SUBR", - "XORI", + "XORB", + "XORW", "XORR", "NOTR", "MULI", @@ -276,29 +275,6 @@ ops = [VMComponent(s.casefold(), i) for i, s in enumerate(op_names)] regs = [VMComponent(s.casefold(), i) for i, s in enumerate(reg_names)] -def value_from_list(fromlist, name): - """ - returns a tuple (name, value) from a list of VMComponents - """ - for el in fromlist: - if el.name == name: - return (el.name, el.value) - if fromlist == ops: - raise InvalidOperation(name) - elif fromlist == regs: - raise InvalidRegister(name) - - -def name_from_list(fromlist, value): - """ - returns a tuple (name, value) from a list of VMComponents - """ - for el in fromlist: - if el.value == value: - return (el.name, el.value) - return None - - def assemble_data(line): sys.stdout.write("DATA:\t") sys.stdout.write(line.strip(",") + "\n")