Пример #1
0
def analyze(sources):
    """
    For each source Xxx.jack file, the analyzer goes through the
    following logic:
    1.  Creates an output file called Xxx.xml and prepare it for writing;
    2.  Use the CompilationEngine to compile the input JackTokenizer into the
        output file.
    :param sources: list of names of sources to compile.
    """

    # Parse each source and translates to it the output:
    for sourcename in sources:
        base = os.path.splitext(sourcename)[0]
        outname_xml = base + XML_EXTENSION
        outname_vm = base + VM_EXTENSION

        # Open source for analyzing, output file for writing
        with open(sourcename, 'r') as source, open(outname_xml, 'w') as \
                outxml, open(outname_vm, 'w') as outvm:
            # Create a CompilationEngine from the Xvmxx.jack input file
            basename = os.path.basename(base)
            engine = CompilationEngine(basename, source, outxml, outvm)
            engine.compileClass()
Пример #2
0
    def Main(self):
        file_name = sys.argv[1]
        if os.path.isdir(file_name):
            files = os.listdir(file_name)
            for file in files:
                if ".jack" in file:
                    abspath = os.path.join(file_name,file)
                    with open(abspath, 'r') as current_file:
                        tokenizer = JackTokenizer(current_file)

                        with open(abspath.replace(".jack", ".vm"),'a') as vmFile:
                            engine = CompilationEngine(tokenizer,vmFile)
                            engine.compileClass() # at this moment only vmWriter deal with output file
                            #XMLfile.write("\n".join(Xml_code))

        # if the file name is rather a normal file
        else:
            with open(file_name, 'r') as current_file:
                tokenizer = JackTokenizer(current_file)
                symbolTable = SymbolTable()
                with open(file_name.replace(".jack", ".vm"), 'a') as vmFile:
                    engine = CompilationEngine(tokenizer, vmFile)
                    engine.compileClass()
Пример #3
0
while jt.hasMoreTokens():
    jt.advance()
    token_type = jt.tokenType()
    token = jt.func_list[token_type]()
    token_xml = token_xml + token

token_xml = token_xml + '</tokens>\n'
elements = token_xml.split('\n')

if debug:
    # print(token_xml)
    pass
else:
    of = open(token_xml_file_name, 'w+')
    of.write(token_xml)
    of.close()

output_file_name = token_xml_file_name.replace('T_', '_')

# part 2, compile
ce.Constructor(token_xml, output_file_name)
f = ce.compileClass()

if debug:
    print(f)
else:
    of = open(output_file_name, 'w')
    of.write(f)
    of.close()
Пример #4
0
import sys,os
 
filename=sys.argv[1]
readfile = open(filename,'r')
 
copyfile = open('copyfile','w')
line=readfile.readline()
while line:
	while line == '\n' or line.startswith('//'):
		line=readfile.readline()
	if '//' in line:
		line=line[:line.find('//')]
	if '/*' in line:
		aline=line[:line.find('/*')]
		while line.find('*/')<0:
			line=readfile.readline()
		bline=line[line.find('*/')+2:]
		line=aline+bline
	copyfile.write(line)
	line=readfile.readline()
copyfile.close()
readfile.close()
 
rfile=open('copyfile','r')
wfile=open(filename.strip('.jack')+'.xml','w')
 
CompilationEngine.compileClass(rfile,wfile)
 
rfile.close()
wfile.close()
os.remove('copyfile')