def second_pass(parser, symbols): is_variable = re.compile(u'^[^\d]') variable_address = 15 coder = Code() translated = [] parser.reset() while parser.hasMoreCommands: parser.advance() command_type = parser.commandType if command_type == 'A_COMMAND': address = parser.symbol if is_variable.match(address): if symbols.contains(address): address = symbols.GetAddress(address) else: variable_address = variable_address + 1 symbols.addEntry(address, variable_address) address = variable_address else: address = int(address) translated.append(A_COMM_TMPL.format(address)[0:16]) if command_type == 'C_COMMAND': comp = coder.comp(parser.comp) dest = coder.dest(parser.dest) jump = coder.jump(parser.jump) translated.append(C_COMM_TMPL.format(comp, dest, jump)[0:16]) return '\n'.join(translated)
def setUp(self): self.coder = Code()