def test_CompileLet(self):
        ipt = StringIO.StringIO("""let blob[foo]=foo;;""")
        opt = StringIO.StringIO("")
        target_string = """<letStatement>
<keyword> let </keyword>
<identifier> blob </identifier>
<symbol> [ </symbol>
<expression>
<term>
<identifier> foo </identifier>
</term>
</expression>
<symbol> ] </symbol>
<symbol> = </symbol>
<expression>
<term>
<identifier> foo </identifier>
</term>
</expression>
<symbol> ; </symbol>
</letStatement>
"""
        comp = CompilationEngine.CompilationEngine(ipt, opt, True)
        comp.CompileLet()
        opt.seek(0)
        res = opt.read()
        self.assertEquals(res, target_string)
def main():
    # ファイル名処理
    path = sys.argv[1]

    if os.path.isfile(path):
        input_file_list = [sys.argv[1]]
    else:
        if path[-1] != '/':
            path = path + '/'
        input_file_list = glob.glob(path + '*.jack')

    # パース・コンパイル
    for input_file in input_file_list:
        output_file = input_file.replace('.jack', '.vm')
        c = CE.CompilationEngine(input_file, output_file)

        # トークンリストの作成
        c.j.make_token_list()

        if c.j.hasMoreTokens():
            c.j.advance()

        if c.j.token == 'class':
            c.compileClass()

        c.j.f.close()
        c.v.close()
    def test_CompileWhile(self):
        ipt = StringIO.StringIO("""while(foo){return bar;};""")
        opt = StringIO.StringIO("")
        target_string = """<whileStatement>
<keyword> while </keyword>
<symbol> ( </symbol>
<expression>
<term>
<identifier> foo </identifier>
</term>
</expression>
<symbol> ) </symbol>
<symbol> { </symbol>
<statements>
<returnStatement>
<keyword> return </keyword>
<expression>
<term>
<identifier> bar </identifier>
</term>
</expression>
<symbol> ; </symbol>
</returnStatement>
</statements>
<symbol> } </symbol>
</whileStatement>
"""
        comp = CompilationEngine.CompilationEngine(ipt, opt, True)
        comp.CompileWhile()
        opt.seek(0)
        res = opt.read()
        self.assertEquals(res, target_string)
Example #4
0
    def __processFile__(self, filePath):
        ''' processes a single file, first feeding the file to JackTokenizer to generate a list of tokens
            (output as T.xml files for debugging use) and that token list is fed through
            CompilationEngine to generate a final result list of XML tokens which is output into an .xml file. '''

        #Phase 1 Tokenize/Analyze
        tokenizer = JackTokenizer(filePath)
        print(filePath)

        xmlTokenList = ["<tokens>"]
        taggedTokenList = [("listStart", "tokens", xmlTokenList[0])]

        token = tokenizer.advance()
        while token:
            taggedToken = self.__wrapTokenInXML__(token)
            taggedTokenList += [taggedToken]
            xmlTokenList += [taggedToken[TT_XML]]
            token = tokenizer.advance()

        xmlTokenList += ["</tokens>"]
        length = len(xmlTokenList)
        taggedTokenList += [("listEnd", "tokens", xmlTokenList[length - 1])]

        Tfilename = str(filePath.parent) + '/' + filePath.stem + "T.xml"
        self.__output__(Tfilename, xmlTokenList)

        #Phase 2 Compile/Translate
        compiler = CompilationEngine(taggedTokenList)
        compiledXMLList = compiler.compileTokens()

        Cfilename = str(filePath.parent) + '/' + filePath.stem + ".xml"
        self.__output__(Cfilename, compiledXMLList)
