예제 #1
0
    def __init__(self, input_filename, output_file=None):
        self.input_filename = input_filename
        self.output_file = output_file
        self.symbol_table = SymbolTable.SymbolTable()

        self.subtoutine_name = ''
        self.subroutine_type = ''
        self.class_name = ''
        self.if_index = 0
        self.while_index = 0
        self.var_number = 0

        self.in_xml = xml.dom.minidom.parse(self.input_filename)
        self.input_child_node_idx = 1

        self.vm_writer = VMWriter.VMWriter(input_filename[:-5])

        self.current_token = self.in_xml.documentElement.childNodes[
            self.input_child_node_idx]
        self.doc = xml.dom.minidom.Document()
        while self.current_token.nodeName == 'keyword' and self.current_token.childNodes[
                0].nodeValue == 'class':
            self.compile_class()
        self.__save_xml()

        pass
예제 #2
0
 def __init__(self, tokenizer):
     """
     Ctor
     _writer: a VM writer object
     _cur_class: the current class
     _cur_func: the current function
     _cur_func_type: the current function type
     _subroutine_table: a symbol table of the subroutine
     _class_table: a list of the symbols in the class
     _tokenizer: the tokenizer object
     _result: a list that is written in the output file
     counters: the kind counters
     label_counters: the label counters
     """
     self._writer = VMWriter()
     self._cur_class = None
     self._cur_func = None
     self._cur_func_type = None
     self._subroutine_table = None
     self._class_table = []
     self._tokenizer = tokenizer
     self._result = []
     self.counters = {'static': 0, 'field': 0, 'argument': 0, 'local': 0}
     self.label_counters = {"if": 0, "while": 0}
     self.compileClass()
예제 #3
0
    def __init__(self, inFile, outFile):
        """ Creates a new CompilationEngine with given
            input and output. The next routine called must
            be compileClass() """

        # Create an object of JackTokenizer with the input file
        self.tokenizer = JackTokenizer.JackTokenizer(inFile)

        # Create an object of SymbolTable
        self.symbolTable = SymbolTable.SymbolTable()

        # Create an object of VMWriter
        self.vmWriter = VMWriter.VMWriter(outFile[:-3] + 'vm')

        # Open a output file to write to
        self.out = open(outFile, 'w')

        self.currentToken = ''
        self.currentTokenType = ''
        self.tabs = 0
        self.i = 0  # for creating unique labels
        self.nfield = 0
        self.nstatic = 0

        self.__advance()
예제 #4
0
    def __init__(self, jackTokenizerOutput, outfileName, outVMName):
        '''
		In: jackTokenizerOutput which is the array of tokens output by JackTokenizer.py
		Function: Creates a new compilation engine with the given input and output. 
		The next routine called must be compileClass().
		'''

        self.tokenArray = jackTokenizerOutput
        self.tokenIndex = 0
        self.line = self.tokenArray[0]  # first item of array is class

        self.curToken = self.line.split()[
            1]  # the second item in a line is the token

        self.curTokenType = self.line.split()[
            0]  # gets the first tag of the token

        # set buffersize to 0
        self.f1 = open(outfileName, 'a', 0)

        # project 11 part: add Symbol table and VMWriter output
        self.sTable = st.SymbolTable()
        self.vm = vmw.VMWriter(outVMName)

        # project 11 class variables
        self.curClass = ''  # name of the current class
        self.whileCount = 0
        self.ifCount = 0
예제 #5
0
 def __init__(self, tokenizer, ostream, file_name_no_ext):
     '''Initialize the compilation engine
     @tokenizer the tokenizer from the input code file
     @ostream the output stream to write the code to'''
     self.tokenizer = tokenizer
     self.vm_writer = VMWriter.VMWriter(ostream)
     self.className = file_name_no_ext
예제 #6
0
 def openXMLFile(self, xml_file):
     self.xml_tree = ET.parse(xml_file)
     self.tokens = list(self.xml_tree.getroot())
     self.tokens.reverse()
     self.st = SymbolTable()
     self.current_class = xml_file[:-5]
     self.vm = VMWriter(self.current_class + '.vm')
예제 #7
0
 def __init__(self, jack_lines):
     self._jack_lines = jack_lines
     self._token = tk.JackTokenizer(jack_lines)
     self._writer = vmw.VMWriter()
     self._table = st.SymbolTable()
     self._class = None
     self._cur_subroutine_ret_type = None
 def __init__(self, file):
     self.lex = Lex(file)
     self.symbols = SymbolTable()
     self.vm = VMWriter()
     self.openout(file)
     self.compile_class()
     self.closeout()
예제 #9
0
 def __init__(self, tokens, file_path):
     self.tokens = tokens
     self.class_symboltable = SymbolTable()
     self.subroutine_symboltable = SymbolTable()
     self.cur_class = None
     self.cur_subroutine_type = None  # for the compile return function
     self.vmwriter = VMWriter(file_path)
     self.labelcounter = 0
