def translate(arg): """ Checks command argument to see if its a directory of virtual machine code files or just one file. Writes translated assembly code to one file, whether working with a directory of vm files or just one file. The translated file, with an .asm extension, is saved to the same directory where the vm file/s reside. """ #get directory or file from arg and, if on Windows, convert to back slashes print('\nUser input: \n\t' + arg) to_translate = arg.strip() to_translate = os.path.abspath(to_translate) #add trailing slash to last directory if it's missing #so to_write file can be appended if not to_translate.endswith('.vm'): to_translate = os.path.join(to_translate, '') #get name to write to name.asm, using either name.vm or name directory path, tail = ntpath.split(to_translate) fname = tail or ntpath.basename(path) to_write = os.path.join(path, fname.replace('.vm', '') + '.asm') writer = code_writer.CodeWriter(to_write) if os.path.isdir(to_translate): print('\nTranslating .vm files in directory: \n\t' + to_translate) print('\nTranslating to: \n\t' + to_write) writer.write_init() #init asm code is added for directories only for root, dirs, files in os.walk(to_translate): for file in files: if file.endswith('.vm'): file_full_path = os.path.join(root, file) translate_file(file, file_full_path, writer) else: print('\nTranslating file: \n\t' + to_translate) print('\nTranslating to: \n\t' + to_write) translate_file(fname, to_translate, writer) writer.close() print('\nTranslation completed') print('\n----------------------------------------------------------------------')
def setUp(self): self.writer = code_writer.CodeWriter('./data/SimpleFunction.asm') self.writer.set_file_name('SimpleFunction') with open('./data/SimpleFunction.vm') as f: self.lines = [line.strip() for line in f.readlines()]
def setUp(self): self.writer = code_writer.CodeWriter( './data/FibonacciElement/Main.asm') self.writer.set_file_name('Main') with open('./data/FibonacciElement/Main.vm') as f: self.lines = [line.strip() for line in f.readlines()]
def setUp(self): self.writer = code_writer.CodeWriter('./data/FibonacciSeries.asm') self.writer.set_file_name('FibonacciSeries') with open('./data/FibonacciSeries.vm') as f: self.lines = [line.strip() for line in f.readlines()]
def setUp(self): self.writer = code_writer.CodeWriter('./data/BasicLoop.asm') self.writer.set_file_name('BasicLoop') with open('./data/BasicLoop.vm') as f: self.lines = [line.strip() for line in f.readlines()]
def setUp(self): self.writer = code_writer.CodeWriter('./data/StaticTest.asm') self.writer.set_file_name('StaticTest') with open('./data/StaticTest.vm') as f: self.lines = [line.strip() for line in f.readlines()]