Example #5
0
def tokenizeAndCompile(filename):
    '''
	In: .jack file 
	Out: xxx.xml file, xxx.vm file 
	Function: handles parsing the names of all the files
	calls JackTokenizer object to create T.xml file 
	then calls CompilationEngine to create .xml file 
	'''

    # get outfileName ('T.xml')
    ToutfileName = os.path.abspath(filename).split('.')[0] + "T.xml"
    if os.path.isfile(ToutfileName): os.remove(ToutfileName)

    tokObj = JT.JackTokenizer(os.path.abspath(filename), ToutfileName)
    tokObj.tokenize()
    tokArray = tokObj.getTokenizedList()

    # remove xxxT.xml file - we needed it for Project 10, but not anymore
    outfileTName = os.path.abspath(filename).split('.')[0] + "T.xml"
    if os.path.isfile(outfileTName): os.remove(outfileTName)

    # get outfileName (name of the final .xml file)
    outfileName = os.path.abspath(filename).split('.')[0] + ".xml"
    if os.path.isfile(outfileName): os.remove(outfileName)

    # get outfileName (name of the final .vm file)
    outVMName = os.path.abspath(filename).split('.')[0] + ".vm"
    if os.path.isfile(outVMName): os.remove(outVMName)

    # inputs to CompilationEngine are JackTokenizerOutput array and outfileName
    c = CE.CompilationEngine(tokArray, outfileName, outVMName)
    c.compileClass()
    def run(self):
        for input_filename in self.input_filenames:
            self.tokenizer_output_filename = input_filename.split(
                '.')[0] + 'T.xml'
            self.final_output_filename = input_filename.split('.')[0] + '.xml'
            with open(input_filename) as in_f:
                print('Compiling ' + input_filename)
                with open(self.tokenizer_output_filename, 'w') as out_f:
                    jt = JackTokenizer.JackTokenizer(in_f, out_f)
                    while jt.has_more_tokens():
                        token_type = jt.token_type()
                        if token_type == 'KEYWORD':
                            jt.keyword()
                        elif token_type == 'SYMBOL':
                            jt.symbol()
                        elif token_type == 'IDENTIFIER':
                            jt.identifier()
                        elif token_type == 'INT_CONST':
                            jt.int_val()
                        elif token_type == 'STRING_CONST':
                            jt.string_val()
                        jt.advance()
                    jt.save_xml()

            with open(self.final_output_filename, 'w') as out_f:
                ce = CompilationEngine.CompilationEngine(
                    self.tokenizer_output_filename, out_f)
        print('Compiling finished.')
Example #7
0
    def __processFile__(self, filePath):
        ''' processes a single file, first feeding the file to JackTokenizer to generate a list of tokens
            (output as T.xml files for debugging use) and that token list is fed through
            CompilationEngine to generate a final result list of XML tokens which is output into an .xml file. '''

        #TODO  make it work

        # create opening token tag for tokenizing lines of .jack
        tokens = ["<tokens>"]
        tokenizer = JackTokenizer(filePath)

        line =  tokenizer.advance()

        # tokenize each line of .jack
        while line:
            tokens += [self.__wrapTokenInXML__(line)]
            line = tokenizer.advance()

        tokens += ["</tokens>"]

        # 2. create a list for compiled tokens to go into, create compEngine instance
        #     compile the tokens
        compiledTokens = []
        compEngine = CompilationEngine(tokens)
        compiledTokens += compEngine.compileTokens()

        # create the filepath names for writing the tokens and full blown xml
        xml_T_FilePath = Path(filePath.parent / (filePath.stem + 'T.xml'))
        finalTokenPath = Path(filePath.parent / (filePath.stem + '.xml'))

        # write out the raw tokens
        self.__output__(xml_T_FilePath, tokens)
        self.__output__(finalTokenPath, compiledTokens)
