def main(): try: input = sys.argv[1] except IndexError: print("Error: you must supply a file for compiling") sys.exit() if os.path.isdir(input): # Directory of files to compile if input.endswith('\\') == False: input = input + '\\' for inputFile in os.listdir(input): if inputFile.endswith('.jack'): outputFile = inputFile[:inputFile.rfind('.')] + '.vm' CompilationEngine(input + inputFile, input + outputFile) elif os.path.isfile(input): # Single file to compile if input.endswith('.jack') == False: raise InputFileError( 'Input file is not recognised as a .jack file type') output = input[:input.rfind('.')] + '.vm' CompilationEngine(input, output) else: # Exit raise InputFileError('Input is not a directory or file')
def main(): import sys import os.path import glob input_path = sys.argv[1] if os.path.isdir(input_path): input_files = glob.glob(os.path.join(input_path, "*.jack")) elif input_path.endswith(".jack"): input_files = [input_path] else: raise ValueError("input is not a jack file") for input_filename in input_files: input_file = open(input_filename, "r") tokenizer = JackTokenizer(input_file) print(f"Compiling {input_filename}") # parse the input tree_builder = ParseTreeBuilder(tokenizer) tree = tree_builder.build() # compile output_filename = input_filename.replace(".jack", ".vm") output_file = open(output_filename, "w") writer = VMWriter(output_file) compiler = CompilationEngine(writer) compiler.compile_class(tree) input_file.close() output_file.close() print(f"Saved {input_filename}")
class JackAnalyzer: def __init__(self, infile): self.infile = infile def run(self): infile = self.infile if infile.find('.jack') != -1: outfile = os.path.splitext(infile)[0] + '.xml' print() print(infile, '->', outfile) self.tk = JackTokenizer(infile) self.ce = CompilationEngine(outfile) try: self.ce.compileClass(self.tk, 0) except UserWarning: print('Syntax Error: \'', self.tk.getToken(), '\' is unexpected') print('Source Code:', self.tk.curLine) else: for root, dirs, files in os.walk(infile): for name in files: if name.find('.jack') != -1: inf = os.path.join(root, name) outf = os.path.splitext(inf)[0] + '.xml' print() print(inf, '->', outf) self.tk = JackTokenizer(inf) self.ce = CompilationEngine(outf) try: self.ce.compileClass(self.tk, 0) except UserWarning: print('Syntax Error: \'', self.tk.getToken(), '\' is unexpected') print('Source Code:', self.tk.curLine)
def run(cls, input_file_name, output_file_name): output_file = open(output_file_name, 'w') input_file = open(input_file_name, 'r') tokenizer = JackTokenizer(input_file) compiler = CompilationEngine(tokenizer, output_file) compiler.compile_class()
class JackAnalyzer(object): 'JackAnalyzer' def __init__(self, file): if file.endswith('.jack') and os.path.isfile(file): self.__infilelist = [file] self.__outfilename = file[:-4] + 'xml' else: if not os.path.isdir(file): print('input should be a ".jack" file or a directory') sys.exit() self.__infilelist = glob.glob(os.path.join(file, '*.jack')) self.__outfilename = os.path.join(file, os.path.basename(os.path.abspath(file)) + '.xml') if self.__infilelist == []: print('no ".jack" file found in the given directory') sys.exit() self.__compileEngine = CompilationEngine() def gen(self): '''generate xml file''' with open(self.__outfilename, 'w') as outfile: for infile in self.__infilelist: tokenizer = JackTokenizer(infile) self.__compileEngine.compile(tokenizer, outfile)
def test_compile_subroutine_dec(self): engine = CompilationEngine("method void decSize(int Ax, int Ay) { var boolean exit; }", "fakeOutputFile", True) engine.compile_subroutine_dec() self.assertEqual(engine.xml_output, [ '<subroutineDec>', '<keyword> method </keyword>', '<keyword> void </keyword>', '<identifier> decSize, category: subroutine, definedOrUsed: defined </identifier>', '<symbol> ( </symbol>', '<parameterList>', '<keyword> int </keyword>', '<identifier> Ax, category: argument, definedOrUsed: defined, index: 1 </identifier>', '<symbol> , </symbol>', '<keyword> int </keyword>', '<identifier> Ay, category: argument, definedOrUsed: defined, index: 2 </identifier>', '</parameterList>', '<symbol> ) </symbol>', '<subroutineBody>', '<symbol> { </symbol>', '<varDec>', '<keyword> var </keyword>', '<keyword> boolean </keyword>', '<identifier> exit, category: local, definedOrUsed: defined, index: 0 </identifier>', '<symbol> ; </symbol>', '</varDec>', '<statements>', '</statements>', '<symbol> } </symbol>', '</subroutineBody>', '</subroutineDec>'])
def main(argv): file_names = [] consol_input = argv[0] if '.jack' in consol_input: file_names.append(consol_input) elif os.path.isdir(consol_input): for (dirpath, _, filenames) in walk(consol_input): file_names.extend( filter(lambda x: '.jack' in x, [(dirpath + ('/' if dirpath[-1] != '/' else '') + filename) for filename in filenames])) else: print('please enter .jack file or directory with .jack files') return for file_name in file_names: tokenizer = JackTokenizer(file_name) output_file_name = file_name[:file_name.index('.jack')] + 'MY.xml' output_file = open(output_file_name, 'w') compile_engine = CompilationEngine(tokenizer, output_file) compile_engine.compile_class() output_file.close()
def test_seven(self): compiler = CompilationEngine(FILE_SEVEN) root_node = compiler.compile() generator = VMGenerator() code = generator.process(root_node) correct_code = FileHandler(ROOT_DIR + '/output/Seven/Main.vm').fileContent self.assertEqual(correct_code, code)
def test_ClassSimplestAndVarDec2VarDecs(self): answer = ['<class>', '<keyword> class </keyword>', '<identifier> bar </identifier>', '<symbol> { </symbol>', '<classVarDec>', '<keyword> static </keyword>', '<keyword> boolean </keyword>', '<identifier> test </identifier>', '<symbol> ; </symbol>', '</classVarDec>', '<classVarDec>', '<keyword> field </keyword>', '<keyword> int </keyword>', '<identifier> test4 </identifier>', '<symbol> ; </symbol>', '</classVarDec>', '<symbol> } </symbol>', '</class>'] testCompEngine = CompilationEngine('testSnippetClassAndVarDec2VarDecs.jack', '/tmp/output') testCompEngine.CompileClass() counter = 0 with open('/tmp/output', mode='r') as f: lines = f.read().splitlines() print("AAA", lines) self.assertNotEqual(len(lines), 0) for line in lines: print("BBB", line) self.assertEqual(line, answer[counter]) counter += 1 self.assertEqual(counter, len(answer))
def test_complex_array(self): compiler = CompilationEngine(COMPLEX_ARRAY) root_node = compiler.compile() generator = VMGenerator() code = generator.process(root_node) correct_code = FileHandler(ROOT_DIR + '/output/ComplexArrays/Main.vm').fileContent self.assertEqual(correct_code, code)
def test_average(self): compiler = CompilationEngine(AVERAGE) root_node = compiler.compile() generator = VMGenerator() code = generator.process(root_node) correct_code = FileHandler(ROOT_DIR + '/output/Average/Main.vm').fileContent self.assertEqual(correct_code, code)
def test_square_game_file(self): engine = CompilationEngine("../Square/SquareGame.jack", "fakeOutputFile") engine.compile_class() xml_file = self.convert_xml_file("../Square/SquareGame.xml") self.assertEqual(len(engine.output), 643) self.assertEqual(engine.output, xml_file)
def __init__(self, path): filenames = os.listdir(path) for filename in filenames: if filename[-5:] == '.jack': compiler = CompilationEngine(path + '/' + filename) output_name = filename[:-5] + '.xml' compiler.compile().to_file(path + '/' + output_name)
def test_compile_return(self): engine = CompilationEngine("return;", "fakeOutputFile", True) engine.compile_return() self.assertEqual(engine.output, [ '<returnStatement>', '<keyword> return </keyword>', '<symbol> ; </symbol>', '</returnStatement>' ])
def compile(): fileDest = open(tokenizerDestFilePath, "w") tokens = JackTokenizer(filePath) lines = [] fileDest.write("<tokens>\n") while (tokens.hasMoreTokens()): tokens.advance() curType = tokens.tokenType() if curType == "stringConstant": curToken = tokens.stringVal() elif curType == "symbol": curToken = tokens.symbol() else: curToken = tokens.getCurrentToken() toWrite = "<" + curType + ">" + " " + curToken + " " + "</" + curType + ">\n" lines.append(toWrite) fileDest.write(toWrite) fileDest.write("</tokens>") fileDest.close() finalDestFile = open(finalPath, "w") engine = CompilationEngine(lines, finalDestFile) engine.CompileClass() finalDestFile.close()
def test_convert_to_bin(self): compiler = CompilationEngine(CONVERT_TO_BIN) root_node = compiler.compile() generator = VMGenerator() code = generator.process(root_node) correct_code = FileHandler(ROOT_DIR + '/output/ConvertToBin/Main.vm').fileContent self.assertEqual(correct_code, code)
def test_compile_class(self): engine = CompilationEngine("BasicTestClass.jack", "fakeOutputFile") engine.compile_class() self.assertEqual(engine.output, [ '<class>', '<keyword> class </keyword>', '<identifier> BasicTestClass </identifier>', '<symbol> { </symbol>', '<symbol> } </symbol>', '</class>' ])
def test_compile_var_dec(self): engine = CompilationEngine("var char key;", "fakeOutputFile", True) engine.compile_var_dec() self.assertEqual(engine.output, [ '<varDec>', '<keyword> var </keyword>', '<keyword> char </keyword>', '<identifier> key </identifier>', '<symbol> ; </symbol>', '</varDec>' ])
def create_compiled_file(jack_file_name, token_file_name): token_file = open(token_file_name, 'rU') engine_file = open(jack_file_name.replace('.jack', '') + '.xml', 'w') engine = CompilationEngine(engine_file, token_file) engine.compileClass() engine_file.close()
def translate_file(input_file, output_file): """ Translates the given input jack file to the given output vm file :param input_file: the input jack file :param output_file: the output vm file """ compilation_engine = CompilationEngine(input_file, output_file) compilation_engine.compile()
def test_compile_return_2(self): engine = CompilationEngine("return this;", "fakeOutputFile", True) engine.compile_return() self.assertEqual(engine.output, [ '<returnStatement>', '<keyword> return </keyword>', '<expression>', '<term>', '<keyword> this </keyword>', '</term>', '</expression>', '<symbol> ; </symbol>', '</returnStatement>' ])
def main(): jackExtension = ".jack" vmExtension = ".vm" if len(sys.argv) != 2: print "Wrong number of arguments." print "Usage: " + sys.argv[ 0] + " <Prog" + jackExtension + "> | <Directory>\n" return 1 else: ArgInputPath = sys.argv[1] InputFilePaths = [] # discover input files and fill list InputFilePaths if os.path.isdir(ArgInputPath): for entry in os.listdir(ArgInputPath): fullpath = os.path.join(ArgInputPath, entry) if not os.path.isdir(fullpath): InputFilePaths.append(fullpath) else: InputFilePaths.append(ArgInputPath) for InputPath in InputFilePaths: # skip non-jack files if not InputPath.endswith(jackExtension): print "Skipping " + InputPath + ". Not a " + jackExtension + " file." continue # get input file for reading try: InputFile = open(InputPath, "r") except IOError: print "The file \"" + InputPath + "\" does not exist." return 2 # make a tokenizer MyTokenizer = JackTokenizer(InputFile) # close input file InputFile.close() OutputPath = os.path.splitext(InputPath)[0] + vmExtension # get output file for writing try: OutputFile = open(OutputPath, "w") except IOError: print "There was a problem writing to " + OutputPath + "." return 3 # compilation engine MyCompilationEngine = CompilationEngine(MyTokenizer, OutputFile) # compile the class print "Writing to: " + OutputPath MyCompilationEngine.compileClass() # close output file OutputFile.close() return 0
def compile_jack(jackfile): out_vm_file = jackfile.with_suffix('.vm') # Tokenize jack_tokenizer = JackTokenizer(jackfile) tokens_with_tokenType = jack_tokenizer.tokenize() # Compile jack_compilation_engine = CompilationEngine(tokens_with_tokenType, out_vm_file) jack_compilation_engine.compile()
def test_compile_do(self): engine = CompilationEngine("do draw();", "fakeOutputFile", True) engine.compile_do() self.assertEqual(engine.output, [ '<doStatement>', '<keyword> do </keyword>', '<identifier> draw </identifier>', '<symbol> ( </symbol>', '<expressionList>', '</expressionList>', '<symbol> ) </symbol>', '<symbol> ; </symbol>', '</doStatement>' ])
def test_compile_class_var_dec(self): engine = CompilationEngine("field int direction;", "fakeOutputFile", True) engine.compile_class_var_dec() self.assertEqual(engine.output, [ '<classVarDec>', '<keyword> field </keyword>', '<keyword> int </keyword>', '<identifier> direction </identifier>', '<symbol> ; </symbol>', '</classVarDec>' ])
def main(): jackExtension=".jack" vmExtension=".vm" if len(sys.argv) != 2: print "Wrong number of arguments." print "Usage: " + sys.argv[0] + " <Prog"+jackExtension+"> | <Directory>\n" return 1 else: ArgInputPath = sys.argv[1] InputFilePaths = [] # discover input files and fill list InputFilePaths if os.path.isdir(ArgInputPath): for entry in os.listdir(ArgInputPath): fullpath=os.path.join(ArgInputPath,entry) if not os.path.isdir(fullpath): InputFilePaths.append(fullpath) else: InputFilePaths.append(ArgInputPath) for InputPath in InputFilePaths: # skip non-jack files if not InputPath.endswith(jackExtension): print "Skipping "+InputPath+". Not a "+jackExtension+" file." continue # get input file for reading try: InputFile = open(InputPath, "r") except IOError: print "The file \""+ InputPath + "\" does not exist." return 2 # make a tokenizer MyTokenizer = JackTokenizer(InputFile) # close input file InputFile.close() OutputPath=os.path.splitext(InputPath)[0]+vmExtension # get output file for writing try: OutputFile = open(OutputPath, "w") except IOError: print "There was a problem writing to "+OutputPath+"." return 3 # compilation engine MyCompilationEngine = CompilationEngine(MyTokenizer, OutputFile) # compile the class print "Writing to: "+OutputPath MyCompilationEngine.compileClass() # close output file OutputFile.close() return 0
def __init__(self, path): file_names = os.listdir(path) for filename in file_names: if filename[-5:] == '.jack': compiler = CompilationEngine(path + '/' + filename) root_node = compiler.compile() generator = VMGenerator() output_path = ''.join([path, '/', filename[:-5], '.vm']) generator.generate_vm_file(root_node, output_path)
def __init__(self, file_path): self.jack_files = self.parse_argv(file_path) for jack_file in self.jack_files: jack = JackTokenizer(jack_file) jack.translate() jack.close() txml_file = jack_file.replace(".jack", "T.xml") xml = CompilationEngine(txml_file) xml.compile_class()
def compile_jack(jack_file_name): token_file_name = jack_file_name.replace('.jack', 'T.xml') token_file = open(token_file_name, 'w') jack_file = open(jack_file_name, 'rU') tokenizer = JackTokenizer(jack_file, token_file) vm_file = open(jack_file_name.replace('.jack', '') + '.vm', 'w') vm_writer = VMWriter(vm_file) engine = CompilationEngine(vm_writer, tokenizer) engine.compile_class()
def test_compile_class_with_subroutine_description(): fake_file = BytesIO(b""" class X{ constructor X new(){ return this; } method void new(){ return this; } function void main() { return; } }""") t = Tokenizer(fake_file) c = CompilationEngine(t) node = c.compile() sd_1 = node.value[3].value sd_2 = node.value[4].value sd_3 = node.value[5].value # Test constructor assert (sd_1.value[0].name == 'keyword') assert (sd_1.value[0].value == 'constructor') assert (sd_1.value[1].name == 'identifier') assert (sd_1.value[1].value == 'X') assert (sd_1.value[2].name == 'identifier') assert (sd_1.value[2].value == 'new') assert (sd_1.value[3].name == 'symbol') assert (sd_1.value[3].value == '(') assert (sd_1.value[4].name == 'parameterList') assert (sd_1.value[4].value == []) assert (sd_1.value[5].name == 'symbol') assert (sd_1.value[5].value == ')') assert (sd_1.value[6].name == 'subroutineBody') assert (sd_1.value[6].value[0].value == '{') # Test method assert (sd_2.value[0].name == 'keyword') assert (sd_2.value[0].value == 'method') # Test function assert (sd_3.value[0].name == 'keyword') assert (sd_3.value[0].value == 'function') # Test that class closes properly assert (node.value[6].name == 'symbol') assert (node.value[6].value == '}')
def main(): if os.path.isdir(path): new_path = path if path.endswith("/"): new_path = path[:-1] for filename in os.listdir(new_path): if filename.endswith(JACK_SUFFIX): CompilationEngine(new_path + '/' + filename, new_path + '/' + filename[:-5] + XML_SUFFIX) else: CompilationEngine(path, path[:-5] + XML_SUFFIX)
def compile_jack(jack_file_name): token_file_name = jack_file_name.replace('.jack', 'T.xml') token_file = open(token_file_name, 'w') jack_file = open(jack_file_name, 'rU') tokenizer = JackTokenizer(jack_file, token_file) vm_file = open(jack_file_name.replace('.jack', '') + '.vm', 'w') vm_writer = VMWriter(vm_file) engine = CompilationEngine(vm_writer, tokenizer) engine.compileClass()
def writeFile(self, inputFile, inputDirName): from JackTokenizer import JackTokenizer from CompilationEngine import CompilationEngine import os outputFileName = os.path.join(inputDirName, "output", os.path.splitext(os.path.basename(inputFile.name))[0] + ".xml") if(not os.path.exists(os.path.dirname(outputFileName))): os.makedirs(os.path.dirname(outputFileName)) outputFile = open(outputFileName, 'w') tokenizer = JackTokenizer(inputFile) engine = CompilationEngine(tokenizer, outputFile) tokenizer.advance() engine.compileClass()
def writeFile(self, inputFile, inputDirName): from JackTokenizer import JackTokenizer from CompilationEngine import CompilationEngine import os basename = os.path.splitext(os.path.basename(inputFile.name))[0] xmlFileName = os.path.join(inputDirName, "output", basename + ".xml") vmFileName = os.path.join(inputDirName, "output", basename + ".vm") if not os.path.exists(os.path.dirname(xmlFileName)): os.makedirs(os.path.dirname(xmlFileName)) xmlFile = open(xmlFileName, "w") vmFile = open(vmFileName, "w") tokenizer = JackTokenizer(inputFile) engine = CompilationEngine(tokenizer, xmlFile, vmFile) tokenizer.advance() engine.compileClass()
def main(): input_path = sys.argv[FILE_POS] if isdir(input_path): input_list = [ input_path + "/" +f for f in listdir(input_path) if (isfile(join(input_path, f))) and (f.endswith(".jack")) ] for f in input_list: index = f.rfind(".jack") output_file_name = f[:index] + ".xml" compiler = CompilationEngine(f, output_file_name) compiler.compile_class() else: index = input_path.find(".jack") output_file_name = input_path[:index] + ".xml" compiler = CompilationEngine(input_path, output_file_name) compiler.compile_class()
def main(argv): source = argv[1] if isfile(source): file = source[0:len(source)-5] ce = CompilationEngine(source, file + ".vm") ce.compileClass() else: if not source.endswith('/'): source = source + '/' for f in listdir(source): if (isfile(source + f) and f.endswith(".jack")): file = f[0:len(f)-5] ce = CompilationEngine(source + file + ".jack", source + file + ".vm") ce.compileClass()
if isdir(source): filenames = [] source = args.source.rstrip('/') for filename_in in glob(join(source, '*.jack')): filename_out = join(source, splitext(basename(filename_in))[0] + suffix) filenames.append((filename_in, filename_out)) else: filename_in = args.source filename_out = join(split(args.source)[0], splitext(basename(source))[0] + suffix) filenames = [(filename_in, filename_out)] return filenames if __name__ == '__main__': args = get_arguments() filenames = get_filenames(args.source) for filename_in, filename_out in filenames: if args.stdout: file_out = sys.stdout else: file_out = open(filename_out, "w") tokenizer = JackTokenizer(open(filename_in)) compiler = CompilationEngine(tokenizer, file_out, args.debug) compiler.compileClass()
def analyzeFile(self, filename): tokenizer = Tokenizer(filename) tokenizer.tokenize() compiler = CompilationEngine(filename) compiler.compile() return
# specifying a dir name else: files = glob.glob('*.jack') # remove the asm file for filename in files: tmp = re.sub(r'\.jack', '.xml', filename) if os.path.exists(tmp): os.remove(tmp) if debug: print "rm : ", tmp else: if debug: print "not exist : ", tmp tmp = re.sub(r'\.jack', 'T.xml', filename) if os.path.exists(tmp): os.remove(tmp) if debug: print "rm : ", tmp else: if debug: print "not exist : ", tmp while len(files): filename = files.pop(0) if debug: print "load source filename : ", filename J = JackTokenizer(filename) J.write_all(re.sub(r'\.jack', 'T.xml', filename)) J = JackTokenizer(filename) C = CompilationEngine(re.sub(r'\.jack', '.xml', filename), J) C.compileClass()
from Parse import Parse infile = sys.argv[1] # Sys.argv is the system argument list object outfile = infile.replace(".jack",".xml") parse = Parse(infile) infileText = "" jtok = JackTokenizer() tokenList = [] while parse.hasMoreCommands(): parse.advance() blah = parse.output() infileText += blah jtok.advance(blah) tokenList.extend(jtok.listOfTokens) ce = CompilationEngine() ce.setListOfTokens(tokenList) ce.run() print ce.outtext output = open(outfile, 'w') output.write(ce.outtext)
#!/usr/bin/env python3 __author__ = 'Tao Zang' import sys import os from CompilationEngine import CompilationEngine if __name__ == '__main__': arg = sys.argv[1]; filename = os.path.basename(arg).split('.')[0]; if os.path.isfile(arg): output = arg.split('.')[0] + '.vm' engine = CompilationEngine(arg, output) engine.compileClass() else : files = [name for name in os.listdir(arg) if name.endswith('.jack')]; for file in files: engine = CompilationEngine(arg + '/' + file, arg + '/' + file.split('.')[0] + '.vm') engine.compileClass()