def pass0(self, file): parser = Parser(file) cur_address = 0 while parser.has_more_commands(): parser.advance() cmd = parser.command_type() if cmd == parser.A_COMMAND or cmd == parser.C_COMMAND: cur_address += 1 elif cmd == parser.L_COMMAND: self.symbols.add_entry( parser.symbol(), cur_address )
def pass1(self, infile, outfile): parser = Parser(infile) outf = open( outfile, 'w' ) code = Code() while parser.has_more_commands(): parser.advance() cmd = parser.command_type() if cmd == parser.A_COMMAND: outf.write( code.gen_a(self._get_address(parser.symbol())) + '\n' ) elif cmd == parser.C_COMMAND: outf.write( code.gen_c(parser.dest(), parser.comp(), parser.jmp()) + '\n' ) elif cmd == parser.L_COMMAND: pass outf.close()
def main(): args, unknown = parse_args() path = os.path.abspath(args.asmfile) # Initialize parser code = AsmCode() table = SymbolTable() print("=== LOOP 1 : Create symbol table phase ===") index = 0 parser = Parser(path) while parser.has_more_commands(): parser.advance() if parser.command_type() is Command.L: table.add_entry(parser.symbol(), index) index -= 1 index += 1 pprint.pprint(table.table()) print("=== LOOP 2 output assembler phase ===") in_name = os.path.basename(path) # *.asm f = open("{}/{}".format(os.path.dirname(path), os.path.splitext(in_name)[0] + ".hack"), "w") # Re-initialize parser parser = Parser(path) l_index = 16 while parser.has_more_commands(): parser.advance() # For debugging # print("{:>3}: {:<20}{:<20}{:<20}{:<20}{:<20}{:<20}".format( # parser.index, # parser.command, # parser.command_type(), # str(parser.symbol()), # str(parser.dest()) + ": " + code.dest(parser.dest()), # str(parser.comp()) + ": " + code.comp(parser.comp()), # str(parser.jump()) + ": " + code.jump(parser.jump()) # )) if parser.command_type() is Command.A: if is_pos_int(parser.symbol()): f.write("0" + str(bin(int(parser.symbol())))[2:].zfill(15) + '\n') else: if table.contains(parser.symbol()): f.write(table.get_address(parser.symbol()) + '\n') else: table.add_entry(parser.symbol(), l_index) f.write(table.get_address(parser.symbol()) + '\n') l_index += 1 elif parser.command_type() is Command.C: f.write("111" + code.comp(parser.comp()) + code.dest(parser.dest()) + code.jump(parser.jump()) + '\n') pprint.pprint(table.table()) f.close()