Example #8
0
def main():
    if len(sys.argv) != 2:
        print("Usage: JackAnalyzer.py fileName.jack/directoryName")
        sys.exit(1)

    in_put = sys.argv[1]

    # Creates a list with all the jack files and
    # saves the path where xml files are going to be created
    if os.path.isfile(in_put):
        jack_files = [in_put]
        in_path = os.path.abspath(in_put)
        in_path = in_path[:in_path.rfind('/')]
    elif os.path.isdir(in_put):
        jack_files = [file for file in os.listdir(in_put) if '.jack' in file]
        in_path = os.path.abspath(in_put)

    # Creates a directory for the xml files
    xml_path = os.path.join(in_path, 'xml')
    try:
        os.mkdir(xml_path)
    except FileExistsError:
        pass

    # Runs the Jack Analyzer for every jack file, and create each corresponding xml and vm file
    for jack_file in jack_files:
        xml_filepath = os.path.join(xml_path, jack_file.split('.')[0] + '.xml')
        vm_path = os.path.join(in_path, jack_file.split('.')[0] + '.vm')
        # Opens each out file and writes xml and vm code
        with open(os.path.join(in_path, jack_file), 'r') as in_file:
            xml_file = open(xml_filepath, 'w')
            CompilationEngine(in_file, xml_file, vm_path).run()
            xml_file.close()
Example #9
0
 def _analyzeFile(self, inputPath):
     outputPath = inputPath.replace('.jack', '.xml')
     outputFile = open(outputPath, 'w')
     compilationEngine = CompilationEngine.CompilationEngine(
         inputPath, outputFile)
     compilationEngine.CompileClass()
     outputFile.close()
def main():

    script, fileName = sys.argv
    fileName = fileName
    isDirectory = False

    if fileName.endswith('.jack'):
        c = CE.CompilationEngine(fileName)
    else:
        try:
            os.chdir(fileName)
            isDirectory = True
        except:
            err = 'Please enter a jack file or directory' \
                  ' containing jack files'
            print err
            sys.exit()

    if isDirectory:
        files = glob.glob('*.jack')
        if files == []:
            err = 'Please enter a jack file or directory' \
                  ' containing jack files'
            print err
            sys.exit()

        for f in files:
            try:
                c = CE.CompilationEngine(f)
                c.compile_class()
            except:
                print 'An error occurred'
                for f in glob.glob('*.vm'):
                    os.remove(f)
                # raise

    else:
        try:
            c = CE.CompilationEngine(fileName)
            c.compile_class()

        except:
            print 'An error occurred'
            for f in glob.glob('*.vm'):
                os.remove(f)
            # raise
    return
Example #11
0
def main():
    root = Tk()
    root.withdraw()
    inputFilepath = filedialog.askdirectory(initialdir = "/", title = "Select a folder")

    for fileName in os.listdir(inputFilepath):
        if fileName.lower().endswith(".jack"):
            compilationEngine = CompilationEngine.CompilationEngine(os.path.basename(inputFilepath) + "\\" + fileName, os.path.basename(inputFilepath) + "\\" + fileName[:-5] + ".xml")
Example #12
0
def compileFile(filename):
    engine = CompilationEngine.CompilationEngine(filename)
    engine.compileClass()
    xml_string = et.tostring(engine.root, encoding='UTF-8', method='html')
    xml_string = xml_string.decode().replace("><", ">\n<")
    xml_file = open(filename.replace('.jack', '.xml'), 'w')
    xml_file.write(xml_string)
    xml_file.close()
Example #13
0
def main():
    fin = open('Main.jack', 'r')
    xout = open('test_out.xml', 'w')

    j = JackTokenizer(fin)
    tokens = j.getTokens()
    CompilationEngine(tokens, xout)
    fin.close()
Example #14
0
def doAll(name):
    """
    runs the compiler for a specific ".jack" file, or, if given a directory
    name, runs the translator for all ".jack" files in directory
    :param name: file/directory name
    """
    if Consts.OLD_SUFFIX == name.split('.')[-1]:
        engine = CompilationEngine.CompilationEngine(name)
        engine.compileClass()
        engine.closeWriter()
    else:
        for fileName in os.listdir(name):
            if Consts.OLD_SUFFIX == fileName.split('.')[-1]:
                engine = CompilationEngine.CompilationEngine(name + "//" +
                                                             fileName)
                engine.compileClass()
                engine.closeWriter()
