Beispiel #1
0
def main():
    inputFileName = getInputName()
    isDirectory = os.path.isdir(inputFileName)

    if isDirectory:
        files = os.listdir(inputFileName)
        vmFiles = filter(isVMFile, files)
        listOfFiles = list(
            map(lambda fileName: inputFileName + "/" + fileName, vmFiles))

        outputFileName = trimFile(inputFileName)
        writer = CodeWriter(inputFileName + "/" + outputFileName)
        if len(listOfFiles) > 1:
            writer.bootstrap()
    else:
        listOfFiles = [inputFileName]
        outputFileName = trimFile(inputFileName)
        writer = CodeWriter(outputFileName)

    for currentFile in listOfFiles:
        print("File to be parsed: {}".format(currentFile))

        writer.setNewInputFile(trimFile(currentFile))
        parser = Parser(currentFile)

        while parser.hasMoreCommands():
            parser.advance()
            command = parser.getCommand()
            if parser.isArithmetic():
                writer.writeArithmetic(command)
            elif parser.isPush() or parser.isPop():
                writer.writePushPop(command)
            elif parser.isLabel():
                writer.writeLabel(command)
            elif parser.isGoto():
                writer.writeGoto(command)
            elif parser.isIf():
                writer.writeIf(command)
            elif parser.isFunction():
                writer.writeFunction(command)
            elif parser.isCall():
                writer.writeCall(command)
            elif parser.isReturn():
                writer.writeReturn(command)
            elif parser.isComment() or parser.isBlankLine():
                continue
            else:
                raise Exception("Command not supported")

    parser.close()
    writer.close()
    print("VMTranslator finished.")
Beispiel #2
0
    for vm_file in dir_content_filtered:
        files_names.append('/'.join([full_dir_name, vm_file, ]))


print('dest_file - {}'.format(dest_file))
print('.vm files to process - {}'.format(files_names))

cw = CodeWriter(dest_file)

# We Want to run preprocessing, to check if there is a need for bootstrapping and using the Sys.init
# if none of the files contains this functions we should not add the bootstrapping
# In general this case should not happen, but because some of the tests here are unit tests,
# we will add the bootstrapping code only when we have Sys.vm file
for file in files_names:
    if "sys.vm" in file.lower():
        cw.bootstrap()

# RUN!
for file in files_names:
    p = Parser(file)
    cw.update_file_name(file)

    while p.has_more_command():

        c_type = p.command_type()

        if c_type == Lex.C_ARITMETIC:
            cw.write_aritmethic(p.current_command())
        elif c_type == Lex.C_PUSH_OR_POP:
            cw.write_push_pop(p.current_command(), p.arg1(), p.arg2())
        elif c_type == Lex.C_IF: