#! /usr/bin/env python3 from software.common import argv_translations from software.assembler import ASSEMBLY_FILE_EXT from software.assembler import BINARY_FILE_EXT from software.assembler import itergotos from software.assembler import iterassembly from software.assembler import address_by_name from software.assembler import assembly2binary if __name__ == '__main__': infiles, _, _ = argv_translations(ASSEMBLY_FILE_EXT) for infile in infiles: outfile = infile.replace(ASSEMBLY_FILE_EXT, BINARY_FILE_EXT) with open(outfile, 'w') as outfile_h, open(infile) as infile_h: for address, label in itergotos(infile_h): address_by_name[label] = int(address) for assembly in iterassembly(infile_h): outfile_h.write(assembly2binary(assembly) + '\n') print('Assembled {} to {}'.format(infile, outfile))
#! /usr/bin/env python3 import itertools import os from software.common import argv_translations from software.vm import LANG_FILE_EXT from software.vm import VM_FILE_EXT from software.vm import itertokens from software.vm import to_xml def outfile(file): return file.replace(VM_FILE_EXT, LANG_FILE_EXT) if __name__ == '__main__': """Compile .jack file(s) to .xml file(s). If a directory is passed in sys.argv[1], then all .jack files in that directory are compiled to .xml files in the same directory. """ langfiles, _, _ = argv_translations(LANG_FILE_EXT) for langfile in langfiles: with open(langfile) as langfile_h, open(outfile(langfile), 'w') as outfile_h: for token in itertokens(langfile_h): outfile_h.write(to_xml(token) + '\n') print('Compiled {} to {}'.format(langfile, outfile))
def get_basename(filepath, strip_ext=False): name = os.path.basename(os.path.normpath(filepath)) if strip_ext: return os.path.splitext(name)[0] return name if __name__ == '__main__': """Compile .vm files to assembly (.asm). If a directory is passed in sys.argv[1], the .vm files in that directory are compiled in the order Sys.vm, then every other .vm file. """ infiles, file_or_dir, is_dir = argv_translations(VM_FILE_EXT) if is_dir: dirname = get_basename(file_or_dir) filename = dirname + ASSEMBLY_FILE_EXT outfile = '{dir}/{filename}'.format(dir=file_or_dir, filename=filename) # compile the bootstrap .vm file first infiles = itertools.chain( ('{}/Sys.vm'.format(file_or_dir),), filter(lambda f: not f.endswith('Sys.vm'), infiles)) else: outfile = file_or_dir.replace(VM_FILE_EXT, ASSEMBLY_FILE_EXT) with open(outfile, 'w') as outfile_h: if is_dir: bootstrap(outfile_h) for infile in infiles: infile_name = get_basename(infile, strip_ext=True)