def analyze(filename):
    """
    Given file/directory, analyze it
    :param filename:
    """
    tokenizer = JackTokenizer(filename)
    tokenizer.tokenize()
    engine = CompilationEngine(tokenizer)
    engine.write(filename.replace('.jack', '.vm'))
    def test_CompileSubroutine(self):
        ipt = StringIO.StringIO(
            """function void test(int blob,foo bar){var int test; return (3+"foo");};"""
        )
        opt = StringIO.StringIO("")
        target_string = """<subroutineDec>
<keyword> function </keyword>
<keyword> void </keyword>
<identifier> test </identifier>
<symbol> ( </symbol>
<parameterList>
<keyword> int </keyword>
<identifier> blob </identifier>
<symbol> , </symbol>
<identifier> foo </identifier>
<identifier> bar </identifier>
</parameterList>
<symbol> ) </symbol>
<subroutineBody>
<symbol> { </symbol>
<varDec>
<keyword> var </keyword>
<keyword> int </keyword>
<identifier> test </identifier>
<symbol> ; </symbol>
</varDec>
<statements>
<returnStatement>
<keyword> return </keyword>
<expression>
<term>
<symbol> ( </symbol>
<expression>
<term>
<integerConstant> 3 </integerConstant>
</term>
<symbol> + </symbol>
<term>
<stringConstant> foo </stringConstant>
</term>
</expression>
<symbol> ) </symbol>
</term>
</expression>
<symbol> ; </symbol>
</returnStatement>
</statements>
<symbol> } </symbol>
</subroutineBody>
</subroutineDec>
"""
        comp = CompilationEngine.CompilationEngine(ipt, opt, True)
        comp.CompileSubroutine()
        opt.seek(0)
        res = opt.read()
        self.assertEquals(res, target_string)
Example #17
0
    def _generate_code_file(self, src):
        tree = ET.ElementTree(file=src)
        engine = CompilationEngine.CompilationEngine(tree)
        engine.CompileClass(engine.desRoot)

        des = src.split(CompilationEngine.TOKEN_FILE)[0] + CompilationEngine.GEN_CODE_FILE
        engine.desTree._setroot(engine.desTree.getroot()[0])#skip first empty token
        engine.desTree.write(os.path.join(path, des), short_empty_elements=False)

        CompilationEngine.pretty_format_xml(os.path.join(path, des), engine.desTree)
Example #18
0
def test_compiler():
    inputFileList = makeInputFileList(getUserInput())
    #tokens = ""

    for file in inputFileList:
        jkt = JackTokenizer.JackTokenizer()
        tokens = jkt.tokenize(file)  # return token list
        comp = CompilationEngine.CompilationEngine(tokens, file)
        compiledTokens = comp.compile()
        print(compiledTokens)
def main():
    # Parameter Configuration
    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group()
    group.add_argument("-f",dest="file")
    args=parser.parse_args()

    #Object Initialization
    print "Starting Compilation"
    comp=CompilationEngine.CompilationEngine(open(args.file),open(args.file.split(".")[0]+".vm","w"),False)
Example #20
0
def analyze_file(given_file):
    """
    creates a tokenizer for the given file, extracts all tokens
    and...
    :param given_file: a jack file
    :return: None
    """
    cur_tokenizer = tokenizer.Tokenizer(given_file)
    engine = CompilationEngine.CompilationEngine(cur_tokenizer)
    engine.compile()
Example #21
0
def analyze(jackFile):
    outputFilename = os.path.splitext(jackFile)[0] + ".xml.cmp"
    t = Tokenizer.Tokenizer(jackFile)
    ce = CompilationEngine.CompilationEngine(t, outputFilename)

    t.advance()
    if t.keyword() != "class":
        print("jack file does not have a class!")
        exit(1)

    ce.CompileClass()
Example #22
0
def main():
    inputFileList = makeInputFileList(getUserInput())
    #tokens = ""

    for file in inputFileList:
        jkt= JackTokenizer.JackTokenizer()
        tokens = jkt.tokenize(file) # return token list
        #outputFile = open(outputFileName, 'a')
        #outputFile.write(tokens)
        comp = CompilationEngine.CompilationEngine(tokens, file)
        compiledTokens = comp.compile()