예제 #10
0
 def __init__(self, inputPath, outputPath):
     self._jackTokenizer = JackTokenizer.JackTokenizer(inputPath)
     if self._jackTokenizer.hasMoreTokens():
         self._jackTokenizer.advance()
     self._vmWriter = VMWriter.VMWriter(outputPath)
     self._symbolTable = SymbolTable.SymbolTable()
     self._currentClassName = ''
     self._whileCount = self._ifCount = 0
예제 #11
0
def compile_file(jack_file_name, vm_file_name):
    jack_file = open(jack_file_name, 'r')
    tokenizer = Tokenizer.Tokenizer(jack_file)
    symbol_table = SymbolTable.SymbolTable()
    vm_file = open(vm_file_name, 'w')
    vm_writer = VMWriter.VMWriter(vm_file)
    compilation_engine = CompilationEngine(tokenizer, vm_writer, symbol_table)
    compilation_engine.compile_class()
예제 #12
0
    def main(self, to_parse):

        # Files to parse consists of file path to parse and file name to write
        files_to_parse = []
        # print("to_parse", to_parse, to_parse[-4:])
        # If file
        if to_parse[-4:] == "jack":

            for_parsing = self.get_files(to_parse)
            files_to_parse.append(for_parsing)

        # If directory
        else:
            get_path = self.get_file_path(to_parse)[1]
            to_check = os.listdir(get_path)
            for f in to_check:
                if f[-4:] == "jack":
                    new_p = to_parse + "/" + f
                    for_parsing = self.get_files(new_p)
                    files_to_parse.append(for_parsing)

        for f in files_to_parse:

            # reset server for unit tests
            self.reset(f[1], f[2], f[3])
            # print("current f", f)
            #
            try:
                t = T.Tokenizer()
                # Read
                with open(f[0]) as f1:
                    with open(f[2], 'a') as fw:
                        # Write Tokens file
                        T.Tokenizer.file_to_write = fw
                        for line in f1:
                            ln = t.remove_comments(line)
                            if ln:
                                t.make_tokens(ln)
                        # END
                        def end():
                            return "</tokens>"

                        fw.write(end())

                vm = VMWriter.VMWriter()
                vm.file_to_write = f[3]
                c = C.Compiler()
                c.vm = vm
                # Write compiled code
                with open(f[1], 'a') as fw:
                    c.tag_generator = XML_H.XMLHelper.read_xml_tags(f[2])
                    xml = c.compile_class()
                    xml_str = ET.tostring(xml,
                                          encoding='unicode',
                                          short_empty_elements=False)
                    fw.write(xml_str)
            except IOError:
                print("File or directory is missing")
예제 #13
0
 def __init__(self, tokenizer):
     self.tokenizer = tokenizer
     self.output_file = open('temp', 'w')
     self.CLASSES = []
     self.classTable = symbolTable.SymbolTable()
     self.subRoutineTable = symbolTable.SymbolTable()
     self.vm_writer = VMWriter.VMWriter()
     self.if_label_counter = 0
     self.while_label_counter = 0
예제 #14
0
    def __init__(self, inputFile, outputFile):
        # self.outFile = outputFile
        self.tokenizer = JackTokenizer(inputFile)
        self.vmwriter = VMWriter(outputFile)
        self.outFile = outputFile
        self.currToken = ""
        self.tabber = ""

        self.argumentsCounter = 0
예제 #15
0
def translate_token(tokenized, root):
    global index, output_file
    vars_table.restart_table()
    output_file = vm.VMWriter(root, tokenized[1])
    index += 1
    # class_name = tokenized[index]  # name
    index += 1
    index += 1
    inside_class(tokenized)
 def __init__(self, input_file, xml_file, vm_file):
     ''' Initialize the xml file, the symbol table adn the VMwriter '''
     super().__init__(input_file)
     self.xml_file = xml_file
     self.symbol_tab = SymbolTable()
     self.vm_writer = VMWriter(vm_file)
     self.subroutine_type = None
     self.if_label, self.while_label = 0, 0
     self.isarray = None
예제 #17
0
 def __init__(self, file_in, file_out):
     '''
     Creates a new compilation engine with the given input and output. 
     Input: file_in (string), file_out (string)
     '''
     self.tokenizer = JackTokenizer.JackTokenizer(file_in)
     self.symbol_table = SymbolTable.SymbolTable()
     self.vm_writer = VMWriter.VMWriter(file_out)
     self.class_name = ''
     self.idx = 0
예제 #18
0
 def __init__(self, tokenizer, outputFile):
     self.XMLCode = []
     self.CodeIndent = 0
     self.tokenizer = tokenizer
     self.symbolTable = SymbolTable()
     self.vmWriter = VMWriter(outputFile)
     self.class_name = None
     self.segment_local_dict = segment_dict
     self.while_count = 0
     self.if_count = 0
