def compile(src_jack_file, output): print(src_jack_file) tokenizer = JackTokenizer(src_jack_file) symbol_table = SymbolTable() vm_writer = VMWriter(output) compilation_engine = CompilationEngine(tokenizer, symbol_table, vm_writer) compilation_engine.compile()
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 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 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 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(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 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 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(): # 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()
def run_compilation_engine(self): CompilationEngine(self.tokenizer, self.vm_writer).run()
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 analyze(self): # for each source filename, compile the class for source_filename in self.source_filenames: compiler = CompilationEngine(source_filename) compiler.compile_class()
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 __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()
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")
# 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
def tokenize(self, code, outfile): tokenizer = JackTokenizer(code) CompilationEngine(tokenizer, outfile).compile()
def setUp(self): self.compiler = CompilationEngine() self.maxDiff = None
class CompilationEngineTest(unittest.TestCase): def setUp(self): self.compiler = CompilationEngine() self.maxDiff = None def reformat_xml_to_standardize_whitespace(self, xml): tree = ET.fromstring(''.join(xml.split())) self.compiler.prettify_elements(tree) return ET.tostring(tree, encoding='unicode', short_empty_elements=False) def assert_xml_equal(self, actual_xml, expected_xml, debug_output=False): if debug_output: print('expected_xml:') print(self.reformat_xml_to_standardize_whitespace(expected_xml)) print('actual_xml:') print(self.reformat_xml_to_standardize_whitespace(actual_xml)) self.assertEqual( self.reformat_xml_to_standardize_whitespace(expected_xml), self.reformat_xml_to_standardize_whitespace(actual_xml)) def test_empty_class(self): output = self.compiler.compile(""" class Main { } """) self.assert_xml_equal( output, """<class> <keyword> class </keyword> <identifier> Main </identifier> <symbol> { </symbol> <symbol> } </symbol> </class>""") def test_class_with_class_variables(self): output = self.compiler.compile(""" class Main { field int x, y; field int size; static boolean test; field Square square; } """) self.assert_xml_equal( output, """<class> <keyword> class </keyword> <identifier> Main </identifier> <symbol> { </symbol> <classVarDec> <keyword> field </keyword> <keyword> int </keyword> <identifier> x </identifier> <symbol> , </symbol> <identifier> y </identifier> <symbol> ; </symbol> </classVarDec> <classVarDec> <keyword> field </keyword> <keyword> int </keyword> <identifier> size </identifier> <symbol> ; </symbol> </classVarDec> <classVarDec> <keyword> static </keyword> <keyword> boolean </keyword> <identifier> test </identifier> <symbol> ; </symbol> </classVarDec> <classVarDec> <keyword> field </keyword> <identifier> Square </identifier> <identifier> square </identifier> <symbol> ; </symbol> </classVarDec> <symbol> } </symbol> </class>""") def test_class_with_variables_and_empty_subroutine(self): output = self.compiler.compile(""" class Main { field int size; function void main() { } } """) self.assert_xml_equal( output, """<class> <keyword> class </keyword> <identifier> Main </identifier> <symbol> { </symbol> <classVarDec> <keyword> field </keyword> <keyword> int </keyword> <identifier> size </identifier> <symbol> ; </symbol> </classVarDec> <subroutineDec> <keyword> function </keyword> <keyword> void </keyword> <identifier> main </identifier> <symbol> ( </symbol> <parameterList> </parameterList> <symbol> ) </symbol> <subroutineBody> <symbol> { </symbol> <statements/> <symbol> } </symbol> </subroutineBody> </subroutineDec> <symbol> } </symbol> </class>""") def test_subroutine_with_variables_and_no_statements(self): output = self.compiler.compile(""" class Main { function void main() { var Array a; var int length; var int i, sum; } } """) self.assert_xml_equal( output, """<class> <keyword> class </keyword> <identifier> Main </identifier> <symbol> { </symbol> <subroutineDec> <keyword> function </keyword> <keyword> void </keyword> <identifier> main </identifier> <symbol> ( </symbol> <parameterList> </parameterList> <symbol> ) </symbol> <subroutineBody> <symbol> { </symbol> <varDec> <keyword> var </keyword> <identifier> Array </identifier> <identifier> a </identifier> <symbol> ; </symbol> </varDec> <varDec> <keyword> var </keyword> <keyword> int </keyword> <identifier> length </identifier> <symbol> ; </symbol> </varDec> <varDec> <keyword> var </keyword> <keyword> int </keyword> <identifier> i </identifier> <symbol> , </symbol> <identifier> sum </identifier> <symbol> ; </symbol> </varDec> <statements></statements> <symbol> } </symbol> </subroutineBody> </subroutineDec> <symbol> } </symbol> </class>""") def test_let_statement(self): output = self.compiler.compile(""" class Main { function void main() { let i = 0; } } """) self.assert_xml_equal( output, """<class> <keyword> class </keyword> <identifier> Main </identifier> <symbol> { </symbol> <subroutineDec> <keyword> function </keyword> <keyword> void </keyword> <identifier> main </identifier> <symbol> ( </symbol> <parameterList> </parameterList> <symbol> ) </symbol> <subroutineBody> <symbol> { </symbol> <statements> <letStatement> <keyword> let </keyword> <identifier> i </identifier> <symbol> = </symbol> <expression> <term> <integerConstant> 0 </integerConstant> </term> </expression> <symbol> ; </symbol> </letStatement> </statements> <symbol> } </symbol> </subroutineBody> </subroutineDec> <symbol> } </symbol> </class>""") def test_method_call(self): output = self.compiler.compile(""" class Main { function void dispose () { do square.dispose(); do Memory.deAlloc(square); return; } }""") self.assert_xml_equal( output, """<class> <keyword> class </keyword> <identifier> Main </identifier> <symbol> { </symbol> <subroutineDec> <keyword> function </keyword> <keyword> void </keyword> <identifier> dispose </identifier> <symbol> ( </symbol> <parameterList> </parameterList> <symbol> ) </symbol> <subroutineBody> <symbol> { </symbol> <statements> <doStatement> <keyword> do </keyword> <identifier> square </identifier> <symbol> . </symbol> <identifier> dispose </identifier> <symbol> ( </symbol> <expressionList> </expressionList> <symbol> ) </symbol> <symbol> ; </symbol> </doStatement> <doStatement> <keyword> do </keyword> <identifier> Memory </identifier> <symbol> . </symbol> <identifier> deAlloc </identifier> <symbol> ( </symbol> <expressionList> <expression> <term> <identifier> square </identifier> </term> </expression> </expressionList> <symbol> ) </symbol> <symbol> ; </symbol> </doStatement> <returnStatement> <keyword> return </keyword> <symbol> ; </symbol> </returnStatement> </statements> <symbol> } </symbol> </subroutineBody> </subroutineDec> <symbol> } </symbol> </class>""", True) def test_if_no_else(self): output = self.compiler.compile(""" class Main { method void moveSquare() { if (direction) { do square.moveUp(); } if (direction) { do square.moveDown(); } return; } }""") self.assert_xml_equal( output, """<class> <keyword> class </keyword> <identifier> Main </identifier> <symbol> { </symbol> <subroutineDec> <keyword> method </keyword> <keyword> void </keyword> <identifier> moveSquare </identifier> <symbol> ( </symbol> <parameterList> </parameterList> <symbol> ) </symbol> <subroutineBody> <symbol> { </symbol> <statements> <ifStatement> <keyword> if </keyword> <symbol> ( </symbol> <expression> <term> <identifier> direction </identifier> </term> </expression> <symbol> ) </symbol> <symbol> { </symbol> <statements> <doStatement> <keyword> do </keyword> <identifier> square </identifier> <symbol> . </symbol> <identifier> moveUp </identifier> <symbol> ( </symbol> <expressionList> </expressionList> <symbol> ) </symbol> <symbol> ; </symbol> </doStatement> </statements> <symbol> } </symbol> </ifStatement> <ifStatement> <keyword> if </keyword> <symbol> ( </symbol> <expression> <term> <identifier> direction </identifier> </term> </expression> <symbol> ) </symbol> <symbol> { </symbol> <statements> <doStatement> <keyword> do </keyword> <identifier> square </identifier> <symbol> . </symbol> <identifier> moveDown </identifier> <symbol> ( </symbol> <expressionList> </expressionList> <symbol> ) </symbol> <symbol> ; </symbol> </doStatement> </statements> <symbol> } </symbol> </ifStatement> <returnStatement> <keyword>return</keyword> <symbol>;</symbol> </returnStatement> </statements> <symbol> } </symbol> </subroutineBody> </subroutineDec> <symbol> } </symbol> </class>""") def test_compound_let_statement(self): output = self.compiler.compile(""" class Main { function void main() { let i = i | j; return; } }""") self.assert_xml_equal( output, """<class> <keyword>class</keyword> <identifier>Main</identifier> <symbol>{</symbol> <subroutineDec> <keyword>function</keyword> <keyword>void</keyword> <identifier>main</identifier> <symbol>(</symbol> <parameterList></parameterList> <symbol>)</symbol> <subroutineBody> <symbol>{</symbol> <statements> <letStatement> <keyword>let</keyword> <identifier>i</identifier> <symbol>=</symbol> <expression> <term> <identifier>i</identifier> </term> <symbol>|</symbol> <term> <identifier>j</identifier> </term> </expression> <symbol>;</symbol> </letStatement> <returnStatement> <keyword>return</keyword> <symbol>;</symbol> </returnStatement> </statements> <symbol>}</symbol> </subroutineBody> </subroutineDec> <symbol>}</symbol> </class> """, True)
def analyze(src_jack_file, output): print(src_jack_file) tokenizer = JackTokenizer(src_jack_file) compilation_engine = CompilationEngine(tokenizer) compilation_engine.analysis(output)