Example #23
0
def main():
    userInput = sys.argv[1]

    if os.path.isdir(userInput):
        if not userInput.endswith("/"):
            userInput += "/"
        files = os.listdir(userInput)
        for file in files:
            if file.endswith('.jack'):
                fileName = file.split(".")[0]
                comp = CompilationEngine.CompilationEngine(userInput + file, userInput + fileName + ".vm")
                comp.compileClass()
    #Case input is file, just parse it
    elif os.path.isfile(userInput):
        userInput = userInput.split(".")[0]
        comp = CompilationEngine.CompilationEngine(userInput + ".jack", userInput + ".vm")
        comp.compileClass()
    #Raise an exception
    else:
        raise Exception("The input is not valid, please try again")
Example #24
0
 def create_out(self):
     """
     creates the output files - first we get the output file name and
     then  we create a compilation engine with our in file and out file
     and the engine compiles the infile and saves the output in
     the output file
     """
     for file in self._inputs:
         out_name = str(os.path.basename(file).split('.')[0]) + ".vm"
         comp = CompilationEngine(file, self._out_path + "/" + out_name)
         comp.compile_class()
Example #25
0
def main():
    if len(sys.argv) != 2:
        print('ERR SYS ARGS')
        return
    input_path = os.path.abspath(sys.argv[1])
    if input_path.endswith('.jack'):
        input_path = [input_path]
    else:
        input_path = glob.glob(input_path + '/*.jack')
    for _ in input_path:
        out_vm = _.replace('.jack', '.vm')
        CompilationEngine(_, out_vm)
Example #26
0
def main():
    if len(sys.argv) != 2:
        print(' Usage error - please insert file or directory.')
        return
    input_path = os.path.abspath(sys.argv[1])
    if input_path.endswith('.jack'):
        input_path = [input_path]
    else:
        input_path = glob.glob(input_path + '/*.jack')
    for _ in input_path:
        out_xml = _.replace('.jack', '.xml')
        CompilationEngine(_, out_xml)
Example #27
0
def evaluate():
    data = request.json["expression"]
    print(data)
    res = str()
    try:
        engine = CompilationEngine(data)
        res = engine.run()
        print("res " + str(res))
    except CompilationError:
        res = "Invalid Expression"
    finally:
        return jsonify(result=res)
Example #28
0
def handle_files(files_list, dir_path):
    """
    Main func go over the lines of the files
    :param files_list: list of files in the dir
    :param dir_path : path to save to
    """
    for file_name in files_list:
        file_explicit_name, file_extension = os.path.splitext(file_name)
        f = open(os.path.join(dir_path, file_explicit_name + XML), 'w')
        compilation_eng = CompilationEngine.CompilationEngine(
            os.path.join(dir_path, file_name), f)
        compilation_eng.compile_class()
        f.close()
Example #29
0
def compileJack(jackFile):
    outputFilename = os.path.splitext(jackFile)[0] + ".vm"
    t = Tokenizer.Tokenizer(jackFile)
    vmw = VMWriter.VMWriter(outputFilename)
    ce = CompilationEngine.CompilationEngine(t, vmw)

    t.advance()
    if t.keyword() != "class":
        print("jack file does not have a class!")
        exit(1)

    ce.CompileClass()
    vmw.close()
Example #30
0
def compile_file(file_path):
    '''Compile a file by its path, save the result to a file
	with the same name and an XML suffix'''

    with open(file_path, 'r') as ifile:
        file_name = os.path.basename(file_path)
        file_path_no_ext, _ = os.path.splitext(file_path)
        file_name_no_ext, _ = os.path.splitext(file_name)

        ofile_path = file_path_no_ext + '.xml'
        with open(ofile_path, 'w') as ofile:
            tokenizer = JackTokenizer.JackTokenizer(ifile.read())
            compiler = CompilationEngine.CompilationEngine(tokenizer, ofile)
            compiler.compile_class()