예제 #19
0
 def __init__(self, input, output):
     """
     creates a new compilation engine with
     the given input and output.
     """
     self.tokenizer = JackTokenizer.JackTokenizer(input)
     self.writer = VMWriter.VMWriter(output)
     self.symbolTable = SymbolTable.SymbolTable()
     self.className = ''
     self.name = ''
예제 #20
0
 def __init__(self, tokenizer, output):
     """Creates a new compilation engine with the given input and output.
              The next routine called must be compileClass(). """
     self.tokenizer = tokenizer
     self.tags = []
     self.scope = 0
     self.writer = VMWriter(output)
     self.class_table = SymbolTable()
     self.method_table = SymbolTable()
     self.class_name = ''
     self.method_or_constructor = False
예제 #21
0
    def __init__(self, input, output):
        """

        :param input: input file name
        :param output: output file name whhere the text will be written
        """
        self.tokenizer = Tokenizer.Tokenizer(input)
        self.writer = VMWriter.VMWriter(output)
        self.symbolTable = SymbolTable.SymbolTable()
        self.classname = ""
        self.name = ""
예제 #22
0
 def __init__(self, tokenizedArray):
     self.tokenizedArray = tokenizedArray
     self.tokenizedArraySize = len(tokenizedArray)
     self.curIndex = 0
     self.indentLevel = 0
     self.compiledArray = []
     self.INDENT_SIZE = 2
     self.className = ''
     self.labelsCounter = 0
     self.symbolsTable = SymbolTable.SymbolsTable()
     self.VMWriter = VMWriter.VMWriter()
예제 #23
0
    def __init__(self, input_file, output_file):
        """
        与えられた入力と出力に対して新しいコンパイルエンジンを生成する
        次に呼ぶルーチンは compileClass() でなければならない
        str, str -> void
        """
        self.j = JT.JackTokenizer(input_file)
        self.s = ST.SymbolTable()
        self.v = VMW.VMWriter(output_file)

        self.class_name = ''
예제 #24
0
 def __init__(self, input_path, output_path):
     """
     creates a new compilation engine with the given input and output. the next routine called must be compileClass()
     :param input_path: input stream/file
     :param output_path: output stream/file
     """
     self.labels = 0
     self.jack_class = None
     self.class_subroutine = None
     self.tokenizer = JackTokenizer(input_path)
     self._writer = VMWriter(output_path)
     self.CompileClass()
예제 #25
0
    def __init__(self, file):
        """ """
        self.label_num = 0
        self.tokenizer = JackTokenizer(file)
        self.advance()

        self.symbols = SymbolTable()
        self.vm = VMWriter()

        self.open_outfile(file)
        self.compile_class()
        self.close_outfile()
예제 #26
0
 def __init__(self, infile, outfile):
     self.className = ""
     self.symbolTable = SymbolTable.SymbolTable()
     self.tokenizer = jackTokenizer(infile)
     self.writer = VMWriter.VMWriter(outfile)
     self.indent = ""
     ##        self.mStack =[] #a stack to store all non terminal blocks opened which have to be closed
     self.keywordConsts = {'true', 'false', 'null', 'this'}
     self.binaryOp = {'+', '-', '*', '|', '<', '>', '=', '/', '&'}
     self.unaryOp = {'-', '~'}
     self.function_type = ""
     self.is_unary = False
예제 #27
0
 def __init__(self, filename):
     """
     constructor, creates a new compilation engine with the given input
     :param filename: the input file name
     """
     self._tokenizer = JackTokenizer.JackTokenizer(filename)
     self._className = filename.split('/')[-1].split('.')[0]
     self._filenameVM = filename.split('.')[0] + '.' + co.NEW_SUFFIX
     self._vmWriter = vm.VMWriter(self._filenameVM)
     self._simba = st.SymbolTable()
     self._ifCount = 0
     self._whileCount = 0
예제 #28
0
    def __init__(self, input_file, tokenizer):
        self.indent_level = 0
        self.tokenizer = tokenizer
        self.symbol_table = SymbolTable.SymbolTable()
        self.label_count = 0

        self.vm_output_path = input_file.replace('.jack', '.vm')
        self.writer = VMWriter.VMWriter(self.vm_output_path, 'w')
        self.xml_output = open(input_file.replace(
            '.jack', '.xml'), 'w')

        self.compileClass()
        self.xml_output.close()
예제 #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()
예제 #30
0
 def __init__(self, in_file, out_file):
     """
     A compilation engine constructor
     :param in_file: the file we are currently compiling
     :param out_file: the file where we save the output
     """
     self._tokenizer = JackTokenizer(in_file)
     self._class_table = SymbolTable()
     self._method_table = SymbolTable()
     self._cur_class_name = ""
     self._vm_writer = VMWriter(out_file)
     self._label_count_while = 0
     self._label_count_if = 0