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>"]

        # 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 #2
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 #3
0
def main():
    global fn
    userInput = sys.argv[1]
    if ".jack" in userInput:
        filename = userInput
        if "/" not in filename:
            pos = filename.find(".jack")
            fn = filename[:pos]
            #newpath = fn
            #if not os.path.isdir(newpath):
            #	os.makedirs(newpath)
            outfilenameT = filename[:filename.index(".")] + "T.xml"
            outfilename = filename[:filename.index(".")] + ".vm"
        else:
            last_pos = filename.rfind("/")
            pos = filename.find(".jack")
            fn = filename[last_pos + 1:pos]
            savepath = filename[:last_pos]
            #newpath = savepath + "/" + fn
            #if not os.path.isdir(newpath):
            #	os.makedirs(newpath)
            outfilenameT = filename[last_pos + 1:filename.rfind(".")] + "T.xml"
            outfilename = filename[last_pos + 1:filename.rfind(".")] + ".vm"
        ofT = open(outfilenameT, "w")
        xmlOp = Tokenizer.tokenizer(filename)
        ofT.write(xmlOp)
        ofT.close()
        of = open(outfilename, "w")
        xmlOp = CompilationEngine.compilationEngine(outfilenameT)
        of.write(xmlOp)
        of.close()
    else:
        os.chdir(userInput)
        if userInput.endswith("/"):
            last_pos = userInput.rfind("/")
            second_last_pos = userInput[:last_pos].rfind("/")
            #newpath = userInput[second_last_pos + 1: last_pos]
        else:
            if "/" in userInput:
                last_pos = userInput.rfind("/")
            else:
                last_pos = 0
            #newpath = userInput[last_pos :]
        #if not os.path.isdir(newpath):
        #	os.makedirs(newpath)
        for filename in os.listdir(os.getcwd()):
            if filename.endswith(".jack"):
                pos = filename.find(".jack")
                fn = filename[:pos]
                outfilenameT = fn + "T.xml"
                outfilename = fn + ".vm"
                ofT = open(outfilenameT, "w")
                xmlOp = Tokenizer.tokenizer(filename)
                ofT.write(xmlOp)
                ofT.close()
                of = open(outfilename, "w")
                xmlOp = CompilationEngine.compilationEngine(outfilenameT)
                print(outfilenameT)
                of.write(xmlOp)
                of.close()
def analyze(filename):
    """
    Given file/directory, analyze it
    :param filename:
    """
    tokenizer = JackTokenizer(filename)
    tokenizer.tokenize()
    engine = CompilationEngine(tokenizer)
    engine.write(filename.replace('.jack', '.vm'))
Example #5
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 #6
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 #7
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 #8
0
def translate_jack_file(file):
    """
    translate only one vm file to asm file
    :param file: name of file
    :return: None
    """
    input_file = open(file, "r")
    # remove comments from file
    clean_file = first_pass(input_file)
    # file to list of tokens
    tokenized = jt.file_to_tokens(clean_file)
    ce.translate_token(tokenized, file[:FILE_NAME_LAST_INDEX])
    input_file.close()
Example #9
0
def generateFile(foldername, filename):  # (Square/mine/, Main.jack)

    jack_filename = foldername.rsplit(
        "/", 2)[0] + "/" + filename  # "Square" + "/" + "Main.jack"
    tokens = JackTokenizer.tokenize(
        jack_filename)  # returns tokens generated from .jack file

    vm_filename = foldername + filename.split(
        ".")[0] + ".vm"  # let VMWriter know what file to write to
    VMWriter.initializeFile(
        vm_filename)  # open .vm file to begin writing to it

    # pass tokens from Tokenizer to CompilationEngine
    class_name = filename.split(".")[0]
    CompilationEngine.compileTokens(tokens, class_name)
    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 #11
0
 def _analyzeFile(self, inputPath):
     outputPath = inputPath.replace('.jack', '.xml')
     outputFile = open(outputPath, 'w')
     compilationEngine = CompilationEngine.CompilationEngine(
         inputPath, outputFile)
     compilationEngine.CompileClass()
     outputFile.close()
Example #12
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 #14
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()
    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()
Example #17
0
 def analyze(self):
     for jack_file in self.jackFiles:
         tokenizer = T.Tokenizer(jack_file)
         xml_file = jack_file.replace('.jack', '.xml')
         comp_engine = CE.Parsing(tokenizer, xml_file)
         comp_engine.outFile.close()
         tokenizer.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 #19
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 #20
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()
Example #21
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 #22
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")
    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)
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 #25
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 #26
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)
Example #27
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 #28
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 #29
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 #30
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 #31
0
import sys
from JackTokenizer import *
from CompilationEngine import *

if(__name__=="__main__"):
    jackTokenizer = JackTokenizer(sys.argv[1])
    compilationEngine = CompilationEngine(jackTokenizer)
    compilationEngine.write(sys.argv[2])

    
Example #32
0
import JackTokenizer
import CompilationEngine
import os
import VMWriter
import SymbolTable
rfile = r"C:\Users\Liu_100\Desktop\nand2tetris\nand2tetris\projects\11\Pong\Ball.jack"
xml = os.path.splitext(rfile)[0] + 'MyVersion.xml'
vm = os.path.splitext(rfile)[0] + 'MyVersion.vm'
xml = open(xml,'w')
vm = open(vm,'w')
jackTokenizer = JackTokenizer.jacktokenizer(rfile, xml)
vmWriter = VMWriter.VMWriter(vm)
symbolTable = SymbolTable.SymbolTable()
compiler = CompilationEngine.compilationengine(xml,symbolTable,vmWriter)
compiler.compileClass(jackTokenizer)

xml.close()
vmWriter.close()
Example #33
0
import JackTokenizer
import CompilationEngine
import os
rfile = r"C:\Users\Liu_100\Desktop\nand2tetris\nand2tetris\projects\10\Square\Square.jack"
wfile = os.path.splitext(rfile)[0] + 'FinalVersion.xml'
wfile = open(wfile,'w')
jackTokenizer = JackTokenizer.jacktokenizer(rfile, wfile)
compiler = CompilationEngine.compilationengine(wfile)
compiler.compileClass(jackTokenizer)
wfile.close()