Example #1
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>"]

        # print(tokens)
        # write out the raw tokens
        xml_T_FilePath = Path(filePath.parent / (filePath.stem + 'T.xml'))
        self.__output__(xml_T_FilePath, 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
        finalTokenPath = Path(filePath.parent / (filePath.stem + '.xml'))

        self.__output__(finalTokenPath, compiledTokens)

        # ===============================
        # ======== project 11 stuff =====
        # ===============================
        vmPath = Path(filePath.parent / (filePath.stem + '.vm'))
        vmCommands = compEngine.get_vmInstructions()
        self.__output__(vmPath, vmCommands)