def compile(input_file_path): input_file = open(input_file_path) output_file_path = f"{input_file_path.strip('.jack')}_.xml" output_file = open(output_file_path, 'w') comp_engine = CompilationEngine() comp_engine.compile(input_file, output_file)
def load_file(jack_filename): jack_file = open(jack_filename, 'r') xml_filename = jack_filename.replace('.jack', '.xml') xml_file = open(xml_filename, 'a') compilation_engine = CompilationEngine(jack_file, xml_file) compilation_engine.compile_class() compilation_engine.save() jack_file.close() xml_file.close()
def run(self): if os.path.isdir(self._in_fname): jack_fnames = glob(os.path.join(self._in_fname, "*.jack")) else: jack_fnames = [self._in_fname] for jack_fname in jack_fnames: out_fname = jack_fname[:-5] + ".vm" CompilationEngine(jack_fname).compile(out_fname)
def compile(file_name, analyze): vm_name = file_name.parent.joinpath(file_name.stem + VM) xml_name = file_name.parent.joinpath(file_name.stem + XML) if analyze else None tokenizer = JackTokenizer(file_name) engine = CompilationEngine(tokenizer, vm_name, xml_name) print('compiling {file}'.format(file=file_name)) engine.compile_class() engine.write() print('Successfully compiled {file} to {output}'.format(file=file_name, output=vm_name))
def main(argv): """ Main flow of program dealing with extracting files for reading and initializing files to translate into """ if not check_args(argv): return # extracting asm file to be processed jack_files_path = argv[1] # creating a .asm file to contain vm files translation to hack machine language if os.path.isdir(jack_files_path): for file in os.listdir(jack_files_path): if file.endswith(".jack"): xml_file_name = "{0}/{1}.xml".format( jack_files_path, os.path.splitext(os.path.basename(file))[0]) CompilationEngine('{0}/{1}'.format(jack_files_path, file), xml_file_name) else: xml_file_name = "{0}.xml".format(os.path.splitext(jack_files_path)[0]) CompilationEngine(jack_files_path, xml_file_name)
def compile_file(self, file_path): print("Compiling", file_path, "...") file_name = os.path.splitext(os.path.basename(file_path))[0] dir_name = os.path.split(file_path)[0] output_file_name = os.path.join(dir_name, file_name + "__.xml") with open(output_file_name, "w") as output_file: tokenizer = Tokenizer(file_path) try: compiler = CompilationEngine(tokenizer, output_file) compiler.compile() print("Compilation successful!", file_path, "=>", output_file_name) except CompilationError as err: tokenizer.close() raise CompilationError("ERROR: " + err.message)
def main(): if len(sys.argv) != 2: print( "Expected 1 argument (either the .jack file or a directory containing .jack files). Exiting!" ) return is_file_arg = sys.argv[1].endswith(".jack") if is_file_arg: jack_files = [sys.argv[1]] else: jack_files = [ join(sys.argv[1], f) for f in listdir(sys.argv[1]) if f.endswith(".jack") ] for jack_file in jack_files: ce = CompilationEngine(JackTokenizer(jack_file), jack_file.split(".jack")[0] + "Nisarg.xml") ce.compile()
def main(): # Input if len(sys.argv) != 2: raise ValueError('Invalid file name.') input_file_path = sys.argv[1] input_texts = get_file_text(input_file_path) splited_input_file_path = input_file_path.split('/') input_file_name = splited_input_file_path[-1] # Output output_tokenizer_file_name = '{}.xml'.format(input_file_name.split('.')[0]) output_tokenizer_file_path = '/'.join([*splited_input_file_path[:-1], output_tokenizer_file_name]) output_vm_file_name = '{}.vm'.format(input_file_name.split('.')[0]) output_vm_file_path = '/'.join([*splited_input_file_path[:-1], output_vm_file_name]) # Text Processing del_blank_content = lambda value: value != '' del_new_line_in_text = lambda value: value.replace('\n', '') # 文中の // を削除して先頭と末尾と空白の文字列を削除 del_comment_in_line = lambda string: re.sub(r'//\s.*', '', string).strip() input_texts = list( filter( del_blank_content, map( del_comment_in_line, filter( remove_comments, map( del_new_line_in_text, input_texts ) ) ) ) ) update_input_texts = [] for input_text in input_texts: # プログラム中のコメントアウト (/** */) は上のテキスト処理では削除できないのでこの処理を追加 if remove_comments(input_text): update_input_texts.append(input_text) print('output_tokenizer_file_name: {}'.format(output_tokenizer_file_name)) print('output_vm_file_name: {}'.format(output_vm_file_name)) with VMWriter(output_vm_file_path) as vmw: with CompilationEngine(update_input_texts, output_tokenizer_file_path, vmw) as engine: engine.compile()
return False def has_more_tokens(self): if self.file.content: return True return False if __name__ == "__main__": jackanalyzer = JackAnalyzer( "/home/victor/coding/nand2tetris/nand2tetris_REAL/projects/10/ArrayTest" ) for _file in jackanalyzer.files: tokenizer = jackanalyzer.create_tokenizer(_file) comp_eng = CompilationEngine() tokens = [] while tokenizer.has_more_tokens(): token = tokenizer.advance() token.token, token.tokentype = jackanalyzer.xml_check( token.token, token.tokentype) tokens.append(token) compilation_engine = CompilationEngine(tokens) # text = "<tokens>\n" # text += '<{kw}> {tok} </{kw}>\n'.format(kw=tokentype, tok=token) # text += '</tokens>' # jackanalyzer.save_xml(_file, text)
def compile(filepath): with CompilationEngine(filepath) as ce: print("compiling %s ..." % filepath) ce.compile()
def compile(filepath): with VmWriter(filepath[:-5] + ".vm") as code_writer: with CompilationEngine(filepath, code_writer) as ce: print("compiling %s ..." % filepath) ce.compile()
def setUp(self): self.compiler = CompilationEngine() self.maxDiff = None
def tokenize(self, code, outfile): tokenizer = JackTokenizer(code) CompilationEngine(tokenizer, outfile).compile()
import glob import sys from pathlib import Path, PurePath from os.path import isfile, isdir, join from jack_tokenizer import JackTokenizer from compilation_engine import CompilationEngine if __name__ == '__main__': if len(sys.argv) > 1: program_path = sys.argv[1] if isfile(program_path): files = [program_path] output_path = Path(program_path).parent elif isdir(program_path): files = glob.glob(join(program_path, '*.jack')) output_path = program_path else: raise FileNotFoundError("[Errno 2] No such file or directory: ", program_path) for file in files: output_file_name = PurePath(file).name.split('.')[0] + '.vm' output_file = Path(output_path, output_file_name) file_tokenizer = JackTokenizer(file) CompilationEngine(file_tokenizer, output_file) else: raise TypeError("1 argument is required: program path, 0 arguments entered")
def __init__(self, file_path): self.jack_files = self.parse_argv(file_path) for jack_file in self.jack_files: compiler = CompilationEngine(jack_file) compiler.compile_class()
def analyze(self): for filename in self.files_to_translate: tokenizer = JackTokenizer(filename) compiler = CompilationEngine(tokenizer.tokens) self.__write_out(filename, compiler.xml_output)
def run_compilation_engine(self): CompilationEngine(self.tokenizer, self.vm_writer).run()
# For handling file/dir paths from pathlib import Path # Import Analyzer components from compilation_engine import CompilationEngine from jack_tokenizer import JackTokenizer # Get input path in_path = Path(argv[1]) if in_path.is_file(): # Path points to a file # Initialize tokenizer tokenizer = JackTokenizer(in_path) # Initialize compilation engine compilationEngine = CompilationEngine(tokenizer, in_path.with_suffix(".xml")) # Start compilation compilationEngine.start_compilation() elif in_path.is_dir(): # Path points to a directory for item in in_path.iterdir(): if item.is_file(): # Compile every jack file if item.suffix == ".jack": tokenizer = JackTokenizer(item) ci = CompilationEngine(tokenizer, item.with_suffix(".xml")) ci.start_compilation() # END OF FILE